Class BoolQueryBuilder

All Implemented Interfaces:
NamedWriteable, Writeable, org.elasticsearch.common.xcontent.ToXContent, org.elasticsearch.common.xcontent.ToXContentObject, QueryBuilder, Rewriteable<QueryBuilder>

public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder>
A Query that matches documents matching boolean combinations of other queries.
  • Field Details

  • Constructor Details

    • BoolQueryBuilder

      public BoolQueryBuilder()
      Build an empty bool query.
    • BoolQueryBuilder

      public BoolQueryBuilder(StreamInput in) throws IOException
      Read from a stream.
      Throws:
      IOException
  • Method Details

    • setMaxNestedDepth

      public static void setMaxNestedDepth(int maxNestedDepth)
      Set the maximum nested depth of bool queries. Default value is 20.
    • doWriteTo

      protected void doWriteTo(StreamOutput out) throws IOException
      Specified by:
      doWriteTo in class AbstractQueryBuilder<BoolQueryBuilder>
      Throws:
      IOException
    • must

      public BoolQueryBuilder must(QueryBuilder queryBuilder)
      Adds a query that must appear in the matching documents and will contribute to scoring. No null value allowed.
    • must

      public 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. No null value allowed.
    • filter

      public 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. No null value allowed.
    • mustNot

      public 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 no MUST clauses one or more SHOULD clauses must match a document for the BooleanQuery to match. No null value allowed.
      See Also:
      minimumShouldMatch(int)
    • should

      public List<QueryBuilder> should()
      Gets the list of clauses that should be matched by the returned documents.
      See Also:
      should(QueryBuilder), minimumShouldMatch(int)
    • minimumShouldMatch

      public String minimumShouldMatch()
      Returns:
      the string representation of the minimumShouldMatch settings for this query
    • minimumShouldMatch

      public BoolQueryBuilder minimumShouldMatch(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()
      Returns true iff this query builder has at least one should, must, must not or filter clause. Otherwise false.
    • adjustPureNegative

      public BoolQueryBuilder adjustPureNegative(boolean adjustPureNegative)
      If a boolean query contains only negative ("must not") clauses should the BooleanQuery be enhanced with a MatchAllDocsQuery in order to act as a pure exclude. The default is true.
    • adjustPureNegative

      public boolean adjustPureNegative()
      Returns:
      the setting for the adjust_pure_negative setting in this query
    • doXContent

      protected void doXContent(org.elasticsearch.common.xcontent.XContentBuilder builder, org.elasticsearch.common.xcontent.ToXContent.Params params) throws IOException
      Specified by:
      doXContent in class AbstractQueryBuilder<BoolQueryBuilder>
      Throws:
      IOException
    • fromXContent

      public static BoolQueryBuilder fromXContent(org.elasticsearch.common.xcontent.XContentParser parser, Integer nestedDepth) throws IOException, ParsingException
      Throws:
      IOException
      ParsingException
    • getWriteableName

      public String getWriteableName()
      Description copied from interface: NamedWriteable
      Returns the name of the writeable object
    • doToQuery

      protected org.apache.lucene.search.Query doToQuery(SearchExecutionContext context) throws IOException
      Specified by:
      doToQuery in class AbstractQueryBuilder<BoolQueryBuilder>
      Throws:
      IOException
    • doHashCode

      protected int doHashCode()
      Specified by:
      doHashCode in class AbstractQueryBuilder<BoolQueryBuilder>
    • doEquals

      protected boolean doEquals(BoolQueryBuilder other)
      Description copied from class: AbstractQueryBuilder
      Indicates whether some other QueryBuilder object of the same type is "equal to" this one.
      Specified by:
      doEquals in class AbstractQueryBuilder<BoolQueryBuilder>
    • doRewrite

      protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException
      Overrides:
      doRewrite in class AbstractQueryBuilder<BoolQueryBuilder>
      Throws:
      IOException
    • extractInnerHitBuilders

      protected void extractInnerHitBuilders(Map<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 class AbstractQueryBuilder<BoolQueryBuilder>