Search | Couchbase Docs (original) (raw)
The Search Service provides an array of options to customize your query. The following table lists them all:
Table 3. Available Search Options
Name | Description |
---|---|
limit(int) | Allows to limit the number of hits returned. |
skip(int) | Allows to skip the first N hits of the results returned. |
explain(boolean) | Adds additional explain debug information to the result. |
scanConsistency(SearchScanConsistency) | Specifies a different consistency level for the result hits. |
consistentWith(MutationState) | Allows to be consistent with previously performed mutations. |
highlight(HighlightStyle, String…) | Specifies highlighting rules for matched fields. |
sort(Object) | Allows to provide custom sorting rules. |
facets(Map<String, SearchFacet>) | Allows to fetch facets in addition to the regular hits. |
fields(String…) | Specifies fields to be included. |
serializer(JsonSerializer) | Allows to use a different serializer for the decoding of the rows. |
raw(String, Object) | Escape hatch to add arguments that are not covered by these options. |
collections(String…) | Limits the search query to a specific list of collection names. |
Limit and Skip
It is possible to limit the returned results to a maximum amount using the limit
option. If you want to skip the first N records it can be done with the skip
option.
SearchResult result = cluster.searchQuery("travel-sample-index", SearchQuery.queryString("swanky"),
searchOptions().skip(3).limit(4));
ScanConsistency and ConsistentWith
By default, all Search queries will return the data from whatever is in the index at the time of query. These semantics can be tuned if needed so that the hits returned include the most recently performed mutations, at the cost of slightly higher latency since the index needs to be updated first.
There are two ways to control consistency: either by supplying a custom SearchScanConsistency
or using consistentWith
. At the moment the cluster only supports consistentWith
, which is why you only see SearchScanConsistency.NOT_BOUNDED
in the enum which is the default setting. The way to make sure that recently written documents show up in the rfc works as follows (commonly referred to "read your own writes" — RYOW):
MutationResult mutationResult = collection.upsert("key", JsonObject.create().put("description", "swanky"));
MutationState mutationState = MutationState.from(mutationResult.mutationToken().get());
SearchResult searchResult = cluster.searchQuery("travel-sample-index", SearchQuery.queryString("swanky"),
searchOptions().consistentWith(mutationState));
Highlight
It is possible to enable highlighting for matched fields. You can either rely on the default highlighting style or provide a specific one. The following snippet uses HTML formatting for two fields:
SearchResult result = cluster.searchQuery("travel-sample-index", SearchQuery.queryString("swanky"),
searchOptions().highlight(HighlightStyle.HTML, "description", "type"));
Sort
By default the Search Engine will sort the results in descending order by score. This behavior can be modified by providing a different sorting order which can also be nested.
SearchResult result = cluster.searchQuery("travel-sample-index", SearchQuery.queryString("swanky"),
searchOptions().sort(SearchSort.byScore(), SearchSort.byField("description")));
Facets are aggregate information collected on a result set and are useful when it comes to categorization of result data. The SDK allows you to provide many different facet configurations to the Search Engine, the following example shows how to create a facet based on a term. Other possible facets include numeric and date ranges.
Facets
Map<String, SearchFacet> facets = new HashMap<>();
facets.put("types", SearchFacet.term("type", 5));
SearchResult result = cluster.searchQuery("travel-sample-index", SearchQuery.queryString("United States"),
searchOptions().facets(facets));
Fields
You can tell the Search Engine to include the full content of a certain number of indexed fields in the response.
SearchResult result = cluster.searchQuery("travel-sample-index", SearchQuery.queryString("swanky"),
searchOptions().fields("description", "type"));
Collections
It is now possible to limit the search query to a specific list of collection names.
Note that this feature is only supported with Couchbase Server 7.0 or later.
SearchResult result = cluster.searchQuery("travel-sample-index", SearchQuery.queryString("San Francisco"),
searchOptions().collections("landmark", "airport"));
Custom JSON Serializer
As with all JSON APIs, it is possible to customize the JSON serializer. You can plug in your own library (like GSON) or custom configure mappings on your own Jackson serializer. This in turn makes it possible to serialize rows into POJOs or other structures that your application defines, and which the SDK has no idea about.