5.6. Search Service of the Unified API

The SearchService provides full-text search capabilities. You can use its methods to quickly find documents based on their current property values and some of their implied properties (such as document creator).

Client search requests are routed through the CoreMedia Content Server to the CoreMedia Search Engine. The CoreMedia Search Engine is a facade to a configured third-party search server which by default is an Apache Solr instance. The search server returns the search result back to the CoreMedia Content Server and the requesting client.

[Note]Note

Note, that the index of the CoreMedia Search Engine is updated asynchronously and therefore does not always represent the current state of the content repository. Note further, that the CoreMedia Search Engine searches for current documents only.

If you need up-to-date results or want to search for folders or document versions, you should consider using the QueryService.

The SearchService has the following methods:

  • isSearchEnabled returns true, if search service is enabled, false otherwise.

  • getSearchConnectionInfo to get information how to contact the third-party search server directly. The information string is a JSON formatted object, which structure depends on the configured search server.

  • search methods to search for non-archived documents using a simple query language describe below.

  • searchNative to search in a search server specific search query language, like Apache Solr Query Language described below.

[Caution]Caution

Note, that search, and searchNative methods may return documents for which the user of the current session does not have read rights. You must handle rights yourself and filter out unreadable documents if needed.

Search with Simple Query Language

Use one of the search methods to perform a simple search for non-archived documents. There are multiple search methods with different parameters to restrict the query to documents of a given type and below a given folder. See the API documentation for details.

These methods take a query string in a simple query language which consists of terms and/or phrases separated by white space. The terms and/or phrases are combined with a logical AND.

A query term is basically a word to search for. Only alphanumeric characters are allowed here. You can prefix the term with a minus operator ('-') to indicate a NOT expression, that is the word must not appear in the search results. Likewise, you can use a plus operator ('+') as prefix but it is the default and will be ignored. The following example query will search for documents which contain the word news but not the word sport:

news -sport

The query term may end with an asterisk ('*') to perform a wildcard query which matches all words that start with the characters before the asterisk. Note, that the asterisk may appear at the end of the term only. The next example returns all documents containing words that start with test:

test*

A phrase to search for is enclosed in double quotes. Wildcards are not allowed in phrases and plus and minus operators are ignored. A search for documents containing the phrase Hello World can be performed with:

"hello world"

The following example searches for documents of any type which contain the word hamburg below a folder /Site. The list obtained from the SearchResult contains the found Content objects sorted by their name. Sorting is explained in a following section.

Content site = contentRepository.getChild("Site");
ContentType type = contentRepository.getDocumentContentType();
SearchResult result =
 searchService.search("hamburg",    // the query string
                      "name", true, // sort ascending by name
                      site, true,   // below folder site
                      type, true,   // any type
                      0, 100);      // max. 100 hits

List<Content> hits = result.getMatches();
Search with Solr Query Language

If you need a more powerful search with Apache Solr directly, you can use the more generic searchNative method and perform a query in the Solr Query Language. For details on the query language refer to the Apache Solr documentation.

One of the fields in the Solr schema is creator which contains the ID of the user who created the document. The following example searches for all documents created by user admin that are located below the folder /Site and contain the word test.

User admin = userRepository.getUserByName("admin");

SearchResult result = searchService.searchNative(String.format(
 "feederstate:SUCCESS creator:\"%s\" folder:(/Site OR /Site/*) " +
  "test", admin.getId())
 "name", true,      // sort ascending by name
 0, 100);           // max. 100 hits

List<Content> hits = result.getMatches();

An important thing to note is the term feederstate:SUCCESS within the query string. You must specify this term in every query except when searching for documents that were not successfully indexed. In the latter case you must include the term feederstate:ERROR. If you don't want to find documents in the recycle bin, you must either search below a given folder, shown in the example above, or include the term isdeleted:false.

The term test is not prefixed with the name of an index field. In that case the default field textbody is used and the search is performed on the full-text content.

Sort search results

You can use method parameters of the search and searchNative methods to specify the sorting of the returned results. The search methods take the name of the search field and whether sorting should be ascending or descending as parameters and return the results sorted accordingly. Sorting is handled by the search engine which is much more efficient than client-side sorting could be.