Package com.couchbase.client.java.search
Class SearchQuery
- java.lang.Object
-
- com.couchbase.client.java.search.SearchQuery
-
@Committed @Public public class SearchQuery extends Object
The FTS API entry point. Describes an FTS query entirely (index, query body and parameters) and can be used at theBucketlevel to perform said query. Also has factory methods for all types of fts queries (as in the various types a query body can have: term, match, conjunction, ...).- Since:
- 2.3.0
- Author:
- Simon Baslé, Michael Nitschinger
-
-
Constructor Summary
Constructors Constructor Description SearchQuery(String indexName, AbstractFtsQuery queryPart)Prepare an FTSSearchQueryon an index.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description SearchQueryaddFacet(String facetName, SearchFacet facet)Adds oneSearchFacetto the query.static BooleanFieldQuerybooleanField(boolean value)Prepare aBooleanFieldQuerybody.static BooleanQuerybooleans()Prepare aBooleanQuerybody.SearchQueryclearFacets()Clears all previously addedSearchFacet.SearchQueryclearHighlight()Clears any previously configured highlighting.static ConjunctionQueryconjuncts(AbstractFtsQuery... queries)Prepare aConjunctionQuerybody.SearchQueryconsistentWith(Document... docs)Sets the consistency to consider for this FTS query to AT_PLUS and uses the mutation information from the given documents to parameterize the consistency.SearchQueryconsistentWith(MutationState mutationState)Sets the consistency to consider for this FTS query to AT_PLUS and uses theMutationStatedirectly to parameterize the consistency.SearchQueryconsistentWith(DocumentFragment... fragments)Sets the consistency to consider for this FTS query to AT_PLUS and uses the mutation information from the given document fragments to parameterize the consistency.static DateRangeQuerydateRange()Prepare aDateRangeQuerybody.static DisjunctionQuerydisjuncts(AbstractFtsQuery... queries)Prepare aDisjunctionQuerybody.static DocIdQuerydocId(String... docIds)Prepare aDocIdQuerybody.SearchQueryexplain()Activates the explanation of each result hit in the response.SearchQueryexplain(boolean explain)Activates or deactivates the explanation of each result hit in the response, according to the parameter.JsonObjectexport()Exports the whole query as aJsonObject.SearchQueryfields(String... fields)Configures the list of fields for which the whole value should be included in the response.static GeoBoundingBoxQuerygeoBoundingBox(double topLeftLon, double topLeftLat, double bottomRightLon, double bottomRightLat)Prepare aGeoBoundingBoxQuerybody.static GeoDistanceQuerygeoDistance(double locationLon, double locationLat, String distance)Prepare aGeoDistanceQuerybody.static GeoPolygonQuerygeoPolygon(List<Coordinate> points)Prepare aGeoPolygonQuerybody.Map<String,SearchFacet>getFacets()String[]getFields()String[]getHighlightFields()HighlightStylegetHighlightStyle()IntegergetLimit()LonggetServerSideTimeout()IntegergetSkip()SearchQueryhighlight()Configures the highlighting of matches in the response for all fields, using the server's default highlighting style.SearchQueryhighlight(HighlightStyle style, String... fields)Configures the highlighting of matches in the response.SearchQueryhighlight(String... fields)Configures the highlighting of matches in the response, for the specified fields and using the server's default highlighting style.StringindexName()voidinjectParams(JsonObject queryJson)Inject the top level parameters of a query into a preparedJsonObjectthat represents the root of the query.SearchQuerylimit(int limit)Add a limit to the query on the number of hits it can return.static MatchQuerymatch(String match)Prepare aMatchQuerybody.static MatchAllQuerymatchAll()Prepare aMatchAllQuerybody.static MatchNoneQuerymatchNone()Prepare aMatchNoneQuerybody.static MatchPhraseQuerymatchPhrase(String matchPhrase)Prepare aMatchPhraseQuerybody.static NumericRangeQuerynumericRange()Prepare aNumericRangeQuerybody.static PhraseQueryphrase(String... terms)Prepare aPhraseQuerybody.static PrefixQueryprefix(String prefix)Prepare aPrefixQuerybody.AbstractFtsQueryquery()static QueryStringQueryqueryString(String query)Prepare aQueryStringQuerybody.static RegexpQueryregexp(String regexp)Prepare aRegexpQuerybody.SearchQuerysearchConsistency(SearchConsistency consistency)Sets the unparameterized consistency to consider for this FTS query.SearchQueryserverSideTimeout(long timeout, TimeUnit unit)Sets the server side timeout.SearchQueryskip(int skip)Set the number of hits to skip (eg.SearchQuerysort(Object... sort)Configures the list of fields (including special fields) which are used for sorting purposes.static TermQueryterm(String term)Prepare aTermQuerybody.static TermRangeQuerytermRange()Prepare aTermRangeQuerybody.static WildcardQuerywildcard(String wildcard)Prepare aWildcardQuerybody.
-
-
-
Constructor Detail
-
SearchQuery
public SearchQuery(String indexName, AbstractFtsQuery queryPart)
Prepare an FTSSearchQueryon an index. Top level query parameters can be set after that by using the fluent API.- Parameters:
indexName- the FTS index to search in.queryPart- the body of the FTS query (eg. a match phrase query).
-
-
Method Detail
-
indexName
public String indexName()
- Returns:
- the name of the index targeted by this query.
-
query
public AbstractFtsQuery query()
- Returns:
- the actual query body.
-
export
public JsonObject export()
Exports the whole query as aJsonObject.
-
injectParams
public void injectParams(JsonObject queryJson)
Inject the top level parameters of a query into a preparedJsonObjectthat represents the root of the query.- Parameters:
queryJson- the preparedJsonObjectfor the whole query.
-
limit
public SearchQuery limit(int limit)
Add a limit to the query on the number of hits it can return.- Parameters:
limit- the maximum number of hits to return.- Returns:
- this SearchQuery for chaining.
-
skip
public SearchQuery skip(int skip)
Set the number of hits to skip (eg. for pagination).- Parameters:
skip- the number of results to skip.- Returns:
- this SearchQuery for chaining.
-
explain
public SearchQuery explain()
Activates the explanation of each result hit in the response.- Returns:
- this SearchQuery for chaining.
-
explain
public SearchQuery explain(boolean explain)
Activates or deactivates the explanation of each result hit in the response, according to the parameter.- Parameters:
explain- should the response include an explanation of each hit (true) or not (false)?- Returns:
- this SearchQuery for chaining.
-
highlight
public SearchQuery highlight(HighlightStyle style, String... fields)
Configures the highlighting of matches in the response. This drives the inclusion of thefragmentsin eachhit. Note that to be highlighted, the fields must be stored in the FTS index.- Parameters:
style- theHighlightStyleto apply.fields- the optional fields on which to highlight. If none, all fields where there is a match are highlighted.- Returns:
- this SearchQuery for chaining.
-
highlight
public SearchQuery highlight(String... fields)
Configures the highlighting of matches in the response, for the specified fields and using the server's default highlighting style. This drives the inclusion of thefragmentsin eachhit. Note that to be highlighted, the fields must be stored in the FTS index.- Parameters:
fields- the optional fields on which to highlight. If none, all fields where there is a match are highlighted.- Returns:
- this SearchQuery for chaining.
-
highlight
public SearchQuery highlight()
Configures the highlighting of matches in the response for all fields, using the server's default highlighting style. This drives the inclusion of thefragmentsin eachhit. Note that to be highlighted, the fields must be stored in the FTS index.- Returns:
- this SearchQuery for chaining.
-
clearHighlight
public SearchQuery clearHighlight()
Clears any previously configured highlighting.- Returns:
- this SearchQuery for chaining.
- See Also:
highlight(HighlightStyle, String...)
-
fields
public SearchQuery fields(String... fields)
Configures the list of fields for which the whole value should be included in the response. If empty, no field values are included. This drives the inclusion of thefieldsin eachhit. Note that to be highlighted, the fields must be stored in the FTS index.- Parameters:
fields-- Returns:
- this SearchQuery for chaining.
-
sort
public SearchQuery sort(Object... sort)
Configures the list of fields (including special fields) which are used for sorting purposes. If empty, the default sorting (descending by score) is used by the server. The list of sort fields can include actual fields (like "firstname" but then they must be stored in the index, configured in the server side mapping). Fields provided first are considered first and in a "tie" case the next sort field is considered. So sorting by "firstname" and then "lastname" will first sort ascending by the firstname and if the names are equal then sort ascending by lastname. Special fields like "_id" and "_score" can also be used. If prefixed with "-" the sort order is set to descending. If no sort is provided, it is equal to sort("-_score"), since the server will sort it by score in descending order.- Parameters:
sort- the fields that should take part in the sorting.- Returns:
- this SearchQuery for chaining.
-
addFacet
public SearchQuery addFacet(String facetName, SearchFacet facet)
Adds oneSearchFacetto the query. This is an additive operation (the given facets are added to any facet previously requested), but if an existing facet has the same name it will be replaced. This drives the inclusion of theSearchQueryResult.facets()facets} in theSearchQueryResult. Note that to be faceted, a field's value must be stored in the FTS index.- Parameters:
facetName- the name of the facet to add (or replace if one already exists with same name).facet- the facet to add.
-
clearFacets
public SearchQuery clearFacets()
Clears all previously addedSearchFacet.- Returns:
- this SearchQuery for chaining.
- See Also:
addFacet(String, SearchFacet)
-
serverSideTimeout
public SearchQuery serverSideTimeout(long timeout, TimeUnit unit)
Sets the server side timeout. By default, the SDK will set this value to the configuredsearchTimeoutfrom the environment.- Parameters:
timeout- the server side timeout to apply.unit- the unit for the timeout.- Returns:
- this SearchQuery for chaining.
-
searchConsistency
public SearchQuery searchConsistency(SearchConsistency consistency)
Sets the unparameterized consistency to consider for this FTS query. This replaces any consistency tuning previously set.- Parameters:
consistency- the simple consistency to use.- Returns:
- this SearchQuery for chaining.
-
consistentWith
public SearchQuery consistentWith(Document... docs)
Sets the consistency to consider for this FTS query to AT_PLUS and uses the mutation information from the given documents to parameterize the consistency. This replaces any consistency tuning previously set.- Parameters:
docs- one or modeDocumentto get mutation state information from.- Returns:
- this SearchQuery for chaining.
-
consistentWith
public SearchQuery consistentWith(DocumentFragment... fragments)
Sets the consistency to consider for this FTS query to AT_PLUS and uses the mutation information from the given document fragments to parameterize the consistency. This replaces any consistency tuning previously set.- Parameters:
fragments- one or modeDocumentFragmentto get mutation state information from.- Returns:
- this SearchQuery for chaining.
-
consistentWith
public SearchQuery consistentWith(MutationState mutationState)
Sets the consistency to consider for this FTS query to AT_PLUS and uses theMutationStatedirectly to parameterize the consistency. This replaces any consistency tuning previously set.- Parameters:
mutationState- theMutationStateinformation to work with.- Returns:
- this SearchQuery for chaining.
-
getLimit
public Integer getLimit()
- Returns:
- the value of the
limit(int)parameter, or null if it was not set.
-
getSkip
public Integer getSkip()
- Returns:
- the value of the
skip(int)parameter, or null if it was not set.
-
getHighlightStyle
public HighlightStyle getHighlightStyle()
- Returns:
- the value of the
highlight styleparameter, or null if it was not set.
-
getHighlightFields
public String[] getHighlightFields()
- Returns:
- the value of the
highlight fieldsparameter, or an empty array if it was not set.
-
getFields
public String[] getFields()
- Returns:
- the value of the
fields(String...)parameter, or an empty array if it was not set.
-
getFacets
public Map<String,SearchFacet> getFacets()
- Returns:
- the Map of
facets (by name), or an empty Map if it was not set.
-
getServerSideTimeout
public Long getServerSideTimeout()
- Returns:
- the value of the
serverSideTimeout(long, TimeUnit)parameter, or null if it was not set.
-
queryString
public static QueryStringQuery queryString(String query)
Prepare aQueryStringQuerybody.
-
match
public static MatchQuery match(String match)
Prepare aMatchQuerybody.
-
matchPhrase
public static MatchPhraseQuery matchPhrase(String matchPhrase)
Prepare aMatchPhraseQuerybody.
-
prefix
public static PrefixQuery prefix(String prefix)
Prepare aPrefixQuerybody.
-
regexp
public static RegexpQuery regexp(String regexp)
Prepare aRegexpQuerybody.
-
termRange
public static TermRangeQuery termRange()
Prepare aTermRangeQuerybody.
-
numericRange
public static NumericRangeQuery numericRange()
Prepare aNumericRangeQuerybody.
-
dateRange
public static DateRangeQuery dateRange()
Prepare aDateRangeQuerybody.
-
disjuncts
public static DisjunctionQuery disjuncts(AbstractFtsQuery... queries)
Prepare aDisjunctionQuerybody.
-
conjuncts
public static ConjunctionQuery conjuncts(AbstractFtsQuery... queries)
Prepare aConjunctionQuerybody.
-
booleans
public static BooleanQuery booleans()
Prepare aBooleanQuerybody.
-
wildcard
public static WildcardQuery wildcard(String wildcard)
Prepare aWildcardQuerybody.
-
docId
public static DocIdQuery docId(String... docIds)
Prepare aDocIdQuerybody.
-
booleanField
public static BooleanFieldQuery booleanField(boolean value)
Prepare aBooleanFieldQuerybody.
-
phrase
public static PhraseQuery phrase(String... terms)
Prepare aPhraseQuerybody.
-
matchAll
public static MatchAllQuery matchAll()
Prepare aMatchAllQuerybody.
-
matchNone
public static MatchNoneQuery matchNone()
Prepare aMatchNoneQuerybody.
-
geoBoundingBox
public static GeoBoundingBoxQuery geoBoundingBox(double topLeftLon, double topLeftLat, double bottomRightLon, double bottomRightLat)
Prepare aGeoBoundingBoxQuerybody.
-
geoDistance
public static GeoDistanceQuery geoDistance(double locationLon, double locationLat, String distance)
Prepare aGeoDistanceQuerybody.
-
geoPolygon
@Experimental public static GeoPolygonQuery geoPolygon(List<Coordinate> points)
Prepare aGeoPolygonQuerybody.
-
-