You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2013/02/24 20:22:45 UTC

svn commit: r1449531 - in /lucene/dev/branches/LUCENE-2878/lucene/core/src: java/org/apache/lucene/search/ java/org/apache/lucene/search/posfilter/ test/org/apache/lucene/search/posfilter/

Author: romseygeek
Date: Sun Feb 24 19:22:44 2013
New Revision: 1449531

URL: http://svn.apache.org/r1449531
Log:
Add new ExactPhraseQuery using PositionFilteredScorer

Added:
    lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/BlockPhraseScorer.java   (with props)
    lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/ExactPhraseQuery.java   (with props)
    lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestExactPhraseQuery.java
      - copied, changed from r1447949, lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestBlockIntervalIterator.java
Removed:
    lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/ExactIntervalPhraseScorer.java
    lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestBlockIntervalIterator.java

Added: lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/BlockPhraseScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/BlockPhraseScorer.java?rev=1449531&view=auto
==============================================================================
--- lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/BlockPhraseScorer.java (added)
+++ lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/BlockPhraseScorer.java Sun Feb 24 19:22:44 2013
@@ -0,0 +1,67 @@
+package org.apache.lucene.search.posfilter;
+
+import org.apache.lucene.search.Scorer;
+
+import java.io.IOException;
+
+/**
+ * Copyright (c) 2013 Lemur Consulting Ltd.
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class BlockPhraseScorer extends PositionFilteredScorer {
+
+  private final Interval[] subIntervals;
+
+  public BlockPhraseScorer(Scorer filteredScorer) {
+    super(filteredScorer);
+    subIntervals = new Interval[subScorers.length];
+    for (int i = 0; i < subScorers.length; i++) {
+      subIntervals[i] = new Interval();
+    }
+  }
+
+  @Override
+  public void reset(int doc) throws IOException {
+    super.reset(doc);
+    for (int i = 0; i < subScorers.length; i++) {
+      subIntervals[i].reset();
+    }
+  }
+
+  @Override
+  protected int doNextPosition() throws IOException {
+    if (subScorers[0].nextPosition() == NO_MORE_POSITIONS)
+      return NO_MORE_POSITIONS;
+    subIntervals[0].update(subScorers[0]);
+    int i = 1;
+    while (i < subScorers.length) {
+      while (subIntervals[i].begin <= subIntervals[i - 1].end) {
+        if (subScorers[i].nextPosition() == NO_MORE_POSITIONS)
+          return NO_MORE_POSITIONS;
+        subIntervals[i].update(subScorers[i]);
+      }
+      if (subIntervals[i].begin == subIntervals[i - 1].end + 1) {
+        i++;
+      }
+      else {
+        if (subScorers[0].nextPosition() == NO_MORE_POSITIONS)
+          return NO_MORE_POSITIONS;
+        subIntervals[0].update(subScorers[0]);
+        i = 1;
+      }
+    }
+    current.update(subIntervals[0], subIntervals[subScorers.length - 1]);
+    return subScorers[0].startPosition();
+  }
+}

Added: lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/ExactPhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/ExactPhraseQuery.java?rev=1449531&view=auto
==============================================================================
--- lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/ExactPhraseQuery.java (added)
+++ lucene/dev/branches/LUCENE-2878/lucene/core/src/java/org/apache/lucene/search/posfilter/ExactPhraseQuery.java Sun Feb 24 19:22:44 2013
@@ -0,0 +1,50 @@
+package org.apache.lucene.search.posfilter;
+
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.Scorer;
+import org.apache.lucene.search.TermQuery;
+
+/**
+ * Copyright (c) 2013 Lemur Consulting Ltd.
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class ExactPhraseQuery extends PositionFilterQuery {
+
+  private final BooleanQuery innerBQ;
+
+  public ExactPhraseQuery() {
+    super(new BooleanQuery(), new ExactPhraseScorerFactory());
+    this.innerBQ = (BooleanQuery) innerQuery;
+  }
+
+  public void add(Term term) {
+    innerBQ.add(new TermQuery(term), BooleanClause.Occur.MUST);
+  }
+
+  private static class ExactPhraseScorerFactory implements ScorerFilterFactory {
+
+    @Override
+    public Scorer scorer(Scorer filteredScorer) {
+      return new BlockPhraseScorer(filteredScorer);
+    }
+
+    @Override
+    public String getName() {
+      return "ExactPhrase";
+    }
+  }
+
+}

Copied: lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestExactPhraseQuery.java (from r1447949, lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestBlockIntervalIterator.java)
URL: http://svn.apache.org/viewvc/lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestExactPhraseQuery.java?p2=lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestExactPhraseQuery.java&p1=lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestBlockIntervalIterator.java&r1=1447949&r2=1449531&rev=1449531&view=diff
==============================================================================
--- lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestBlockIntervalIterator.java (original)
+++ lucene/dev/branches/LUCENE-2878/lucene/core/src/test/org/apache/lucene/search/posfilter/TestExactPhraseQuery.java Sun Feb 24 19:22:44 2013
@@ -28,15 +28,19 @@ import org.apache.lucene.search.interval
 
 import java.io.IOException;
 
-public class TestBlockIntervalIterator extends IntervalTestBase {
+public class TestExactPhraseQuery extends IntervalTestBase {
   
   protected void addDocs(RandomIndexWriter writer) throws IOException {
     {
       Document doc = new Document();
       doc.add(newField(
           "field",
-          "Pease porridge hot! Pease porridge cold! Pease porridge in the pot nine days old! Some like it hot, some"
-              + " like it cold, Some like it in the pot nine days old! Pease porridge hot! Pease porridge cold!",
+        //  0       1      2     3     4       5     6      7      8   9  10   11   12   13
+          "Pease porridge hot! Pease porridge cold! Pease porridge in the pot nine days old! "
+        //  14   15  16 17    18   19  20  21    22   23  24 25 26  27   28   29   30
+        + "Some like it hot, some like it cold, Some like it in the pot nine days old! "
+        //  31      32     33    34     35     36
+        + "Pease porridge hot! Pease porridge cold!",
               TextField.TYPE_STORED));
       writer.addDocument(doc);
     }
@@ -45,53 +49,72 @@ public class TestBlockIntervalIterator e
       Document doc = new Document();
       doc.add(newField(
           "field",
-          "Pease porridge cold! Pease porridge hot! Pease porridge in the pot nine days old! Some like it cold, some"
-              + " like it hot, Some like it in the pot nine days old! Pease porridge cold! Pease porridge hot!",
+        //  0       1      2     3     4       5     6      7      8   9  10   11   12   13
+          "Pease porridge cold! Pease porridge hot! Pease porridge in the pot nine days old! "
+        //  14   15  16 17    18   19  20  21    22   23  24 25 26  27   28   29   30
+        + "Some like it cold, some like it hot, Some like it in the pot nine days old! "
+        //  31      32     33    34     35     36
+        + "Pease porridge cold! Pease porridge hot!",
           TextField.TYPE_STORED));
       writer.addDocument(doc);
     }
   }
 
-  public void testMatchingBlockIntervalFilter() throws IOException {
+  public void testOverlaps() throws IOException {
+    ExactPhraseQuery q = new ExactPhraseQuery();
+    q.add(new Term("field", "some"));
+    q.add(new Term("field", "like"));
+    q.add(new Term("field", "it"));
+    q.add(new Term("field", "cold,"));
+    q.add(new Term("field", "some"));
+    q.add(new Term("field", "like"));
+    checkIntervals(q, searcher, new int[][]{
+        { 0, 18, 23 },
+        { 1, 14, 19 }
+    });
+  }
 
-    BooleanQuery query = new BooleanQuery();
-    query.add(new BooleanClause(new TermQuery(new Term("field", "pease")), Occur.MUST));
-    query.add(new BooleanClause(new TermQuery(new Term("field", "porridge")), Occur.MUST));
-    query.add(new BooleanClause(new TermQuery(new Term("field", "hot!")), Occur.MUST));
-    IntervalFilterQuery filterQuery = new IntervalFilterQuery(query, new BlockIntervalFilter(false));
+  public void testMatching() throws IOException {
 
-    checkIntervals(filterQuery, searcher, new int[][]{
-        { 0, 0, 2, 31, 33 },
-        { 1, 3, 5, 34, 36 }
+    ExactPhraseQuery q = new ExactPhraseQuery();
+    q.add(new Term("field", "pease"));
+    q.add(new Term("field", "porridge"));
+    q.add(new Term("field", "hot!"));
+
+    checkIntervals(q, searcher, new int[][]{
+        {0, 0, 2, 31, 33},
+        {1, 3, 5, 34, 36}
     });
 
   }
 
-  public void testPartialMatchingBlockIntervalFilter() throws IOException {
+  public void testPartialMatching() throws IOException {
 
-    BooleanQuery query = new BooleanQuery();
-    query.add(new BooleanClause(new TermQuery(new Term("field", "pease")), Occur.MUST));
-    query.add(new BooleanClause(new TermQuery(new Term("field", "porridge")), Occur.MUST));
-    query.add(new BooleanClause(new TermQuery(new Term("field", "hot!")), Occur.MUST));
-    query.add(new BooleanClause(new TermQuery(new Term("field", "pease")), Occur.MUST));
-    query.add(new BooleanClause(new TermQuery(new Term("field", "porridge")), Occur.MUST));
-    query.add(new BooleanClause(new TermQuery(new Term("field", "cold!")), Occur.MUST));
-    IntervalFilterQuery filterQuery = new IntervalFilterQuery(query, new BlockIntervalFilter(false));
+    ExactPhraseQuery q = new ExactPhraseQuery();
+    q.add(new Term("field", "pease"));
+    q.add(new Term("field", "porridge"));
+    q.add(new Term("field", "hot!"));
+    q.add(new Term("field", "pease"));
+    q.add(new Term("field", "porridge"));
+    q.add(new Term("field", "cold!"));
 
-    checkIntervals(filterQuery, searcher, new int[][]{
-        { 0, 0, 5, 31, 36 },
+    checkIntervals(q, searcher, new int[][]{
+        {0, 0, 5, 31, 36},
     });
 
-
   }
 
-  public void testNonMatchingBlockIntervalFilter() throws IOException {
+  public void testNonMatching() throws IOException {
 
     BooleanQuery query = new BooleanQuery();
     query.add(new BooleanClause(new TermQuery(new Term("field", "pease")), Occur.MUST));
     query.add(new BooleanClause(new TermQuery(new Term("field", "hot!")), Occur.MUST));
     IntervalFilterQuery filterQuery = new IntervalFilterQuery(query, new BlockIntervalFilter());
 
+    ExactPhraseQuery q = new ExactPhraseQuery();
+    q.add(new Term("field", "pease"));
+    q.add(new Term("field", "hot!"));
+
     checkIntervals(filterQuery, searcher, new int[][]{});
 
   }