Confluence 2.10 has reached end of life
Check out the [latest version] of the documentation
The v2 search API provides a fast way of searching content within Confluence. We highly recommend that all plugin authors switch to this API where possible.
To illustrate how to use this API, we have included a simple code snippet for a basic search that:
- searches for all content labelled with administration in the space with key DOC.
- sorts these results with the latest modified content displayed first.
- limits the number of results to 10.
SearchQuery query = BooleanQuery.composeAndQuery(new LabelQuery("administration"), new InSpaceQuery("DOC"));
SearchSort sort = new ModifiedSort(SearchSort.Order.DESCENDING); // latest modified content first
SearchFilter securityFilter = SiteSearchPermissionsSearchFilter.getInstance();
ResultFilter resultFilter = new SubsetResultFilter(10);
Search search = new Search(query, sort, securityFilter, resultFilter);
SearchResults searchResults;
try
{
searchResults = searchManager.search(search);
}
catch (InvalidSearchException e)
{
// discard search and assign empty results
searchResults = LuceneSearchResults.EMPTY_RESULTS;
}
// iterating over search results
for (SearchResult searchResult : searchResults.getAll())
{
System.out.println("Title: " + searchResult.getDisplayTitle());
System.out.println("Content: " + searchResult.getContent());
System.out.println("SpaceKey: " + searchResult.getSpaceKey());
}
// total number of results found
System.out.println("Total number of results: " + searchResults.getUnfilteredResultsCount());
Further comments:
Please ensure you include
com.atlassian.confluence.search.v2.searchfilter.SiteSearchPermissionsSearchFilterin your search. This is a bundled filter that will handle permission checking and content filtering automatically for you.- The number of results returned has been limited with the use of
com.atlassian.confluence.search.v2.filter.SubsetResultFilter. This class efficiently filters search results during search time. - The search is executed using
searchManager.search(search). This invocation returns search results populated with data from your index.- To iterate over the search results returned, you can get a reference to the list of search results with
searchResults.getAll()or an iterator to this list usingsearchResults.iterator(). - Common information about a search result like title, body and space key can be extracted from the search result using
getDisplayTitle(),getContent()andgetSpaceKey()respectively. For more accessors see the API documentation forcom.atlassian.confluence.search.v2.SearchResult. - This invocation does not go to the database to construct any search results. If you want
com.atlassian.bonnie.Searchableobjects from the database to be returned by the search, callsearchManager.searchEntities(search)instead.
- To iterate over the search results returned, you can get a reference to the list of search results with
- An exception
com.atlassian.confluence.search.v2.InvalidSearchExceptionis thrown when either:- there is an error mapping a v2 search object to the corresponding Lucene search object, or
- no mapper could be found to map one of the search objects. (The mapper plugin responsible for mapping this search may have been uninstalled.)
- You should simply discard the search if an exception is thrown as described above.
RELATED TOPICS
Overview
Content Tools
Apps
