mirror of https://github.com/apache/lucene.git
Bug in PhraseScorer and ConjunctionScorer
skipTo implementation fixed. Unit test for this bug added. git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150303 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0cc255bf7b
commit
e51e76636c
|
@ -46,12 +46,14 @@ final class ConjunctionScorer extends Scorer {
|
|||
} else if (more) {
|
||||
more = last().next(); // trigger further scanning
|
||||
}
|
||||
|
||||
return doNext();
|
||||
}
|
||||
|
||||
private boolean doNext() throws IOException {
|
||||
while (more && first().doc() < last().doc()) { // find doc w/ all clauses
|
||||
more = first().skipTo(last().doc()); // skip first upto last
|
||||
scorers.addLast(scorers.removeFirst()); // move first to last
|
||||
}
|
||||
|
||||
return more; // found a doc with all clauses
|
||||
}
|
||||
|
||||
|
@ -62,7 +64,7 @@ final class ConjunctionScorer extends Scorer {
|
|||
}
|
||||
if (more)
|
||||
sortScorers(); // re-sort scorers
|
||||
return more;
|
||||
return doNext();
|
||||
}
|
||||
|
||||
public float score() throws IOException {
|
||||
|
@ -96,6 +98,7 @@ final class ConjunctionScorer extends Scorer {
|
|||
Scorer[] array = (Scorer[])scorers.toArray(new Scorer[scorers.size()]);
|
||||
scorers.clear(); // empty the list
|
||||
|
||||
// note that this comparator is not consistent with equals!
|
||||
Arrays.sort(array, new Comparator() { // sort the array
|
||||
public int compare(Object o1, Object o2) {
|
||||
return ((Scorer)o1).doc() - ((Scorer)o2).doc();
|
||||
|
|
|
@ -63,7 +63,11 @@ abstract class PhraseScorer extends Scorer {
|
|||
} else if (more) {
|
||||
more = last.next(); // trigger further scanning
|
||||
}
|
||||
|
||||
return doNext();
|
||||
}
|
||||
|
||||
// next without initial increment
|
||||
private boolean doNext() throws IOException {
|
||||
while (more) {
|
||||
while (more && first.doc < last.doc) { // find doc w/ all the terms
|
||||
more = first.skipTo(last.doc); // skip first upto last
|
||||
|
@ -94,7 +98,7 @@ abstract class PhraseScorer extends Scorer {
|
|||
}
|
||||
if (more)
|
||||
sort(); // re-sort
|
||||
return more;
|
||||
return doNext();
|
||||
}
|
||||
|
||||
protected abstract float phraseFreq() throws IOException;
|
||||
|
|
|
@ -34,24 +34,16 @@ import org.apache.lucene.store.RAMDirectory;
|
|||
public class TestPhraseQuery extends TestCase {
|
||||
private IndexSearcher searcher;
|
||||
private PhraseQuery query;
|
||||
private RAMDirectory directory;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
RAMDirectory directory = new RAMDirectory();
|
||||
directory = new RAMDirectory();
|
||||
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
|
||||
|
||||
Document doc = new Document();
|
||||
doc.add(Field.Text("field", "one two three four five"));
|
||||
writer.addDocument(doc);
|
||||
|
||||
doc = new Document();
|
||||
doc.add(new Field("source", "marketing info", true, true, true));
|
||||
writer.addDocument(doc);
|
||||
|
||||
doc = new Document();
|
||||
doc.add(new Field("contents", "foobar", true, true, true));
|
||||
doc.add(new Field("source", "marketing info", true, true, true));
|
||||
writer.addDocument(doc);
|
||||
|
||||
writer.optimize();
|
||||
writer.close();
|
||||
|
||||
|
@ -61,6 +53,7 @@ public class TestPhraseQuery extends TestCase {
|
|||
|
||||
public void tearDown() throws Exception {
|
||||
searcher.close();
|
||||
directory.close();
|
||||
}
|
||||
|
||||
public void testNotCloseEnough() throws Exception {
|
||||
|
@ -186,16 +179,79 @@ public class TestPhraseQuery extends TestCase {
|
|||
}
|
||||
|
||||
public void testPhraseQueryInConjunctionScorer() throws Exception {
|
||||
query.add(new Term("source", "marketing"));
|
||||
query.add(new Term("source", "info"));
|
||||
Hits hits = searcher.search(query);
|
||||
RAMDirectory directory = new RAMDirectory();
|
||||
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
|
||||
|
||||
Document doc = new Document();
|
||||
doc.add(new Field("source", "marketing info", true, true, true));
|
||||
writer.addDocument(doc);
|
||||
|
||||
doc = new Document();
|
||||
doc.add(new Field("contents", "foobar", true, true, true));
|
||||
doc.add(new Field("source", "marketing info", true, true, true));
|
||||
writer.addDocument(doc);
|
||||
|
||||
writer.optimize();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(directory);
|
||||
|
||||
PhraseQuery phraseQuery = new PhraseQuery();
|
||||
phraseQuery.add(new Term("source", "marketing"));
|
||||
phraseQuery.add(new Term("source", "info"));
|
||||
Hits hits = searcher.search(phraseQuery);
|
||||
assertEquals(2, hits.length());
|
||||
|
||||
TermQuery termQuery = new TermQuery(new Term("contents","foobar"));
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
booleanQuery.add(termQuery, true, false);
|
||||
booleanQuery.add(query, true, false);
|
||||
booleanQuery.add(phraseQuery, true, false);
|
||||
hits = searcher.search(booleanQuery);
|
||||
assertEquals(1, hits.length());
|
||||
|
||||
searcher.close();
|
||||
|
||||
writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
|
||||
doc = new Document();
|
||||
doc.add(new Field("contents", "map entry woo", true, true, true));
|
||||
writer.addDocument(doc);
|
||||
|
||||
doc = new Document();
|
||||
doc.add(new Field("contents", "woo map entry", true, true, true));
|
||||
writer.addDocument(doc);
|
||||
|
||||
doc = new Document();
|
||||
doc.add(new Field("contents", "map foobarword entry woo", true, true, true));
|
||||
writer.addDocument(doc);
|
||||
|
||||
writer.optimize();
|
||||
writer.close();
|
||||
|
||||
searcher = new IndexSearcher(directory);
|
||||
|
||||
termQuery = new TermQuery(new Term("contents","woo"));
|
||||
phraseQuery = new PhraseQuery();
|
||||
phraseQuery.add(new Term("contents","map"));
|
||||
phraseQuery.add(new Term("contents","entry"));
|
||||
|
||||
hits = searcher.search(termQuery);
|
||||
assertEquals(3, hits.length());
|
||||
hits = searcher.search(phraseQuery);
|
||||
assertEquals(2, hits.length());
|
||||
|
||||
booleanQuery = new BooleanQuery();
|
||||
booleanQuery.add(termQuery, true, false);
|
||||
booleanQuery.add(phraseQuery, true, false);
|
||||
hits = searcher.search(booleanQuery);
|
||||
assertEquals(2, hits.length());
|
||||
|
||||
booleanQuery = new BooleanQuery();
|
||||
booleanQuery.add(phraseQuery, true, false);
|
||||
booleanQuery.add(termQuery, true, false);
|
||||
hits = searcher.search(booleanQuery);
|
||||
assertEquals(2, hits.length());
|
||||
|
||||
searcher.close();
|
||||
directory.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue