public class PhraseQuery extends Query
"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();
Modifier and Type | Class and Description |
---|---|
static class |
PhraseQuery.Builder
A builder for phrase queries.
|
(package private) static class |
PhraseQuery.PostingsAndFreq |
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
field |
private int[] |
positions |
private int |
slop |
private static int |
TERM_OPS_PER_POS
Number of simple operations in
Lucene50PostingsReader.BlockPostingsEnum#nextPosition()
when no seek or buffer refill is done. |
private static int |
TERM_POSNS_SEEK_OPS_PER_DOC
A guess of
the average number of simple operations for the initial seek and buffer refill
per document for the positions of a term.
|
private Term[] |
terms |
Modifier | Constructor and Description |
---|---|
|
PhraseQuery(int slop,
java.lang.String field,
BytesRef... terms)
Create a phrase query which will match documents that contain the given
list of terms at consecutive positions in
field , and at a
maximum edit distance of slop . |
|
PhraseQuery(int slop,
java.lang.String field,
java.lang.String... terms)
Create a phrase query which will match documents that contain the given
list of terms at consecutive positions in
field , and at a
maximum edit distance of slop . |
private |
PhraseQuery(int slop,
Term[] terms,
int[] positions) |
|
PhraseQuery(java.lang.String field,
BytesRef... terms)
Create a phrase query which will match documents that contain the given
list of terms at consecutive positions in
field . |
|
PhraseQuery(java.lang.String field,
java.lang.String... terms)
Create a phrase query which will match documents that contain the given
list of terms at consecutive positions in
field . |
Modifier and Type | Method and Description |
---|---|
Weight |
createWeight(IndexSearcher searcher,
ScoreMode scoreMode,
float boost)
Expert: Constructs an appropriate Weight implementation for this query.
|
boolean |
equals(java.lang.Object other)
Returns true iff
o is equal to this. |
private boolean |
equalsTo(PhraseQuery other) |
java.lang.String |
getField()
Returns the field this query applies to
|
int[] |
getPositions()
Returns the relative positions of terms in this phrase.
|
int |
getSlop()
Return the slop for this
PhraseQuery . |
Term[] |
getTerms()
Returns the list of terms in this phrase.
|
int |
hashCode()
Returns a hash code value for this object.
|
private static int[] |
incrementalPositions(int length) |
Query |
rewrite(IndexReader reader)
Expert: called to re-write queries into primitive queries.
|
private static boolean |
termNotInReader(LeafReader reader,
Term term) |
(package private) 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.
|
java.lang.String |
toString(java.lang.String f)
Prints a user-readable version of this query.
|
private static Term[] |
toTerms(java.lang.String field,
BytesRef... termBytes) |
private static Term[] |
toTerms(java.lang.String field,
java.lang.String... termStrings) |
void |
visit(QueryVisitor visitor)
Recurse through the query tree, visiting any child queries
|
classHash, sameClassAs, toString
private final int slop
private final java.lang.String field
private final Term[] terms
private final int[] positions
private static final int TERM_POSNS_SEEK_OPS_PER_DOC
Lucene50PostingsReader.BlockPostingsEnum#nextPosition()
.
Aside: Instead of being constant this could depend among others on
Lucene50PostingsFormat.BLOCK_SIZE
,
TermsEnum.docFreq()
,
TermsEnum.totalTermFreq()
,
DocIdSetIterator.cost()
(expected number of matching docs),
IndexReader.maxDoc()
(total number of docs in the segment),
and the seek time and block size of the device storing the index.
private static final int TERM_OPS_PER_POS
Lucene50PostingsReader.BlockPostingsEnum#nextPosition()
when no seek or buffer refill is done.private PhraseQuery(int slop, Term[] terms, int[] positions)
public PhraseQuery(int slop, java.lang.String field, java.lang.String... terms)
field
, and at a
maximum edit distance of slop
. For more complicated use-cases,
use PhraseQuery.Builder
.getSlop()
public PhraseQuery(java.lang.String field, java.lang.String... terms)
field
.public PhraseQuery(int slop, java.lang.String field, BytesRef... terms)
field
, and at a
maximum edit distance of slop
. For more complicated use-cases,
use PhraseQuery.Builder
.getSlop()
public PhraseQuery(java.lang.String field, BytesRef... terms)
field
.private static int[] incrementalPositions(int length)
private static Term[] toTerms(java.lang.String field, java.lang.String... termStrings)
public int getSlop()
PhraseQuery
.
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 of fox
and quick
is 1.
So "a quick brown fox"
would be at an edit distance of 1 since the
difference of the positions of fox
and quick
is 2.
Similarly, "the fox is quick"
would be at an edit distance of 3
since the difference of the positions of fox
and quick
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.
public java.lang.String getField()
public Term[] getTerms()
public int[] getPositions()
public Query rewrite(IndexReader reader) throws java.io.IOException
Query
public void visit(QueryVisitor visitor)
Query
static float termPositionsCost(TermsEnum termsEnum) throws java.io.IOException
TwoPhaseIterator.matchCost()
implementations.termsEnum
- The term is the term at which this TermsEnum is positioned.java.io.IOException
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws java.io.IOException
Query
Only implemented by primitive queries, which re-write to themselves.
createWeight
in class Query
scoreMode
- How the produced scorers will be consumed.boost
- The boost that is propagated by the parent queries.java.io.IOException
private static boolean termNotInReader(LeafReader reader, Term term) throws java.io.IOException
java.io.IOException
public java.lang.String toString(java.lang.String f)
public boolean equals(java.lang.Object other)
o
is equal to this.equals
in class Query
Query.sameClassAs(Object)
,
Query.classHash()
private boolean equalsTo(PhraseQuery other)
public int hashCode()
hashCode
in class Query
Query.equals(Object)