PhraseQuery.Builder (Lucene 10.5.0 core API)
Description: declaration: package: org.apache.lucene.search, class: PhraseQuery, class: Builder

Package org.apache.lucene.search

# Class PhraseQuery.Builder

java.lang.Object
org.apache.lucene.search.PhraseQuery.Builder

Enclosing class:
  `PhraseQuery`

public static class PhraseQuery.Builder extends Object
A builder for phrase queries.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Builder()`
  Sole constructor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `PhraseQuery.Builder`
  `add(Term term)`
  Adds a term to the end of the query phrase.
  `PhraseQuery.Builder`
  `add(Term term, int position)`
  Adds a term to the end of the query phrase.
  `PhraseQuery`
  `build()`
  Build a phrase query based on the terms that have been added.
  `PhraseQuery.Builder`
  `setMaxTerms(int maxTerms)`
  Set the maximum number of terms allowed in the phrase query.
  `PhraseQuery.Builder`
  `setSlop(int slop)`
  Set the slop.

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

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Builder

    public Builder()
    Sole constructor.

- ## Method Details

  - ### setSlop

    public PhraseQuery.Builder setSlop(int slop)
    Set the slop.
    See Also:
      - `PhraseQuery.getSlop()`

  - ### setMaxTerms

    public PhraseQuery.Builder setMaxTerms(int maxTerms)
    Set the maximum number of terms allowed in the phrase query. This helps prevent excessive memory usage for very long phrases.

    If the number of terms added via `add(Term)` or `add(Term, int)` exceeds this threshold, an `IllegalArgumentException` will be thrown.

  - ### add

    public PhraseQuery.Builder add(Term term)
    Adds a term to the end of the query phrase. The relative position of the term is the one immediately after the last term added.

  - ### add

    public PhraseQuery.Builder add(Term term, int position)
    Adds a term to the end of the query phrase. The relative position of the term within the phrase is specified explicitly, but must be greater than or equal to that of the previously added term. A greater position allows phrases with gaps (e.g. in connection with stopwords). If the position is equal, you most likely should be using `MultiPhraseQuery` instead which only requires one term at each position to match; this class requires all of them.

  - ### build

    public PhraseQuery build()
    Build a phrase query based on the terms that have been added.