Skip to content

Apache Lucene open-source search software

PhraseQuery and PhrasePrefixQuery are extended. It's now possible to specify the relative position of a term within a phrase. This allows gaps and multiple terms at the same position.

Christoph Gollerfd4cd859c579
diff --git a/src/java/org/apache/lucene/search/ExactPhraseScorer.java b/src/java/org/apache/lucene/search/ExactPhraseScorer.java
index 8d288377e0c..82328345c13 100644
--- a/src/java/org/apache/lucene/search/ExactPhraseScorer.java
+++ b/src/java/org/apache/lucene/search/ExactPhraseScorer.java
@@ -21,9 +21,9 @@ import org.apache.lucene.index.*;
final class ExactPhraseScorer extends PhraseScorer {
- ExactPhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
+ ExactPhraseScorer(Weight weight, TermPositions[] tps, int[] positions, Similarity similarity,
byte[] norms) throws IOException {
- super(weight, tps, similarity, norms);
+ super(weight, tps, positions, similarity, norms);
}
protected final float phraseFreq() throws IOException {
diff --git a/src/java/org/apache/lucene/search/PhrasePrefixQuery.java b/src/java/org/apache/lucene/search/PhrasePrefixQuery.java
index 07810bb168e..88e0983fe1e 100644
--- a/src/java/org/apache/lucene/search/PhrasePrefixQuery.java
+++ b/src/java/org/apache/lucene/search/PhrasePrefixQuery.java
@@ -19,6 +19,7 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
+import java.util.Vector;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultipleTermPositions;
@@ -40,6 +41,7 @@ import org.apache.lucene.search.Query;
public class PhrasePrefixQuery extends Query {
private String field;
private ArrayList termArrays = new ArrayList();
+ private Vector positions = new Vector();
private int slop = 0;
@@ -64,18 +66,45 @@ public class PhrasePrefixQuery extends Query {
* @see PhraseQuery#add(Term)
*/
public void add(Term[] terms) {
- 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);
+ int position = 0;
+ if(positions.size() > 0)
+ position = ((Integer) positions.lastElement()).intValue() + 1;
+
+ add(terms, position);
+ }
+
+ /**
+ * Allows to specify the relative position of terms within the phrase.
+ *
+ * @ see PhraseQuery#add(Term, int)
+ * @param terms
+ * @param position
+ */
+ 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.addElement(new Integer(position));
+ }
+
+ /**
+ * Returns the relative positions of terms in this phrase.
+ * @return
+ */
+ public int[] getPositions() {
+ int[] result = new int[positions.size()];
+ for(int i = 0; i < positions.size(); i++)
+ result[i] = ((Integer) positions.elementAt(i)).intValue();
+ return result;
}
private class PhrasePrefixWeight implements Weight {
@@ -131,10 +160,10 @@ public class PhrasePrefixQuery extends Query {
}
if (slop == 0)
- return new ExactPhraseScorer(this, tps, getSimilarity(searcher),
+ return new ExactPhraseScorer(this, tps, getPositions(), getSimilarity(searcher),
reader.norms(field));
else
- return new SloppyPhraseScorer(this, tps, getSimilarity(searcher),
+ return new SloppyPhraseScorer(this, tps, getPositions(), getSimilarity(searcher),
slop, reader.norms(field));
}
diff --git a/src/java/org/apache/lucene/search/PhraseQuery.java b/src/java/org/apache/lucene/search/PhraseQuery.java
index d0e40da59d5..bf1932c3d20 100644
--- a/src/java/org/apache/lucene/search/PhraseQuery.java
+++ b/src/java/org/apache/lucene/search/PhraseQuery.java
@@ -29,6 +29,7 @@ import org.apache.lucene.index.IndexReader;
public class PhraseQuery extends Query {
private String field;
private Vector terms = new Vector();
+ private Vector positions = new Vector();
private int slop = 0;
/** Constructs an empty phrase query. */
@@ -52,21 +53,52 @@ public class PhraseQuery extends Query {
/** Returns the slop. See setSlop(). */
public int getSlop() { return slop; }
- /** Adds a term to the end of the query phrase. */
+ /**
+ * 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.
+ */
public void add(Term term) {
- if (terms.size() == 0)
- field = term.field();
- else if (term.field() != field)
- throw new IllegalArgumentException
- ("All phrase terms must be in the same field: " + term);
-
- terms.addElement(term);
+ int position = 0;
+ if(positions.size() > 0)
+ position = ((Integer) positions.lastElement()).intValue() + 1;
+
+ add(term, position);
+ }
+
+ /**
+ * Adds a term to the end of the query phrase.
+ * The relative position of the term within the phrase is specified explicitly.
+ * This allows e.g. phrases with more than one term at the same position
+ * or phrases with gaps (e.g. in connection with stopwords).
+ *
+ * @param term
+ * @param position
+ */
+ public void add(Term term, int position) {
+ if (terms.size() == 0)
+ field = term.field();
+ else if (term.field() != field)
+ throw new IllegalArgumentException("All phrase terms must be in the same field: " + term);
+
+ terms.addElement(term);
+ positions.addElement(new Integer(position));
}
/** Returns the set of terms in this phrase. */
public Term[] getTerms() {
return (Term[])terms.toArray(new Term[0]);
}
+
+ /**
+ * Returns the relative positions of terms in this phrase.
+ * @return
+ */
+ public int[] getPositions() {
+ int[] result = new int[positions.size()];
+ for(int i = 0; i < positions.size(); i++)
+ result[i] = ((Integer) positions.elementAt(i)).intValue();
+ return result;
+ }
private class PhraseWeight implements Weight {
private Searcher searcher;
@@ -109,11 +141,11 @@ public class PhraseQuery extends Query {
}
if (slop == 0) // optimize exact case
- return new ExactPhraseScorer(this, tps, getSimilarity(searcher),
+ return new ExactPhraseScorer(this, tps, getPositions(), getSimilarity(searcher),
reader.norms(field));
else
return
- new SloppyPhraseScorer(this, tps, getSimilarity(searcher), slop,
+ new SloppyPhraseScorer(this, tps, getPositions(), getSimilarity(searcher), slop,
reader.norms(field));
}
@@ -244,14 +276,16 @@ public class PhraseQuery extends Query {
PhraseQuery other = (PhraseQuery)o;
return (this.getBoost() == other.getBoost())
&& (this.slop == other.slop)
- && this.terms.equals(other.terms);
+ && this.terms.equals(other.terms)
+ && this.positions.equals(other.positions);
}
/** Returns a hash code value for this object.*/
public int hashCode() {
return Float.floatToIntBits(getBoost())
^ Float.floatToIntBits(slop)
- ^ terms.hashCode();
+ ^ terms.hashCode()
+ ^ positions.hashCode();
}
}
diff --git a/src/java/org/apache/lucene/search/PhraseScorer.java b/src/java/org/apache/lucene/search/PhraseScorer.java
index 57012c6a92b..311b20e5122 100644
--- a/src/java/org/apache/lucene/search/PhraseScorer.java
+++ b/src/java/org/apache/lucene/search/PhraseScorer.java
@@ -32,7 +32,8 @@ abstract class PhraseScorer extends Scorer {
private float freq;
- PhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
+
+ PhraseScorer(Weight weight, TermPositions[] tps, int[] positions, Similarity similarity,
byte[] norms) {
super(similarity);
this.norms = norms;
@@ -41,7 +42,7 @@ abstract class PhraseScorer extends Scorer {
// convert tps to a list
for (int i = 0; i < tps.length; i++) {
- PhrasePositions pp = new PhrasePositions(tps[i], i);
+ PhrasePositions pp = new PhrasePositions(tps[i], positions[i]);
if (last != null) { // add next to end of list
last.next = pp;
} else
diff --git a/src/java/org/apache/lucene/search/SloppyPhraseScorer.java b/src/java/org/apache/lucene/search/SloppyPhraseScorer.java
index 40768cfaffd..af6064d7827 100644
--- a/src/java/org/apache/lucene/search/SloppyPhraseScorer.java
+++ b/src/java/org/apache/lucene/search/SloppyPhraseScorer.java
@@ -23,9 +23,9 @@ import java.io.IOException;
final class SloppyPhraseScorer extends PhraseScorer {
private int slop;
- SloppyPhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
+ SloppyPhraseScorer(Weight weight, TermPositions[] tps, int[] positions, Similarity similarity,
int slop, byte[] norms) {
- super(weight, tps, similarity, norms);
+ super(weight, tps, positions, similarity, norms);
this.slop = slop;
}