[opencms-dev] CmsSearch - search for all words, any of the word, exact phrase, exclude words

Christian Steinert christian_steinert at web.de
Mon Mar 9 07:21:12 CET 2009


Kesarkar, Dipak ha scritto:
> Hi,
>
>  
>
> I am using setQuery method of the CmsSearch class for simple searches.
>
>  
>
>             CmsSearch cmsSearch = new CmsSearch();
>
>  
> cmsSearch.init(CmsObjectLoader.getInstance().getCmsObject());
>
>             cmsSearch.setIndex(this.searchIndex);
>
>             cmsSearch.setField(this.searchFields);
>
>             cmsSearch.setSearchPage(0); //To get all the search results
>
>  
>
>             cmsSearch.setQuery(searchKey)
>
>             List<CmsSearchResult> currentSearchResults =
> cmsSearch.getSearchResult();
>
>  
>
>  
>
>  
>
> I need to have advanced search facilities like search for all words, any
> of the word, exact phrase, exclude words
>
>  
>
>  
>
> How can I use CmsSearch for such advance searches?
>
> Is there any other way? 

What I did was to just rewrite the actual search queries. If the 
user-provided query does not contain things like quotes/AND/OR then I will:
- replace all multi-whitespace with a single space
- remove any whitespace at the beginning and end of the search string (trim)
- replace any space by " AND "

In other words, I insert AND in between each word and then hand that 
modified query over to opencms. This searches for all words that the 
user proided. "Any words" (OR) is default behavior. Phrases can be 
searched for by enclosing the search query with double quotes. Exclude 
can be done by adding a minus "-" before each word that should be excluded.

Rewriting the query like this should be the easiest way.

hth
Christian




More information about the opencms-dev mailing list