Package org.elasticsearch.index.query
Class BoolQueryBuilder
- java.lang.Object
-
- org.elasticsearch.index.query.AbstractQueryBuilder<BoolQueryBuilder>
-
- org.elasticsearch.index.query.BoolQueryBuilder
-
- All Implemented Interfaces:
NamedWriteable
,Writeable
,ToXContent
,ToXContentObject
,QueryBuilder
,Rewriteable<QueryBuilder>
public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder>
A Query that matches documents matching boolean combinations of other queries.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.elasticsearch.common.xcontent.ToXContent
ToXContent.DelegatingMapParams, ToXContent.MapParams, ToXContent.Params
-
Nested classes/interfaces inherited from interface org.elasticsearch.common.io.stream.Writeable
Writeable.Reader<V>, Writeable.Writer<V>
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
ADJUST_PURE_NEGATIVE_DEFAULT
static java.lang.String
NAME
-
Fields inherited from class org.elasticsearch.index.query.AbstractQueryBuilder
boost, BOOST_FIELD, DEFAULT_BOOST, NAME_FIELD, queryName
-
Fields inherited from interface org.elasticsearch.index.query.Rewriteable
MAX_REWRITE_ROUNDS
-
Fields inherited from interface org.elasticsearch.common.xcontent.ToXContent
EMPTY_PARAMS
-
-
Constructor Summary
Constructors Constructor Description BoolQueryBuilder()
Build an empty bool query.BoolQueryBuilder(StreamInput in)
Read from a stream.
-
Method Summary
Modifier and Type Method Description boolean
adjustPureNegative()
BoolQueryBuilder
adjustPureNegative(boolean adjustPureNegative)
If a boolean query contains only negative ("must not") clauses should the BooleanQuery be enhanced with aMatchAllDocsQuery
in order to act as a pure exclude.protected boolean
doEquals(BoolQueryBuilder other)
Indicates whether some otherQueryBuilder
object of the same type is "equal to" this one.protected int
doHashCode()
protected QueryBuilder
doRewrite(QueryRewriteContext queryRewriteContext)
protected org.apache.lucene.search.Query
doToQuery(QueryShardContext context)
protected void
doWriteTo(StreamOutput out)
protected void
doXContent(XContentBuilder builder, ToXContent.Params params)
protected void
extractInnerHitBuilders(java.util.Map<java.lang.String,InnerHitContextBuilder> innerHits)
For internal usage only! Extracts the inner hits from the query tree.java.util.List<QueryBuilder>
filter()
Gets the queries that must appear in the matching documents but don't contribute to scoringBoolQueryBuilder
filter(QueryBuilder queryBuilder)
Adds a query that must appear in the matching documents but will not contribute to scoring.static BoolQueryBuilder
fromXContent(XContentParser parser)
java.lang.String
getWriteableName()
Returns the name of the writeable objectboolean
hasClauses()
Returnstrue
iff this query builder has at least one should, must, must not or filter clause.java.lang.String
minimumShouldMatch()
BoolQueryBuilder
minimumShouldMatch(int minimumShouldMatch)
Specifies a minimum number of the optional (should) boolean clauses which must be satisfied.BoolQueryBuilder
minimumShouldMatch(java.lang.String minimumShouldMatch)
Sets the minimum should match parameter using the special syntax (for example, supporting percentage).java.util.List<QueryBuilder>
must()
Gets the queries that must appear in the matching documents.BoolQueryBuilder
must(QueryBuilder queryBuilder)
Adds a query that must appear in the matching documents and will contribute to scoring.java.util.List<QueryBuilder>
mustNot()
Gets the queries that must not appear in the matching documents.BoolQueryBuilder
mustNot(QueryBuilder queryBuilder)
Adds a query that must not appear in the matching documents.java.util.List<QueryBuilder>
should()
Gets the list of clauses that should be matched by the returned documents.BoolQueryBuilder
should(QueryBuilder queryBuilder)
Adds a clause that should be matched by the returned documents.-
Methods inherited from class org.elasticsearch.index.query.AbstractQueryBuilder
addValidationError, boost, boost, checkNegativeBoost, declareStandardFields, equals, getName, hashCode, parseInnerQueryBuilder, printBoostAndQueryName, queryName, queryName, requireValue, rewrite, throwParsingExceptionOnMultipleFields, toFilter, toQuery, toString, toString, toXContent, writeTo
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.elasticsearch.common.xcontent.ToXContentObject
isFragment
-
-
-
-
Field Detail
-
NAME
public static final java.lang.String NAME
- See Also:
- Constant Field Values
-
ADJUST_PURE_NEGATIVE_DEFAULT
public static final boolean ADJUST_PURE_NEGATIVE_DEFAULT
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BoolQueryBuilder
public BoolQueryBuilder()
Build an empty bool query.
-
BoolQueryBuilder
public BoolQueryBuilder(StreamInput in) throws java.io.IOException
Read from a stream.- Throws:
java.io.IOException
-
-
Method Detail
-
doWriteTo
protected void doWriteTo(StreamOutput out) throws java.io.IOException
- Specified by:
doWriteTo
in classAbstractQueryBuilder<BoolQueryBuilder>
- Throws:
java.io.IOException
-
must
public BoolQueryBuilder must(QueryBuilder queryBuilder)
Adds a query that must appear in the matching documents and will contribute to scoring. Nonull
value allowed.
-
must
public java.util.List<QueryBuilder> must()
Gets the queries that must appear in the matching documents.
-
filter
public BoolQueryBuilder filter(QueryBuilder queryBuilder)
Adds a query that must appear in the matching documents but will not contribute to scoring. Nonull
value allowed.
-
filter
public java.util.List<QueryBuilder> filter()
Gets the queries that must appear in the matching documents but don't contribute to scoring
-
mustNot
public BoolQueryBuilder mustNot(QueryBuilder queryBuilder)
Adds a query that must not appear in the matching documents. Nonull
value allowed.
-
mustNot
public java.util.List<QueryBuilder> mustNot()
Gets the queries that must not appear in the matching documents.
-
should
public BoolQueryBuilder should(QueryBuilder queryBuilder)
Adds a clause that should be matched by the returned documents. For a boolean query with noMUST
clauses one or moreSHOULD
clauses must match a document for the BooleanQuery to match. Nonull
value allowed.- See Also:
minimumShouldMatch(int)
-
should
public java.util.List<QueryBuilder> should()
Gets the list of clauses that should be matched by the returned documents.- See Also:
should(QueryBuilder)
,minimumShouldMatch(int)
-
minimumShouldMatch
public java.lang.String minimumShouldMatch()
- Returns:
- the string representation of the minimumShouldMatch settings for this query
-
minimumShouldMatch
public BoolQueryBuilder minimumShouldMatch(java.lang.String minimumShouldMatch)
Sets the minimum should match parameter using the special syntax (for example, supporting percentage).- See Also:
minimumShouldMatch(int)
-
minimumShouldMatch
public BoolQueryBuilder minimumShouldMatch(int minimumShouldMatch)
Specifies a minimum number of the optional (should) boolean clauses which must be satisfied.By default no optional clauses are necessary for a match (unless there are no required clauses). If this method is used, then the specified number of clauses is required.
Use of this method is totally independent of specifying that any specific clauses are required (or prohibited). This number will only be compared against the number of matching optional clauses.
- Parameters:
minimumShouldMatch
- the number of optional clauses that must match
-
hasClauses
public boolean hasClauses()
Returnstrue
iff this query builder has at least one should, must, must not or filter clause. Otherwisefalse
.
-
adjustPureNegative
public BoolQueryBuilder adjustPureNegative(boolean adjustPureNegative)
If a boolean query contains only negative ("must not") clauses should the BooleanQuery be enhanced with aMatchAllDocsQuery
in order to act as a pure exclude. The default istrue
.
-
adjustPureNegative
public boolean adjustPureNegative()
- Returns:
- the setting for the adjust_pure_negative setting in this query
-
doXContent
protected void doXContent(XContentBuilder builder, ToXContent.Params params) throws java.io.IOException
- Specified by:
doXContent
in classAbstractQueryBuilder<BoolQueryBuilder>
- Throws:
java.io.IOException
-
fromXContent
public static BoolQueryBuilder fromXContent(XContentParser parser) throws java.io.IOException, ParsingException
- Throws:
java.io.IOException
ParsingException
-
getWriteableName
public java.lang.String getWriteableName()
Description copied from interface:NamedWriteable
Returns the name of the writeable object
-
doToQuery
protected org.apache.lucene.search.Query doToQuery(QueryShardContext context) throws java.io.IOException
- Specified by:
doToQuery
in classAbstractQueryBuilder<BoolQueryBuilder>
- Throws:
java.io.IOException
-
doHashCode
protected int doHashCode()
- Specified by:
doHashCode
in classAbstractQueryBuilder<BoolQueryBuilder>
-
doEquals
protected boolean doEquals(BoolQueryBuilder other)
Description copied from class:AbstractQueryBuilder
Indicates whether some otherQueryBuilder
object of the same type is "equal to" this one.- Specified by:
doEquals
in classAbstractQueryBuilder<BoolQueryBuilder>
-
doRewrite
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws java.io.IOException
- Overrides:
doRewrite
in classAbstractQueryBuilder<BoolQueryBuilder>
- Throws:
java.io.IOException
-
extractInnerHitBuilders
protected void extractInnerHitBuilders(java.util.Map<java.lang.String,InnerHitContextBuilder> innerHits)
Description copied from class:AbstractQueryBuilder
For internal usage only! Extracts the inner hits from the query tree. While it extracts inner hits, child inner hits are inlined into the inner hit builder they belong to.- Overrides:
extractInnerHitBuilders
in classAbstractQueryBuilder<BoolQueryBuilder>
-
-