Package org.elasticsearch.index.query
Class QueryBuilders
org.elasticsearch.index.query.QueryBuilders
public final class QueryBuilders
extends
Utility class to create search queries.
-
Method Summary
Modifier and TypeMethodDescriptionstatic BoolQueryBuilder
A Query that matches documents matching boolean combinations of other queries.static BoostingQueryBuilder
boostingQuery(QueryBuilder positiveQuery, QueryBuilder negativeQuery)
The BoostingQuery class can be used to effectively demote results that match a given query.static CombinedFieldsQueryBuilder
text, ... fieldNames)
Creates a combined fields query for the provided field names and text.static CommonTermsQueryBuilder
fieldName, text)
Deprecated.static ConstantScoreQueryBuilder
constantScoreQuery(QueryBuilder queryBuilder)
A query that wraps another query and simply returns a constant score equal to the query boost for every document in the query.static DisMaxQueryBuilder
A query that generates the union of documents produced by its sub-queries, and that scores each document with the maximum score for that document as produced by any sub-query, plus a tie breaking increment for any additional matching sub-queries.static DistanceFeatureQueryBuilder
name, DistanceFeatureQueryBuilder.Origin origin, pivot)
A query to boost scores based on their proximity to the given origin for date, date_nanos and geo_point field types.static ExistsQueryBuilder
name)
A filter to filter only documents where a field exists in them.static FieldMaskingSpanQueryBuilder
fieldMaskingSpanQuery(SpanQueryBuilder query, field)
static FunctionScoreQueryBuilder
functionScoreQuery(FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders)
A query that allows to define a custom scoring functionstatic FunctionScoreQueryBuilder
functionScoreQuery(ScoreFunctionBuilder function)
A query that allows to define a custom scoring function.static FunctionScoreQueryBuilder
functionScoreQuery(QueryBuilder queryBuilder)
A function_score query with no functions.static FunctionScoreQueryBuilder
functionScoreQuery(QueryBuilder queryBuilder, FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders)
A query that allows to define a custom scoring functionstatic FunctionScoreQueryBuilder
functionScoreQuery(QueryBuilder queryBuilder, ScoreFunctionBuilder function)
A query that allows to define a custom scoring function.static FuzzyQueryBuilder
name, value)
A Query that matches documents using fuzzy query.static FuzzyQueryBuilder
name, value)
A Query that matches documents using fuzzy query.static GeoBoundingBoxQueryBuilder
name)
A filter to filter based on a bounding box defined by top left and bottom right locations / pointsstatic GeoShapeQueryBuilder
name, indexedShapeId)
static GeoShapeQueryBuilder
name, indexedShapeId, indexedShapeType)
Deprecated.Types are in the process of being removed, usegeoDisjointQuery(String, String)
instead.static GeoShapeQueryBuilder
name, ShapeBuilder shape)
Deprecated.usegeoDisjointQuery(String, Geometry)
insteadstatic GeoShapeQueryBuilder
name, org.elasticsearch.geometry.Geometry shape)
A filter to filter indexed shapes that are not intersection with the query shapestatic GeoDistanceQueryBuilder
name)
A filter to filter based on a specific distance from a specific geo location / point.static GeoShapeQueryBuilder
name, indexedShapeId)
static GeoShapeQueryBuilder
name, indexedShapeId, indexedShapeType)
Deprecated.Types are in the process of being removed, usegeoIntersectionQuery(String, String)
instead.static GeoShapeQueryBuilder
name, ShapeBuilder shape)
Deprecated.usegeoIntersectionQuery(String, Geometry)
insteadstatic GeoShapeQueryBuilder
name, org.elasticsearch.geometry.Geometry shape)
A filter to filter indexed shapes intersecting with shapesstatic GeoPolygonQueryBuilder
name, <GeoPoint> points)
Deprecated.usegeoIntersectionQuery(String, Geometry)
insteadstatic GeoShapeQueryBuilder
name, indexedShapeId)
static GeoShapeQueryBuilder
name, indexedShapeId, indexedShapeType)
Deprecated.Types are in the process of being removed, usegeoShapeQuery(String, String)
instead.static GeoShapeQueryBuilder
name, ShapeBuilder shape)
Deprecated.usegeoShapeQuery(String, Geometry)
insteadstatic GeoShapeQueryBuilder
name, org.elasticsearch.geometry.Geometry shape)
A filter based on the relationship of a shape and indexed shapesstatic GeoShapeQueryBuilder
name, indexedShapeId)
static GeoShapeQueryBuilder
name, indexedShapeId, indexedShapeType)
Deprecated.Types are in the process of being removed, usegeoWithinQuery(String, String)
instead.static GeoShapeQueryBuilder
name, ShapeBuilder shape)
Deprecated.usegeoWithinQuery(String, Geometry)
insteadstatic GeoShapeQueryBuilder
name, org.elasticsearch.geometry.Geometry shape)
A filter to filter indexed shapes that are contained by a shapestatic IdsQueryBuilder
idsQuery()
Constructs a query that will match only specific ids within all types.static IdsQueryBuilder
... types)
Deprecated.Types are in the process of being removed, useidsQuery()
instead.static MatchAllQueryBuilder
A query that matches on all documents.static MatchBoolPrefixQueryBuilder
name, text)
Creates a text query with type "BOOL_PREFIX" for the provided field name and text.name, text)
Creates a match query with type "PHRASE_PREFIX" for the provided field name and text.static MatchPhraseQueryBuilder
name, text)
Creates a text query with type "PHRASE" for the provided field name and text.static MatchQueryBuilder
name, text)
Creates a match query with type "BOOLEAN" for the provided field name and text.static MoreLikeThisQueryBuilder
[] likeTexts)
A more like this query that finds documents that are "like" the provided texts which is checked against the "_all" field.static MoreLikeThisQueryBuilder
[] fields, [] likeTexts, MoreLikeThisQueryBuilder.Item[] likeItems)
A more like this query that finds documents that are "like" the provided texts or documents which is checked against the fields the query is constructed with.static MoreLikeThisQueryBuilder
[] likeTexts, MoreLikeThisQueryBuilder.Item[] likeItems)
A more like this query that finds documents that are "like" the provided texts or documents which is checked against the "_all" field.static MoreLikeThisQueryBuilder
moreLikeThisQuery(MoreLikeThisQueryBuilder.Item[] likeItems)
A more like this query that finds documents that are "like" the provided documents which is checked against the "_all" field.static MultiMatchQueryBuilder
text, ... fieldNames)
Creates a match query with type "BOOLEAN" for the provided field name and text.static NestedQueryBuilder
path, QueryBuilder query, org.apache.lucene.search.join.ScoreMode scoreMode)
static PrefixQueryBuilder
name, prefix)
A Query that matches documents containing terms with a specified prefix.static QueryStringQueryBuilder
queryString)
A query that parses a query string and runs it.static RangeQueryBuilder
name)
A Query that matches documents within an range of terms.static RegexpQueryBuilder
name, regexp)
A Query that matches documents containing terms with a specified regular expression.static ScriptQueryBuilder
scriptQuery(Script script)
A builder for filter based on a script.static ScriptScoreQueryBuilder
scriptScoreQuery(QueryBuilder queryBuilder, Script script)
A query that allows to define a custom scoring function through script.static SimpleQueryStringBuilder
queryString)
A query that acts similar to a query_string query, but won't throw exceptions for any weird string syntax.static SpanContainingQueryBuilder
spanContainingQuery(SpanQueryBuilder big, SpanQueryBuilder little)
Creates a newspan_containing
builder.static SpanFirstQueryBuilder
spanFirstQuery(SpanQueryBuilder match, int end)
static SpanMultiTermQueryBuilder
spanMultiTermQueryBuilder(MultiTermQueryBuilder multiTermQueryBuilder)
Creates aSpanQueryBuilder
which allows having a sub query which implementsMultiTermQueryBuilder
.static SpanNearQueryBuilder
spanNearQuery(SpanQueryBuilder initialClause, int slop)
static SpanNotQueryBuilder
spanNotQuery(SpanQueryBuilder include, SpanQueryBuilder exclude)
static SpanOrQueryBuilder
spanOrQuery(SpanQueryBuilder initialClause)
static SpanTermQueryBuilder
name, double value)
static SpanTermQueryBuilder
name, float value)
static SpanTermQueryBuilder
name, int value)
static SpanTermQueryBuilder
name, long value)
static SpanTermQueryBuilder
name, value)
static SpanWithinQueryBuilder
spanWithinQuery(SpanQueryBuilder big, SpanQueryBuilder little)
Creates a newspan_within
builder.static TermQueryBuilder
name, boolean value)
A Query that matches documents containing a term.static TermQueryBuilder
name, double value)
A Query that matches documents containing a term.static TermQueryBuilder
name, float value)
A Query that matches documents containing a term.static TermQueryBuilder
name, int value)
A Query that matches documents containing a term.static TermQueryBuilder
name, long value)
A Query that matches documents containing a term.static TermQueryBuilder
name, value)
A Query that matches documents containing a term.static TermQueryBuilder
name, value)
A Query that matches documents containing a term.static TermsQueryBuilder
name, TermsLookup termsLookup)
A terms query that can extract the terms from another doc in an index.static TermsQueryBuilder
name, double... values)
A filter for a field based on several terms matching on any of them.static TermsQueryBuilder
name, float... values)
A filter for a field based on several terms matching on any of them.static TermsQueryBuilder
name, int... values)
A filter for a field based on several terms matching on any of them.static TermsQueryBuilder
name, long... values)
A filter for a field based on several terms matching on any of them.static TermsQueryBuilder
name, ... values)
A filter for a field based on several terms matching on any of them.static TermsQueryBuilder
name, ... values)
A filter for a field based on several terms matching on any of them.static TermsQueryBuilder
name, <?> values)
A filter for a field based on several terms matching on any of them.static TypeQueryBuilder
type)
Deprecated.Types are going away, prefer filtering on a field.static WildcardQueryBuilder
name, query)
Implements the wildcard search query.static WrapperQueryBuilder
wrapperQuery(byte[] source)
A Query builder which allows building a query thanks to a JSON string or binary data.static WrapperQueryBuilder
source)
A Query builder which allows building a query thanks to a JSON string or binary data.static WrapperQueryBuilder
wrapperQuery(BytesReference source)
A Query builder which allows building a query thanks to a JSON string or binary data.Methods inherited from class java.lang.
, , , , , , , , , ,
-
Method Details
-
matchAllQuery
A query that matches on all documents. -
matchQuery
public static name, text)Creates a match query with type "BOOLEAN" for the provided field name and text.- Parameters:
name
- The field name.text
- The query text (to be analyzed).
-
commonTermsQuery
public static fieldName, text)Deprecated.Creates a common query for the provided field name and text.- Parameters:
fieldName
- The field name.text
- The query text (to be analyzed).
-
multiMatchQuery
public static text, ... fieldNames)Creates a match query with type "BOOLEAN" for the provided field name and text.- Parameters:
fieldNames
- The field names.text
- The query text (to be analyzed).
-
matchBoolPrefixQuery
public static name, text)Creates a text query with type "BOOL_PREFIX" for the provided field name and text.- Parameters:
name
- The field name.text
- The query text (to be analyzed).
-
matchPhraseQuery
public static name, text)Creates a text query with type "PHRASE" for the provided field name and text.- Parameters:
name
- The field name.text
- The query text (to be analyzed).
-
matchPhrasePrefixQuery
public static name, text)Creates a match query with type "PHRASE_PREFIX" for the provided field name and text.- Parameters:
name
- The field name.text
- The query text (to be analyzed).
-
disMaxQuery
A query that generates the union of documents produced by its sub-queries, and that scores each document with the maximum score for that document as produced by any sub-query, plus a tie breaking increment for any additional matching sub-queries. -
combinedFieldsQuery
public static text, ... fieldNames)Creates a combined fields query for the provided field names and text.- Parameters:
text
- The query text (to be analyzed).fieldNames
- The field names.
-
distanceFeatureQuery
public static name, DistanceFeatureQueryBuilder.Origin origin, pivot)A query to boost scores based on their proximity to the given origin for date, date_nanos and geo_point field types.- Parameters:
name
- The field nameorigin
- The origin of the distance calculation. Can be a long, string orGeoPoint
, depending on field type.pivot
- The distance from the origin at which relevance scores receive half of the boost value.
-
idsQuery
Constructs a query that will match only specific ids within all types. -
idsQuery
public static ... types)Deprecated.Types are in the process of being removed, useidsQuery()
instead.Constructs a query that will match only specific ids within types.- Parameters:
types
- The mapping/doc type
-
termQuery
public static name, value)A Query that matches documents containing a term.- Parameters:
name
- The name of the fieldvalue
- The value of the term
-
termQuery
public static name, int value)A Query that matches documents containing a term.- Parameters:
name
- The name of the fieldvalue
- The value of the term
-
termQuery
public static name, long value)A Query that matches documents containing a term.- Parameters:
name
- The name of the fieldvalue
- The value of the term
-
termQuery
public static name, float value)A Query that matches documents containing a term.- Parameters:
name
- The name of the fieldvalue
- The value of the term
-
termQuery
public static name, double value)A Query that matches documents containing a term.- Parameters:
name
- The name of the fieldvalue
- The value of the term
-
termQuery
public static name, boolean value)A Query that matches documents containing a term.- Parameters:
name
- The name of the fieldvalue
- The value of the term
-
termQuery
public static name, value)A Query that matches documents containing a term.- Parameters:
name
- The name of the fieldvalue
- The value of the term
-
fuzzyQuery
public static name, value)A Query that matches documents using fuzzy query.- Parameters:
name
- The name of the fieldvalue
- The value of the term- See Also:
matchQuery(String, Object)
,rangeQuery(String)
-
fuzzyQuery
public static name, value)A Query that matches documents using fuzzy query.- Parameters:
name
- The name of the fieldvalue
- The value of the term- See Also:
matchQuery(String, Object)
,rangeQuery(String)
-
prefixQuery
public static name, prefix)A Query that matches documents containing terms with a specified prefix.- Parameters:
name
- The name of the fieldprefix
- The prefix query
-
rangeQuery
public static name)A Query that matches documents within an range of terms.- Parameters:
name
- The field name
-
wildcardQuery
public static name, query)Implements the wildcard search query. Supported wildcards are*
, which matches any character sequence (including the empty one), and?
, which matches any single character. Note this query can be slow, as it needs to iterate over many terms. In order to prevent extremely slow WildcardQueries, a Wildcard term should not start with one of the wildcards*
or?
. (The wildcard field type however, is optimised for leading wildcards)- Parameters:
name
- The field namequery
- The wildcard query string
-
regexpQuery
public static name, regexp)A Query that matches documents containing terms with a specified regular expression.- Parameters:
name
- The name of the fieldregexp
- The regular expression
-
queryStringQuery
public static queryString)A query that parses a query string and runs it. There are two modes that this operates. The first, when no field is added (usingQueryStringQueryBuilder.field(String)
, will run the query once and non prefixed fields will use theQueryStringQueryBuilder.defaultField(String)
set. The second, when one or more fields are added (usingQueryStringQueryBuilder.field(String)
), will run the parsed query against the provided fields, and combine them either using Dismax.- Parameters:
queryString
- The query string to run
-
simpleQueryStringQuery
public static queryString)A query that acts similar to a query_string query, but won't throw exceptions for any weird string syntax. SeeSimpleQueryParser
for the full supported syntax. -
boostingQuery
public static BoostingQueryBuilder boostingQuery(QueryBuilder positiveQuery, QueryBuilder negativeQuery)The BoostingQuery class can be used to effectively demote results that match a given query. Unlike the "NOT" clause, this still selects documents that contain undesirable terms, but reduces their overall score: -
boolQuery
A Query that matches documents matching boolean combinations of other queries. -
spanTermQuery
public static name, value) -
spanTermQuery
public static name, int value) -
spanTermQuery
public static name, long value) -
spanTermQuery
public static name, float value) -
spanTermQuery
public static name, double value) -
spanFirstQuery
-
spanNearQuery
-
spanNotQuery
-
spanOrQuery
-
spanWithinQuery
public static SpanWithinQueryBuilder spanWithinQuery(SpanQueryBuilder big, SpanQueryBuilder little)Creates a newspan_within
builder.- Parameters:
big
- the big clause, it must encloselittle
for a match.little
- the little clause, it must be contained withinbig
for a match.
-
spanContainingQuery
public static SpanContainingQueryBuilder spanContainingQuery(SpanQueryBuilder big, SpanQueryBuilder little)Creates a newspan_containing
builder.- Parameters:
big
- the big clause, it must encloselittle
for a match.little
- the little clause, it must be contained withinbig
for a match.
-
spanMultiTermQueryBuilder
public static SpanMultiTermQueryBuilder spanMultiTermQueryBuilder(MultiTermQueryBuilder multiTermQueryBuilder)Creates aSpanQueryBuilder
which allows having a sub query which implementsMultiTermQueryBuilder
. This is useful for having e.g. wildcard or fuzzy queries inside spans.- Parameters:
multiTermQueryBuilder
- TheMultiTermQueryBuilder
that backs the created builder.
-
fieldMaskingSpanQuery
-
constantScoreQuery
A query that wraps another query and simply returns a constant score equal to the query boost for every document in the query.- Parameters:
queryBuilder
- The query to wrap in a constant score query
-
functionScoreQuery
A function_score query with no functions.- Parameters:
queryBuilder
- The query to custom score- Returns:
- the function score query
-
functionScoreQuery
public static FunctionScoreQueryBuilder functionScoreQuery(QueryBuilder queryBuilder, FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders)A query that allows to define a custom scoring function- Parameters:
queryBuilder
- The query to custom scorefilterFunctionBuilders
- the filters and functions to execute- Returns:
- the function score query
-
functionScoreQuery
public static FunctionScoreQueryBuilder functionScoreQuery(FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders)A query that allows to define a custom scoring function- Parameters:
filterFunctionBuilders
- the filters and functions to execute- Returns:
- the function score query
-
functionScoreQuery
A query that allows to define a custom scoring function.- Parameters:
function
- The function builder used to custom score
-
functionScoreQuery
public static FunctionScoreQueryBuilder functionScoreQuery(QueryBuilder queryBuilder, ScoreFunctionBuilder function)A query that allows to define a custom scoring function.- Parameters:
queryBuilder
- The query to custom scorefunction
- The function builder used to custom score
-
scriptScoreQuery
A query that allows to define a custom scoring function through script.- Parameters:
queryBuilder
- The query to custom scorescript
- The script used to score the query
-
moreLikeThisQuery
public static [] fields, [] likeTexts, MoreLikeThisQueryBuilder.Item[] likeItems)A more like this query that finds documents that are "like" the provided texts or documents which is checked against the fields the query is constructed with.- Parameters:
fields
- the field names that will be used when generating the 'More Like This' query.likeTexts
- the text to use when generating the 'More Like This' query.likeItems
- the documents to use when generating the 'More Like This' query.
-
moreLikeThisQuery
public static [] likeTexts, MoreLikeThisQueryBuilder.Item[] likeItems)A more like this query that finds documents that are "like" the provided texts or documents which is checked against the "_all" field.- Parameters:
likeTexts
- the text to use when generating the 'More Like This' query.likeItems
- the documents to use when generating the 'More Like This' query.
-
moreLikeThisQuery
public static [] likeTexts)A more like this query that finds documents that are "like" the provided texts which is checked against the "_all" field.- Parameters:
likeTexts
- the text to use when generating the 'More Like This' query.
-
moreLikeThisQuery
public static MoreLikeThisQueryBuilder moreLikeThisQuery(MoreLikeThisQueryBuilder.Item[] likeItems)A more like this query that finds documents that are "like" the provided documents which is checked against the "_all" field.- Parameters:
likeItems
- the documents to use when generating the 'More Like This' query.
-
nestedQuery
public static path, QueryBuilder query, org.apache.lucene.search.join.ScoreMode scoreMode) -
termsQuery
public static name, ... values)A filter for a field based on several terms matching on any of them.- Parameters:
name
- The field namevalues
- The terms
-
termsQuery
public static name, int... values)A filter for a field based on several terms matching on any of them.- Parameters:
name
- The field namevalues
- The terms
-
termsQuery
public static name, long... values)A filter for a field based on several terms matching on any of them.- Parameters:
name
- The field namevalues
- The terms
-
termsQuery
public static name, float... values)A filter for a field based on several terms matching on any of them.- Parameters:
name
- The field namevalues
- The terms
-
termsQuery
public static name, double... values)A filter for a field based on several terms matching on any of them.- Parameters:
name
- The field namevalues
- The terms
-
termsQuery
public static name, ... values)A filter for a field based on several terms matching on any of them.- Parameters:
name
- The field namevalues
- The terms
-
termsQuery
public static name, <?> values)A filter for a field based on several terms matching on any of them.- Parameters:
name
- The field namevalues
- The terms
-
wrapperQuery
public static source)A Query builder which allows building a query thanks to a JSON string or binary data. -
wrapperQuery
A Query builder which allows building a query thanks to a JSON string or binary data. -
wrapperQuery
A Query builder which allows building a query thanks to a JSON string or binary data. -
typeQuery
public static type)Deprecated.Types are going away, prefer filtering on a field.A filter based on doc/mapping type. -
termsLookupQuery
public static name, TermsLookup termsLookup)A terms query that can extract the terms from another doc in an index. -
scriptQuery
A builder for filter based on a script.- Parameters:
script
- The script to filter by.
-
geoDistanceQuery
public static name)A filter to filter based on a specific distance from a specific geo location / point.- Parameters:
name
- The location field name.
-
geoBoundingBoxQuery
public static name)A filter to filter based on a bounding box defined by top left and bottom right locations / points- Parameters:
name
- The location field name.
-
geoPolygonQuery
Deprecated.usegeoIntersectionQuery(String, Geometry)
insteadA filter to filter based on a polygon defined by a set of locations / points.- Parameters:
name
- The location field name.
-
geoShapeQuery
public static name, org.elasticsearch.geometry.Geometry shape) throwsA filter based on the relationship of a shape and indexed shapes- Parameters:
name
- The shape field nameshape
- Shape to use in the filter- Throws:
-
geoShapeQuery
Deprecated.usegeoShapeQuery(String, Geometry)
instead- Throws:
-
geoShapeQuery
public static name, indexedShapeId) -
geoShapeQuery
public static name, indexedShapeId, indexedShapeType)Deprecated.Types are in the process of being removed, usegeoShapeQuery(String, String)
instead. -
geoIntersectionQuery
public static name, org.elasticsearch.geometry.Geometry shape) throwsA filter to filter indexed shapes intersecting with shapes- Parameters:
name
- The shape field nameshape
- Shape to use in the filter- Throws:
-
geoIntersectionQuery
Deprecated.usegeoIntersectionQuery(String, Geometry)
instead- Throws:
-
geoIntersectionQuery
public static name, indexedShapeId) -
geoIntersectionQuery
public static name, indexedShapeId, indexedShapeType)Deprecated.Types are in the process of being removed, usegeoIntersectionQuery(String, String)
instead. -
geoWithinQuery
public static name, org.elasticsearch.geometry.Geometry shape) throwsA filter to filter indexed shapes that are contained by a shape- Parameters:
name
- The shape field nameshape
- Shape to use in the filter- Throws:
-
geoWithinQuery
Deprecated.usegeoWithinQuery(String, Geometry)
instead- Throws:
-
geoWithinQuery
public static name, indexedShapeId) -
geoWithinQuery
public static name, indexedShapeId, indexedShapeType)Deprecated.Types are in the process of being removed, usegeoWithinQuery(String, String)
instead. -
geoDisjointQuery
public static name, org.elasticsearch.geometry.Geometry shape) throwsA filter to filter indexed shapes that are not intersection with the query shape- Parameters:
name
- The shape field nameshape
- Shape to use in the filter- Throws:
-
geoDisjointQuery
Deprecated.usegeoDisjointQuery(String, Geometry)
instead- Throws:
-
geoDisjointQuery
public static name, indexedShapeId) -
geoDisjointQuery
public static name, indexedShapeId, indexedShapeType)Deprecated.Types are in the process of being removed, usegeoDisjointQuery(String, String)
instead. -
existsQuery
public static name)A filter to filter only documents where a field exists in them.- Parameters:
name
- The name of the field
-
CommonTermsQueryBuilder