Package org.elasticsearch.index.query
Class QueryBuilders
- java.lang.Object
-
- org.elasticsearch.index.query.QueryBuilders
-
public final class QueryBuilders extends Object
Utility class to create search queries.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BoolQueryBuilderboolQuery()A Query that matches documents matching boolean combinations of other queries.static BoostingQueryBuilderboostingQuery(QueryBuilder positiveQuery, QueryBuilder negativeQuery)The BoostingQuery class can be used to effectively demote results that match a given query.static ConstantScoreQueryBuilderconstantScoreQuery(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 DisMaxQueryBuilderdisMaxQuery()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 DistanceFeatureQueryBuilderdistanceFeatureQuery(String name, DistanceFeatureQueryBuilder.Origin origin, String pivot)A query to boost scores based on their proximity to the given origin for date, date_nanos and geo_point field types.static ExistsQueryBuilderexistsQuery(String name)A filter to filter only documents where a field exists in them.static FieldMaskingSpanQueryBuilderfieldMaskingSpanQuery(SpanQueryBuilder query, String field)static FuzzyQueryBuilderfuzzyQuery(String name, Object value)A Query that matches documents using fuzzy query.static FuzzyQueryBuilderfuzzyQuery(String name, String value)A Query that matches documents using fuzzy query.static GeoShapeQueryBuildergeoDisjointQuery(String name, String indexedShapeId)static GeoShapeQueryBuildergeoIntersectionQuery(String name, String indexedShapeId)static GeoShapeQueryBuildergeoShapeQuery(String name, String indexedShapeId)static GeoShapeQueryBuildergeoWithinQuery(String name, String indexedShapeId)static IdsQueryBuilderidsQuery()Constructs a query that will match only specific ids within all types.static MatchAllQueryBuildermatchAllQuery()A query that matches on all documents.static MatchPhrasePrefixQueryBuildermatchPhrasePrefixQuery(String name, Object text)Creates a match query with type "PHRASE_PREFIX" for the provided field name and text.static MatchPhraseQueryBuildermatchPhraseQuery(String name, Object text)Creates a text query with type "PHRASE" for the provided field name and text.static MatchQueryBuildermatchQuery(String name, Object text)Creates a match query with type "BOOLEAN" for the provided field name and text.static MoreLikeThisQueryBuildermoreLikeThisQuery(String[] likeTexts)A more like this query that finds documents that are "like" the provided texts which is checked against the "_all" field.static MoreLikeThisQueryBuildermoreLikeThisQuery(String[] fields, String[] 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 MoreLikeThisQueryBuildermoreLikeThisQuery(String[] 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 MoreLikeThisQueryBuildermoreLikeThisQuery(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 MultiMatchQueryBuildermultiMatchQuery(Object text, String... fieldNames)Creates a match query with type "BOOLEAN" for the provided field name and text.static PrefixQueryBuilderprefixQuery(String name, String prefix)A Query that matches documents containing terms with a specified prefix.static QueryStringQueryBuilderqueryStringQuery(String queryString)A query that parses a query string and runs it.static RangeQueryBuilderrangeQuery(String name)A Query that matches documents within an range of terms.static RegexpQueryBuilderregexpQuery(String name, String regexp)A Query that matches documents containing terms with a specified regular expression.static SimpleQueryStringBuildersimpleQueryStringQuery(String queryString)A query that acts similar to a query_string query, but won't throw exceptions for any weird string syntax.static SpanContainingQueryBuilderspanContainingQuery(SpanQueryBuilder big, SpanQueryBuilder little)Creates a newspan_containingbuilder.static SpanFirstQueryBuilderspanFirstQuery(SpanQueryBuilder match, int end)static SpanMultiTermQueryBuilderspanMultiTermQueryBuilder(MultiTermQueryBuilder multiTermQueryBuilder)Creates aSpanQueryBuilderwhich allows having a sub query which implementsMultiTermQueryBuilder.static SpanNearQueryBuilderspanNearQuery(SpanQueryBuilder initialClause, int slop)static SpanNotQueryBuilderspanNotQuery(SpanQueryBuilder include, SpanQueryBuilder exclude)static SpanOrQueryBuilderspanOrQuery(SpanQueryBuilder initialClause)static SpanTermQueryBuilderspanTermQuery(String name, double value)static SpanTermQueryBuilderspanTermQuery(String name, float value)static SpanTermQueryBuilderspanTermQuery(String name, int value)static SpanTermQueryBuilderspanTermQuery(String name, long value)static SpanTermQueryBuilderspanTermQuery(String name, String value)static SpanWithinQueryBuilderspanWithinQuery(SpanQueryBuilder big, SpanQueryBuilder little)Creates a newspan_withinbuilder.static TermQueryBuildertermQuery(String name, boolean value)A Query that matches documents containing a term.static TermQueryBuildertermQuery(String name, double value)A Query that matches documents containing a term.static TermQueryBuildertermQuery(String name, float value)A Query that matches documents containing a term.static TermQueryBuildertermQuery(String name, int value)A Query that matches documents containing a term.static TermQueryBuildertermQuery(String name, long value)A Query that matches documents containing a term.static TermQueryBuildertermQuery(String name, Object value)A Query that matches documents containing a term.static TermQueryBuildertermQuery(String name, String value)A Query that matches documents containing a term.static TermsQueryBuildertermsLookupQuery(String name, TermsLookup termsLookup)A terms query that can extract the terms from another doc in an index.static TermsQueryBuildertermsQuery(String name, double... values)A filter for a field based on several terms matching on any of them.static TermsQueryBuildertermsQuery(String name, float... values)A filter for a field based on several terms matching on any of them.static TermsQueryBuildertermsQuery(String name, int... values)A filter for a field based on several terms matching on any of them.static TermsQueryBuildertermsQuery(String name, long... values)A filter for a field based on several terms matching on any of them.static TermsQueryBuildertermsQuery(String name, Object... values)A filter for a field based on several terms matching on any of them.static TermsQueryBuildertermsQuery(String name, String... values)A filter for a field based on several terms matching on any of them.static TermsQueryBuildertermsQuery(String name, Collection<?> values)A filter for a field based on several terms matching on any of them.static WildcardQueryBuilderwildcardQuery(String name, String query)Implements the wildcard search query.
-
-
-
Method Detail
-
matchAllQuery
public static MatchAllQueryBuilder matchAllQuery()
A query that matches on all documents.
-
matchQuery
public static MatchQueryBuilder matchQuery(String name, Object 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).
-
multiMatchQuery
public static MultiMatchQueryBuilder multiMatchQuery(Object text, String... 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).
-
matchPhraseQuery
public static MatchPhraseQueryBuilder matchPhraseQuery(String name, Object 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 MatchPhrasePrefixQueryBuilder matchPhrasePrefixQuery(String name, Object 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
public static DisMaxQueryBuilder 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.
-
distanceFeatureQuery
public static DistanceFeatureQueryBuilder distanceFeatureQuery(String name, DistanceFeatureQueryBuilder.Origin origin, String 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
public static IdsQueryBuilder idsQuery()
Constructs a query that will match only specific ids within all types.
-
termQuery
public static TermQueryBuilder termQuery(String name, String value)
A Query that matches documents containing a term.- Parameters:
name- The name of the fieldvalue- The value of the term
-
termQuery
public static TermQueryBuilder termQuery(String 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 TermQueryBuilder termQuery(String 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 TermQueryBuilder termQuery(String 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 TermQueryBuilder termQuery(String 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 TermQueryBuilder termQuery(String 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 TermQueryBuilder termQuery(String name, Object value)
A Query that matches documents containing a term.- Parameters:
name- The name of the fieldvalue- The value of the term
-
fuzzyQuery
public static FuzzyQueryBuilder fuzzyQuery(String name, String 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 FuzzyQueryBuilder fuzzyQuery(String name, Object 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 PrefixQueryBuilder prefixQuery(String name, String 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 RangeQueryBuilder rangeQuery(String name)
A Query that matches documents within an range of terms.- Parameters:
name- The field name
-
wildcardQuery
public static WildcardQueryBuilder wildcardQuery(String name, String 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?.- Parameters:
name- The field namequery- The wildcard query string
-
regexpQuery
public static RegexpQueryBuilder regexpQuery(String name, String 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 QueryStringQueryBuilder queryStringQuery(String 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 SimpleQueryStringBuilder simpleQueryStringQuery(String queryString)
A query that acts similar to a query_string query, but won't throw exceptions for any weird string syntax. SeeSimpleQueryParserfor 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
public static BoolQueryBuilder boolQuery()
A Query that matches documents matching boolean combinations of other queries.
-
spanTermQuery
public static SpanTermQueryBuilder spanTermQuery(String name, String value)
-
spanTermQuery
public static SpanTermQueryBuilder spanTermQuery(String name, int value)
-
spanTermQuery
public static SpanTermQueryBuilder spanTermQuery(String name, long value)
-
spanTermQuery
public static SpanTermQueryBuilder spanTermQuery(String name, float value)
-
spanTermQuery
public static SpanTermQueryBuilder spanTermQuery(String name, double value)
-
spanFirstQuery
public static SpanFirstQueryBuilder spanFirstQuery(SpanQueryBuilder match, int end)
-
spanNearQuery
public static SpanNearQueryBuilder spanNearQuery(SpanQueryBuilder initialClause, int slop)
-
spanNotQuery
public static SpanNotQueryBuilder spanNotQuery(SpanQueryBuilder include, SpanQueryBuilder exclude)
-
spanOrQuery
public static SpanOrQueryBuilder spanOrQuery(SpanQueryBuilder initialClause)
-
spanWithinQuery
public static SpanWithinQueryBuilder spanWithinQuery(SpanQueryBuilder big, SpanQueryBuilder little)
Creates a newspan_withinbuilder.- Parameters:
big- the big clause, it must encloselittlefor a match.little- the little clause, it must be contained withinbigfor a match.
-
spanContainingQuery
public static SpanContainingQueryBuilder spanContainingQuery(SpanQueryBuilder big, SpanQueryBuilder little)
Creates a newspan_containingbuilder.- Parameters:
big- the big clause, it must encloselittlefor a match.little- the little clause, it must be contained withinbigfor a match.
-
spanMultiTermQueryBuilder
public static SpanMultiTermQueryBuilder spanMultiTermQueryBuilder(MultiTermQueryBuilder multiTermQueryBuilder)
Creates aSpanQueryBuilderwhich allows having a sub query which implementsMultiTermQueryBuilder. This is useful for having e.g. wildcard or fuzzy queries inside spans.- Parameters:
multiTermQueryBuilder- TheMultiTermQueryBuilderthat backs the created builder.
-
fieldMaskingSpanQuery
public static FieldMaskingSpanQueryBuilder fieldMaskingSpanQuery(SpanQueryBuilder query, String field)
-
constantScoreQuery
public 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.- Parameters:
queryBuilder- The query to wrap in a constant score query
-
moreLikeThisQuery
public static MoreLikeThisQueryBuilder moreLikeThisQuery(String[] fields, String[] 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 MoreLikeThisQueryBuilder moreLikeThisQuery(String[] 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 MoreLikeThisQueryBuilder moreLikeThisQuery(String[] 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.
-
termsQuery
public static TermsQueryBuilder termsQuery(String name, String... 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 TermsQueryBuilder termsQuery(String 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 TermsQueryBuilder termsQuery(String 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 TermsQueryBuilder termsQuery(String 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 TermsQueryBuilder termsQuery(String 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 TermsQueryBuilder termsQuery(String name, Object... 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 TermsQueryBuilder termsQuery(String name, Collection<?> values)
A filter for a field based on several terms matching on any of them.- Parameters:
name- The field namevalues- The terms
-
termsLookupQuery
public static TermsQueryBuilder termsLookupQuery(String name, TermsLookup termsLookup)
A terms query that can extract the terms from another doc in an index.
-
geoShapeQuery
public static GeoShapeQueryBuilder geoShapeQuery(String name, String indexedShapeId)
-
geoIntersectionQuery
public static GeoShapeQueryBuilder geoIntersectionQuery(String name, String indexedShapeId)
-
geoWithinQuery
public static GeoShapeQueryBuilder geoWithinQuery(String name, String indexedShapeId)
-
geoDisjointQuery
public static GeoShapeQueryBuilder geoDisjointQuery(String name, String indexedShapeId)
-
existsQuery
public static ExistsQueryBuilder existsQuery(String name)
A filter to filter only documents where a field exists in them.- Parameters:
name- The name of the field
-
-