goller 2004/09/15 02:38:50 Modified: src/java/org/apache/lucene/search PhrasePrefixQuery.java Log: 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. Revision Changes Path 1.14 +42 -13 jakarta-lucene/src/java/org/apache/lucene/search/PhrasePrefixQuery.java Index: PhrasePrefixQuery.java =================================================================== RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/PhrasePrefixQuery.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- PhrasePrefixQuery.java 27 Aug 2004 20:20:47 -0000 1.13 +++ PhrasePrefixQuery.java 15 Sep 2004 09:38:50 -0000 1.14 @@ -19,6 +19,7 @@ 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 @@ 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 @@ * @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]); - } - } + 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); + 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 @@ } 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)); }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]