lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gol...@apache.org
Subject cvs commit: jakarta-lucene/src/java/org/apache/lucene/search PhraseScorer.java ConjunctionScorer.java
Date Wed, 21 Apr 2004 15:23:29 GMT
goller      2004/04/21 08:23:29

  Modified:    src/test/org/apache/lucene/search TestPhraseQuery.java
               src/java/org/apache/lucene/search PhraseScorer.java
                        ConjunctionScorer.java
  Log:
  Bug in PhraseScorer and ConjunctionScorer
  skipTo implementation fixed.
  
  Unit test for this bug added.
  
  Revision  Changes    Path
  1.6       +70 -14    jakarta-lucene/src/test/org/apache/lucene/search/TestPhraseQuery.java
  
  Index: TestPhraseQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/search/TestPhraseQuery.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestPhraseQuery.java	20 Apr 2004 13:39:08 -0000	1.5
  +++ TestPhraseQuery.java	21 Apr 2004 15:23:29 -0000	1.6
  @@ -34,24 +34,16 @@
   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 void tearDown() throws Exception {
       searcher.close();
  +    directory.close();
     }
   
     public void testNotCloseEnough() throws Exception {
  @@ -186,16 +179,79 @@
     }
     
     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();
     }
   }
  
  
  
  1.13      +6 -2      jakarta-lucene/src/java/org/apache/lucene/search/PhraseScorer.java
  
  Index: PhraseScorer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/PhraseScorer.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- PhraseScorer.java	7 Apr 2004 16:40:34 -0000	1.12
  +++ PhraseScorer.java	21 Apr 2004 15:23:29 -0000	1.13
  @@ -63,7 +63,11 @@
       } 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 @@
       }
       if (more)
         sort();                                     // re-sort
  -    return more;
  +    return doNext();
     }
   
     protected abstract float phraseFreq() throws IOException;
  
  
  
  1.3       +6 -3      jakarta-lucene/src/java/org/apache/lucene/search/ConjunctionScorer.java
  
  Index: ConjunctionScorer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/ConjunctionScorer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ConjunctionScorer.java	29 Mar 2004 22:48:03 -0000	1.2
  +++ ConjunctionScorer.java	21 Apr 2004 15:23:29 -0000	1.3
  @@ -46,12 +46,14 @@
       } 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 @@
       }
       if (more)
         sortScorers();                              // re-sort scorers
  -    return more;
  +    return doNext();
     }
   
     public float score() throws IOException {
  @@ -96,6 +98,7 @@
       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();
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message