MultiPhraseQuery is a generalized version of PhraseQuery, with an added
method
.
To use this class, to search for the phrase "Microsoft app*" first use
add(Term) on the term "Microsoft", then find all terms that have "app" as
prefix using IndexReader.terms(Term), and use MultiPhraseQuery.add(Term[]
terms) to add them to the query.
Method from org.apache.lucene.search.MultiPhraseQuery Detail: |
public void add(Term term) {
add(new Term[]{term});
}
Add a single term at the next position in the phrase. |
public void add(Term[] terms) {
int position = 0;
if (positions.size() > 0)
position = positions.get(positions.size()-1).intValue() + 1;
add(terms, position);
}
Add multiple terms at the next position in the phrase. Any of the terms
may match. |
public void add(Term[] terms,
int position) {
if (termArrays.size() == 0)
field = terms[0].field();
for (int i = 0; i < terms.length; i++) {
if (terms[i].field() != field) {
throw new IllegalArgumentException(
"All phrase terms must be in the same field (" + field + "): "
+ terms[i]);
}
}
termArrays.add(terms);
positions.add(Integer.valueOf(position));
}
Allows to specify the relative position of terms within the phrase. |
public Weight createWeight(Searcher searcher) throws IOException {
return new MultiPhraseWeight(searcher);
}
|
public boolean equals(Object o) {
if (!(o instanceof MultiPhraseQuery)) return false;
MultiPhraseQuery other = (MultiPhraseQuery)o;
return this.getBoost() == other.getBoost()
&& this.slop == other.slop
&& termArraysEquals(this.termArrays, other.termArrays)
&& this.positions.equals(other.positions);
}
Returns true if o is equal to this. |
public void extractTerms(Set<Term> terms) {
for (final Term[] arr : termArrays) {
for (final Term term: arr) {
terms.add(term);
}
}
}
|
public int[] getPositions() {
int[] result = new int[positions.size()];
for (int i = 0; i < positions.size(); i++)
result[i] = positions.get(i).intValue();
return result;
}
Returns the relative positions of terms in this phrase. |
public int getSlop() {
return slop;
}
Sets the phrase slop for this query. |
public List<Term> getTermArrays() {
return Collections.unmodifiableList(termArrays);
}
Returns a List of the terms in the multiphrase.
Do not modify the List or its contents. |
public int hashCode() {
return Float.floatToIntBits(getBoost())
^ slop
^ termArraysHashCode()
^ positions.hashCode()
^ 0x4AC65113;
}
Returns a hash code value for this object. |
public Query rewrite(IndexReader reader) {
if (termArrays.size() == 1) { // optimize one-term case
Term[] terms = termArrays.get(0);
BooleanQuery boq = new BooleanQuery(true);
for (int i=0; i< terms.length; i++) {
boq.add(new TermQuery(terms[i]), BooleanClause.Occur.SHOULD);
}
boq.setBoost(getBoost());
return boq;
} else {
return this;
}
}
|
public void setSlop(int s) {
slop = s;
}
Sets the phrase slop for this query. |
public final String toString(String f) {
StringBuilder buffer = new StringBuilder();
if (!field.equals(f)) {
buffer.append(field);
buffer.append(":");
}
buffer.append("\"");
Iterator< Term[] > i = termArrays.iterator();
while (i.hasNext()) {
Term[] terms = i.next();
if (terms.length > 1) {
buffer.append("(");
for (int j = 0; j < terms.length; j++) {
buffer.append(terms[j].text());
if (j < terms.length-1)
buffer.append(" ");
}
buffer.append(")");
} else {
buffer.append(terms[0].text());
}
if (i.hasNext())
buffer.append(" ");
}
buffer.append("\"");
if (slop != 0) {
buffer.append("~");
buffer.append(slop);
}
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
}
Prints a user-readable version of this query. |