org.apache.lucene.search.Query and can be combined with BooleanQuery. The sections below describe each type, when to reach for it, and how to construct it.
TermQuery
TermQuery
Matches documents that contain an exact term in a specified field. It is the simplest and most efficient query type.
TermQuery matches the literal byte value stored in the index. If the field was analyzed at index time, you must use the same analyzed form (e.g. lowercased) as the search term.BooleanQuery
BooleanQuery
Combines multiple sub-queries using Boolean logic. Each clause carries an
Use To require a minimum number of
Occur value:| Occur | Meaning |
|---|---|
MUST | The clause must match. Contributes to the score. |
SHOULD | The clause may match. Contributes to the score when it does. |
MUST_NOT | The clause must not match. Does not contribute to the score. |
FILTER | The clause must match but does not contribute to the score. |
BooleanQuery.Builder to assemble clauses:SHOULD clauses to match, call setMinimumNumberShouldMatch:The default maximum clause count is 1,024. Attempting to add more clauses throws
IndexSearcher.TooManyClauses. Increase the limit with IndexSearcher.setMaxClauseCount(int).PhraseQuery and MultiPhraseQuery
PhraseQuery and MultiPhraseQuery
PhraseQuery matches documents that contain a sequence of terms in order, with an optional slop (number of allowed intervening positions).MultiPhraseQuery allows multiple alternative terms at one or more positions, which is useful for synonyms:FuzzyQuery
FuzzyQuery
Matches terms within a given edit distance (Damerau-Levenshtein) of the target term. Useful for handling typos.
Lucene supports at most 2 edits (
LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE). Terms shorter than 3 characters are unlikely to match because the minimum term length is used to bound the allowed edit distance.WildcardQuery
WildcardQuery
Matches terms using
? (single character) and * (zero or more characters) wildcards.PrefixQuery
PrefixQuery
Matches all terms that begin with a given prefix. More efficient than a leading-wildcard
WildcardQuery.TermRangeQuery
TermRangeQuery
Matches all terms in a field that fall within a lexicographic range. Suitable for string fields (e.g. dates stored as
yyyyMMdd strings).PointRangeQuery (numeric ranges)
PointRangeQuery (numeric ranges)
For numeric fields indexed with
IntField, LongField, FloatField, or DoubleField, use the static range methods on the corresponding point class. These are backed by a BKD tree and are significantly faster than TermRangeQuery for numeric data.RegexpQuery
RegexpQuery
Matches terms that conform to a regular expression using Lucene’s automaton-based regexp engine (not Java’s Lucene’s regexp syntax supports
java.util.regex).., *, +, ?, |, [...], and {n,m} repetition but does not support lookaheads or backreferences.MatchAllDocsQuery and MatchNoDocsQuery
MatchAllDocsQuery and MatchNoDocsQuery
MatchAllDocsQuery matches every document in the index. MatchNoDocsQuery matches none.ConstantScoreQuery
ConstantScoreQuery
Wraps another query and assigns a constant score to all matching documents, bypassing BM25 scoring. Useful when you need a filter that contributes to ranking but with a fixed weight.To assign a specific boost value, wrap with
BoostQuery:DisjunctionMaxQuery
DisjunctionMaxQuery
Returns documents matching any of the sub-queries, but scores each document using only the highest-scoring sub-query match rather than summing scores. A small
tieBreakMultiplier is added to distinguish documents that match more than one sub-query.KnnFloatVectorQuery (vector / semantic search)
KnnFloatVectorQuery (vector / semantic search)
Performs approximate k-nearest-neighbor search over a float vector field indexed with You can combine a
KnnFloatVectorField. Returns the k nearest neighbors by cosine or dot-product similarity, depending on how the field was configured.KnnFloatVectorQuery with a BooleanQuery to blend keyword and semantic results:Quick reference table
| Query class | Typical use case |
|---|---|
TermQuery | Exact keyword match |
BooleanQuery | Combine multiple queries |
PhraseQuery | Ordered phrase matching with optional slop |
MultiPhraseQuery | Phrase matching with synonym alternatives |
FuzzyQuery | Typo-tolerant term matching |
WildcardQuery | Glob-style pattern matching |
PrefixQuery | Prefix / autocomplete |
TermRangeQuery | Lexicographic range on string fields |
IntField.newRangeQuery etc. | Numeric range on point-indexed fields |
RegexpQuery | Regular expression on term dictionary |
MatchAllDocsQuery | Return all documents |
ConstantScoreQuery | Filter without scoring |
DisjunctionMaxQuery | Multi-field best-match scoring |
KnnFloatVectorQuery | Dense vector / semantic search |