Class PhraseQuery
"new york"
.
This query may be combined with other terms or queries with a BooleanQuery
.
NOTE: All terms in the phrase must match, even those at the same position. If you have
terms at the same position, perhaps synonyms, you probably want MultiPhraseQuery
instead
which only requires one term at a position to match.
Also, Leading holes don't have any particular meaning for this query and will be ignored. For
instance this query:
PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.add(new Term("body", "one"), 4); builder.add(new Term("body", "two"), 5); PhraseQuery pq = builder.build();is equivalent to the below query:
PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.add(new Term("body", "one"), 0); builder.add(new Term("body", "two"), 1); PhraseQuery pq = builder.build();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A builder for phrase queries.static class
Term postings and position information for phrase matching -
Constructor Summary
ConstructorsConstructorDescriptionPhraseQuery
(int slop, String field, String... terms) Create a phrase query which will match documents that contain the given list of terms at consecutive positions infield
, and at a maximum edit distance ofslop
.PhraseQuery
(int slop, String field, BytesRef... terms) Create a phrase query which will match documents that contain the given list of terms at consecutive positions infield
, and at a maximum edit distance ofslop
.PhraseQuery
(String field, String... terms) Create a phrase query which will match documents that contain the given list of terms at consecutive positions infield
.PhraseQuery
(String field, BytesRef... terms) Create a phrase query which will match documents that contain the given list of terms at consecutive positions infield
. -
Method Summary
Modifier and TypeMethodDescriptioncreateWeight
(IndexSearcher searcher, ScoreMode scoreMode, float boost) Expert: Constructs an appropriate Weight implementation for this query.boolean
Returns true iffo
is equal to this.getField()
Returns the field this query applies toint[]
Returns the relative positions of terms in this phrase.int
getSlop()
Return the slop for thisPhraseQuery
.Term[]
getTerms()
Returns the list of terms in this phrase.int
hashCode()
Returns a hash code value for this object.rewrite
(IndexSearcher indexSearcher) Expert: called to re-write queries into primitive queries.static float
termPositionsCost
(TermsEnum termsEnum) Returns an expected cost in simple operations of processing the occurrences of a term in a document that contains the term.Prints a user-readable version of this query.void
visit
(QueryVisitor visitor) Recurse through the query tree, visiting any child queries.Methods inherited from class org.apache.lucene.search.Query
classHash, sameClassAs, toString
-
Constructor Details
-
PhraseQuery
Create a phrase query which will match documents that contain the given list of terms at consecutive positions infield
, and at a maximum edit distance ofslop
. For more complicated use-cases, usePhraseQuery.Builder
.- See Also:
-
PhraseQuery
Create a phrase query which will match documents that contain the given list of terms at consecutive positions infield
. -
PhraseQuery
Create a phrase query which will match documents that contain the given list of terms at consecutive positions infield
, and at a maximum edit distance ofslop
. For more complicated use-cases, usePhraseQuery.Builder
.- See Also:
-
PhraseQuery
Create a phrase query which will match documents that contain the given list of terms at consecutive positions infield
.
-
-
Method Details
-
getSlop
public int getSlop()Return the slop for thisPhraseQuery
.The slop is an edit distance between respective positions of terms as defined in this
PhraseQuery
and the positions of terms in a document.For instance, when searching for
"quick fox"
, it is expected that the difference between the positions offox
andquick
is 1. So"a quick brown fox"
would be at an edit distance of 1 since the difference of the positions offox
andquick
is 2. Similarly,"the fox is quick"
would be at an edit distance of 3 since the difference of the positions offox
andquick
is -2. The slop defines the maximum edit distance for a document to match.More exact matches are scored higher than sloppier matches, thus search results are sorted by exactness.
-
getField
Returns the field this query applies to -
getTerms
Returns the list of terms in this phrase. -
getPositions
public int[] getPositions()Returns the relative positions of terms in this phrase. -
rewrite
Description copied from class:Query
Expert: called to re-write queries into primitive queries. For example, a PrefixQuery will be rewritten into a BooleanQuery that consists of TermQuerys.Callers are expected to call
rewrite
multiple times if necessary, until the rewritten query is the same as the original query.The rewrite process may be able to make use of IndexSearcher's executor and be executed in parallel if the executor is provided.
- Overrides:
rewrite
in classQuery
- Throws:
IOException
- See Also:
-
visit
Description copied from class:Query
Recurse through the query tree, visiting any child queries. -
termPositionsCost
Returns an expected cost in simple operations of processing the occurrences of a term in a document that contains the term. This is for use byTwoPhaseIterator.matchCost()
implementations.- Parameters:
termsEnum
- The term is the term at which this TermsEnum is positioned.- Throws:
IOException
-
createWeight
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException Description copied from class:Query
Expert: Constructs an appropriate Weight implementation for this query.Only implemented by primitive queries, which re-write to themselves.
- Overrides:
createWeight
in classQuery
- Parameters:
scoreMode
- How the produced scorers will be consumed.boost
- The boost that is propagated by the parent queries.- Throws:
IOException
-
toString
Prints a user-readable version of this query. -
equals
Returns true iffo
is equal to this. -
hashCode
public int hashCode()Returns a hash code value for this object.
-