Sample search results pages

From OpenCms Wiki
Jump to: navigation, search

You would use the search index in a JSP something like this:


CmsJspActionElement cmsJae = new CmsJspActionElement(pageContext, request, response);
CmsSearch search = new CmsSearch();
search.setIndex("online");
search.setQuery(queryString);
search.init(cmsJae.getCmsObject());
List searchResults = search.getSearchResult();
Iterator iterator = searchResults.iterator();
while (iterator.hasNext()) {
   CmsSearchResult r = (CmsSearchResult)iterator.next();
   //do stuff...
}


Often you want to exclude some files or directories from the search results. You can do this by using search categories. All you have to do is set a default category in the properties for the root folder of the site (for example "searchable"). This property is inherited by folders and files in the site, as long as you do not overwrite it. Now set this category when initializing the search object:

search.setCategories(new String[] {"searchable"} );

Files or complete folders that shall not appear in the search results simply gets a different category (for example "nosearch").

Of course you can add other categories and use them for other purposes, but as long as you always set a category in the search object, you will not find files in the "forbidden" category.

Remember to rebuild the index after assigning categories.


Have a look at a result page example for showing custom xml content fields on Writing_only_specific_xml-element-data_to_the_lucene_index#Creating_a_search_result_page

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox