You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ar...@apache.org on 2015/05/28 19:46:56 UTC

svn commit: r1682290 - in /lucene/dev/trunk/lucene: core/src/java/org/apache/lucene/util/fst/Util.java suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java

Author: areek
Date: Thu May 28 17:46:55 2015
New Revision: 1682290

URL: http://svn.apache.org/r1682290
Log:
LUCENE-6510: take path boosts into account when polling TopNSearcher queue

Modified:
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/fst/Util.java
    lucene/dev/trunk/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/fst/Util.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/fst/Util.java?rev=1682290&r1=1682289&r2=1682290&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/fst/Util.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/fst/Util.java Thu May 28 17:46:55 2015
@@ -273,7 +273,7 @@ public final class Util {
 
     @Override
     public String toString() {
-      return "input=" + input + " cost=" + cost + "context=" + context + "boost=" + boost;
+      return "input=" + input.get() + " cost=" + cost + "context=" + context + "boost=" + boost;
     }
   }
 
@@ -307,7 +307,8 @@ public final class Util {
 
     private final FST.Arc<T> scratchArc = new FST.Arc<>();
     
-    final Comparator<T> comparator;
+    private final Comparator<T> comparator;
+    private final Comparator<FSTPath<T>> pathComparator;
 
     TreeSet<FSTPath<T>> queue = null;
 
@@ -329,7 +330,7 @@ public final class Util {
       this.topN = topN;
       this.maxQueueDepth = maxQueueDepth;
       this.comparator = comparator;
-
+      this.pathComparator = pathComparator;
       queue = new TreeSet<>(pathComparator);
     }
 
@@ -343,7 +344,7 @@ public final class Util {
 
       if (queue.size() == maxQueueDepth) {
         FSTPath<T> bottom = queue.last();
-        int comp = comparator.compare(cost, bottom.cost);
+        int comp = pathComparator.compare(path, bottom);
         if (comp > 0) {
           // Doesn't compete
           return;

Modified: lucene/dev/trunk/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java?rev=1682290&r1=1682289&r2=1682290&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java (original)
+++ lucene/dev/trunk/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java Thu May 28 17:46:55 2015
@@ -478,56 +478,55 @@ public class TestContextQuery extends Lu
   @Test
   public void testRandomContextQueryScoring() throws Exception {
     Analyzer analyzer = new MockAnalyzer(random());
-    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwcWithSuggestField(analyzer, "suggest_field"));
-    int numSuggestions = atLeast(20);
-    int numContexts = atLeast(5);
+    try(RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwcWithSuggestField(analyzer, "suggest_field"))) {
+      int numSuggestions = atLeast(20);
+      int numContexts = atLeast(5);
 
-    Set<Integer> seenWeights = new HashSet<>();
-    List<Entry> expectedEntries = new ArrayList<>();
-    List<CharSequence> contexts = new ArrayList<>();
-    for (int i = 1; i <= numContexts; i++) {
-      CharSequence context = TestUtil.randomSimpleString(random(), 10) + i;
-      contexts.add(context);
-      for (int j = 1; j <= numSuggestions; j++) {
-        String suggestion = "sugg_" + TestUtil.randomSimpleString(random(), 10) + j;
-        int weight = TestUtil.nextInt(random(), 1, 1000 * numContexts * numSuggestions);
-        while (seenWeights.contains(weight)) {
-          weight = TestUtil.nextInt(random(), 1, 1000 * numContexts * numSuggestions);
+      Set<Integer> seenWeights = new HashSet<>();
+      List<Entry> expectedEntries = new ArrayList<>();
+      List<CharSequence> contexts = new ArrayList<>();
+      for (int i = 1; i <= numContexts; i++) {
+        CharSequence context = TestUtil.randomSimpleString(random(), 10) + i;
+        contexts.add(context);
+        for (int j = 1; j <= numSuggestions; j++) {
+          String suggestion = "sugg_" + TestUtil.randomSimpleString(random(), 10) + j;
+          int weight = TestUtil.nextInt(random(), 1, 1000 * numContexts * numSuggestions);
+          while (seenWeights.contains(weight)) {
+            weight = TestUtil.nextInt(random(), 1, 1000 * numContexts * numSuggestions);
+          }
+          seenWeights.add(weight);
+          Document document = new Document();
+          document.add(new ContextSuggestField("suggest_field", Collections.singletonList(context), suggestion, weight));
+          iw.addDocument(document);
+          expectedEntries.add(new Entry(suggestion, context.toString(), i * weight));
+        }
+        if (rarely()) {
+          iw.commit();
         }
-        seenWeights.add(weight);
-        Document document = new Document();
-        document.add(new ContextSuggestField("suggest_field", Collections.singletonList(context), suggestion, weight));
-        iw.addDocument(document);
-        expectedEntries.add(new Entry(suggestion, context.toString(), i * weight));
-      }
-      if (rarely()) {
-        iw.commit();
       }
-    }
-    Entry[] expectedResults = expectedEntries.toArray(new Entry[expectedEntries.size()]);
+      Entry[] expectedResults = expectedEntries.toArray(new Entry[expectedEntries.size()]);
 
-    ArrayUtil.introSort(expectedResults, new Comparator<Entry>() {
-      @Override
-      public int compare(Entry o1, Entry o2) {
-        int cmp = Float.compare(o2.value, o1.value);
-        if (cmp != 0) {
-          return cmp;
-        } else {
-          return o1.output.compareTo(o2.output);
+      ArrayUtil.introSort(expectedResults, new Comparator<Entry>() {
+        @Override
+        public int compare(Entry o1, Entry o2) {
+          int cmp = Float.compare(o2.value, o1.value);
+          if (cmp != 0) {
+            return cmp;
+          } else {
+            return o1.output.compareTo(o2.output);
+          }
         }
-      }
-    });
+      });
 
-    DirectoryReader reader = iw.getReader();
-    SuggestIndexSearcher suggestIndexSearcher = new SuggestIndexSearcher(reader);
-    ContextQuery query = new ContextQuery(new PrefixCompletionQuery(analyzer, new Term("suggest_field", "sugg")));
-    for (int i = 0; i < contexts.size(); i++) {
-      query.addContext(contexts.get(i), i + 1);
+      try(DirectoryReader reader = iw.getReader()) {
+        SuggestIndexSearcher suggestIndexSearcher = new SuggestIndexSearcher(reader);
+        ContextQuery query = new ContextQuery(new PrefixCompletionQuery(analyzer, new Term("suggest_field", "sugg")));
+        for (int i = 0; i < contexts.size(); i++) {
+          query.addContext(contexts.get(i), i + 1);
+        }
+        TopSuggestDocs suggest = suggestIndexSearcher.suggest(query, 4);
+        assertSuggestions(suggest, Arrays.copyOfRange(expectedResults, 0, 4));
+      }
     }
-    TopSuggestDocs suggest = suggestIndexSearcher.suggest(query, 4);
-    assertSuggestions(suggest, Arrays.copyOfRange(expectedResults, 0, 4));
-
-    reader.close();
-    iw.close();
   }
 }