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:
1 2@Named public class V2SearchExample { @ConfluenceImport private SearchManager searchManager; @Inject public V2SearchExample(SearchManager searchManager) { this.searchManager = searchManager; } public void search() { SearchQuery query = BooleanQuery.andQuery(new LabelQuery("administration"), new InSpaceQuery("DOC")); SearchSort sort = new ModifiedSort(SearchSort.Order.DESCENDING); // latest modified content first SearchFilter searchFilter = SiteSearchPermissionsSearchFilter.getInstance(); ContentSearch search = new ContentSearch(query, sort, searchFilter, 0, 10); SearchResults searchResults; try { searchResults = searchManager.search(search); } catch (InvalidSearchException e) { // discard search and assign empty results searchResults = new DefaultSearchResults(Collections.emptyList(), 0); } for (SearchResult searchResult : searchResults.getAll()) { System.out.println("Title: " + searchResult.getDisplayTitle()); System.out.println("Content: " + searchResult.getContent()); System.out.println("SpaceKey: " + searchResult.getSpaceKey()); } System.out.println("Total number of results: " + searchResults.getUnfilteredResultsCount()); } }
Further comments:
com.atlassian.confluence.search.v2.searchfilter.SiteSearchPermissionsSearchFilter in your search. This is a bundled filter that will handle permission checking and content filtering automatically for you.ContentSearch parameters.searchManager.search(search). This invocation returns search results populated with data from the index targeted by the search.
ContentSearch and ChangesSearch will target the content and change indexes respectively. Any other implementations of ISearch passed to the searchManager should return which search index to target via search.getSearchIndexes().searchResults.getAll() or an iterator to this list using searchResults.iterator().getDisplayTitle(), getContent(), and getSpaceKey() respectively. For more accessors, see the API documentation for com.atlassian.confluence.search.v2.SearchResult.com.atlassian.bonnie.Searchable objects from the database, call searchManager.searchEntities(search) instead.com.atlassian.confluence.search.v2.InvalidSearchException is thrown in one of the following variants:
Rate this page: