close

Filter

loading table of contents...

Unified API Developer Manual / Version 2310

Table Of Contents

5.8 Search Service of the Unified API

The SearchService provides full-text search capabilities. You can use its methods to quickly find contents based on their current property values and some of their implied properties (such as content 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 does not allow searching in old content item versions.

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

The SearchService has the following methods:

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

  • search methods to search for not deleted contents using a simple query language as described 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 contents for which the user of the current session does not have read rights. You must handle rights yourself and filter out unreadable contents if needed.

Search with Simple Query Language

Use one of the search methods to perform a simple search for not deleted contents. There are multiple search methods with different parameters to restrict the query to contents 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 contents 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 contents 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 contents containing the phrase Hello World can be performed with:

"hello world"

The following example searches for content items of any type (but not folders) 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,   // documents only
                      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 content. The following example searches for all contents created by user admin that are located below the folder /Site and contain the word test.

User user = userRepository.getUserByName("admin");
int userId = IdHelper.parseUserId(user.getId());

Content folder = contentRepository.getChild("/Sites");
int folderId = IdHelper.parseContentId(folder.getId());

String query = "feederstate:SUCCESS" +
               " creator:" + userId +
               " folderpath:" + folderId +
               " test";

SearchResult result = searchService.searchNative(
  query,
  "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 contents that were not successfully indexed. In the latter case you must include the term feederstate:ERROR. If you don't want to find contents in the recycle bin, you must either search below a given folder, as 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.

Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.