org.apache.lucene.queries

## Class TermsQuery

- java.lang.Object
- - org.apache.lucene.search.Query
  - - org.apache.lucene.queries.TermsQuery

- All Implemented Interfaces:
    Cloneable, Accountable

      public class TermsQuery
      extends Query
      implements Accountable

  Specialization for a disjunction over many terms that behaves like a `ConstantScoreQuery` over a `BooleanQuery` containing only `BooleanClause.Occur.SHOULD` clauses.

  For instance in the following example, both @{code q1} and `q2` would yield the same scores:

       Query q1 = new TermsQuery(new Term("field", "foo"), new Term("field", "bar"));

       BooleanQuery bq = new BooleanQuery();
       bq.add(new TermQuery(new Term("field", "foo")), Occur.SHOULD);
       bq.add(new TermQuery(new Term("field", "bar")), Occur.SHOULD);
       Query q2 = new ConstantScoreQuery(bq);

  When there are few terms, this query executes like a regular disjunction. However, when there are many terms, instead of merging iterators on the fly, it will populate a bit set with matching docs and return a `Scorer` over this bit set.

  NOTE: This query produces scores that are equal to its boost

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `TermsQuery(Collection<Term> terms)` Creates a new `TermsQuery` from the given collection.
    `TermsQuery(String field, BytesRef... terms)` Creates a new `TermsQuery` from the given `BytesRef` array for a single field.
    `TermsQuery(String field, Collection<BytesRef> terms)` Creates a new `TermsQuery` from the given collection for a single field.
    `TermsQuery(Term... terms)` Creates a new `TermsQuery` from the given array.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `Weight` | `createWeight(IndexSearcher searcher, boolean needsScores)`
    `boolean` | `equals(Object obj)`
    `Collection<Accountable>` | `getChildResources()`
    `int` | `hashCode()`
    `long` | `ramBytesUsed()`
    `Query` | `rewrite(IndexReader reader)`
    `String` | `toString(String defaultField)`

    - ### Methods inherited from class org.apache.lucene.search.Query

      `clone, getBoost, setBoost, toString`

    - ### Methods inherited from class java.lang.Object

      `finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### TermsQuery

          public TermsQuery(Collection<Term> terms)

      Creates a new `TermsQuery` from the given collection. It can contain duplicate terms and multiple fields.

    - #### TermsQuery

          public TermsQuery(String field,
                    Collection<BytesRef> terms)

      Creates a new `TermsQuery` from the given collection for a single field. It can contain duplicate terms.

    - #### TermsQuery

          public TermsQuery(String field,
                    BytesRef... terms)

      Creates a new `TermsQuery` from the given `BytesRef` array for a single field.

    - #### TermsQuery

          public TermsQuery(Term... terms)

      Creates a new `TermsQuery` from the given array. The array can contain duplicate terms and multiple fields.

  - ### Method Detail

    - #### rewrite

          public Query rewrite(IndexReader reader)
                        throws IOException

      Overrides:
        `rewrite` in class `Query`
      Throws:
        `IOException`

    - #### equals

          public boolean equals(Object obj)

      Overrides:
        `equals` in class `Query`

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Query`

    - #### toString

          public String toString(String defaultField)

      Specified by:
        `toString` in class `Query`

    - #### ramBytesUsed

          public long ramBytesUsed()

      Specified by:
        `ramBytesUsed` in interface `Accountable`

    - #### getChildResources

          public Collection<Accountable> getChildResources()

      Specified by:
        `getChildResources` in interface `Accountable`

    - #### createWeight

          public Weight createWeight(IndexSearcher searcher,
                            boolean needsScores)
                              throws IOException

      Overrides:
        `createWeight` in class `Query`
      Throws:
        `IOException`

Copyright © 2000-2017 Apache Software Foundation. All Rights Reserved.