lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rm...@apache.org
Subject svn commit: r1524815 - in /lucene/dev/branches/branch_4x: ./ lucene/ lucene/expressions/ lucene/expressions/src/java/org/apache/lucene/expressions/ lucene/expressions/src/test/org/apache/lucene/expressions/
Date Thu, 19 Sep 2013 19:50:39 GMT
Author: rmuir
Date: Thu Sep 19 19:50:39 2013
New Revision: 1524815

URL: http://svn.apache.org/r1524815
Log:
LUCENE-5226: Optimize SortField.needsScore for expressions

Modified:
    lucene/dev/branches/branch_4x/   (props changed)
    lucene/dev/branches/branch_4x/lucene/   (props changed)
    lucene/dev/branches/branch_4x/lucene/expressions/   (props changed)
    lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java
    lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java
    lucene/dev/branches/branch_4x/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java

Modified: lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java?rev=1524815&r1=1524814&r2=1524815&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java
(original)
+++ lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java
Thu Sep 19 19:50:39 2013
@@ -18,15 +18,14 @@ package org.apache.lucene.expressions;
 
 import java.io.IOException;
 
-import org.apache.lucene.queries.function.ValueSource;
 import org.apache.lucene.search.FieldComparator;
 import org.apache.lucene.search.SortField;
 
 /** A {@link SortField} which sorts documents by the evaluated value of an expression for
each document */
 class ExpressionSortField extends SortField {
-  private final ValueSource source;
+  private final ExpressionValueSource source;
 
-  ExpressionSortField(String name, ValueSource source, boolean reverse) {
+  ExpressionSortField(String name, ExpressionValueSource source, boolean reverse) {
     super(name, Type.CUSTOM, reverse);
     this.source = source;
   }
@@ -73,6 +72,6 @@ class ExpressionSortField extends SortFi
 
   @Override
   public boolean needsScores() {
-    return true; // TODO: maybe we can optimize by "figuring this out" somehow...
+    return source.needsScores();
   }
 }
\ No newline at end of file

Modified: lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java?rev=1524815&r1=1524814&r2=1524815&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java
(original)
+++ lucene/dev/branches/branch_4x/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java
Thu Sep 19 19:50:39 2013
@@ -85,4 +85,19 @@ final class ExpressionValueSource extend
   public boolean equals(Object obj) {
     return obj == this;
   }
+  
+  boolean needsScores() {
+    for (int i = 0; i < expression.variables.length; i++) {
+      String externalName = expression.variables[i];
+      ValueSource source = bindings.getValueSource(externalName);
+      if (source instanceof ScoreValueSource) {
+        return true;
+      } else if (source instanceof ExpressionValueSource) {
+        if (((ExpressionValueSource)source).needsScores()) {
+          return true;
+        }
+      }
+    }
+    return false;
+  }
 }

Modified: lucene/dev/branches/branch_4x/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java?rev=1524815&r1=1524814&r2=1524815&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java
(original)
+++ lucene/dev/branches/branch_4x/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java
Thu Sep 19 19:50:39 2013
@@ -66,4 +66,51 @@ public class TestExpressionSortField ext
     // same instance:
     assertEquals(sf1, sf1);
   }
+  
+  public void testNeedsScores() throws Exception {
+    SimpleBindings bindings = new SimpleBindings();
+    // refers to score directly
+    Expression exprA = JavascriptCompiler.compile("_score");
+    // constant
+    Expression exprB = JavascriptCompiler.compile("0");
+    // field
+    Expression exprC = JavascriptCompiler.compile("intfield");
+    
+    // score + constant
+    Expression exprD = JavascriptCompiler.compile("_score + 0");
+    // field + constant
+    Expression exprE = JavascriptCompiler.compile("intfield + 0");
+    
+    // expression + constant (score ref'd)
+    Expression exprF = JavascriptCompiler.compile("a + 0");
+    // expression + constant
+    Expression exprG = JavascriptCompiler.compile("e + 0");
+    
+    // several variables (score ref'd)
+    Expression exprH = JavascriptCompiler.compile("b / c + e * g - sqrt(f)");
+    // several variables
+    Expression exprI = JavascriptCompiler.compile("b / c + e * g");
+    
+    bindings.add(new SortField("_score", SortField.Type.SCORE));
+    bindings.add(new SortField("intfield", SortField.Type.INT));
+    bindings.add("a", exprA);
+    bindings.add("b", exprB);
+    bindings.add("c", exprC);
+    bindings.add("d", exprD);
+    bindings.add("e", exprE);
+    bindings.add("f", exprF);
+    bindings.add("g", exprG);
+    bindings.add("h", exprH);
+    bindings.add("i", exprI);
+    
+    assertTrue(exprA.getSortField(bindings, true).needsScores());
+    assertFalse(exprB.getSortField(bindings, true).needsScores());
+    assertFalse(exprC.getSortField(bindings, true).needsScores());
+    assertTrue(exprD.getSortField(bindings, true).needsScores());
+    assertFalse(exprE.getSortField(bindings, true).needsScores());
+    assertTrue(exprF.getSortField(bindings, true).needsScores());
+    assertFalse(exprG.getSortField(bindings, true).needsScores());
+    assertTrue(exprH.getSortField(bindings, true).needsScores());
+    assertFalse(exprI.getSortField(bindings, false).needsScores());
+  }
 }



Mime
View raw message