Index: lucene/suggest/build.xml
===================================================================
--- lucene/suggest/build.xml (revision 1553029)
+++ lucene/suggest/build.xml (working copy)
@@ -31,12 +31,16 @@
-
-
+
+
+
+
+
+
Index: lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentExpressionDictionary.java
===================================================================
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentExpressionDictionary.java (revision 1553029)
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentExpressionDictionary.java (working copy)
@@ -18,22 +18,17 @@
*/
import java.io.IOException;
-import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
-import java.util.Set;
import org.apache.lucene.document.NumericDocValuesField; // javadocs
-import org.apache.lucene.expressions.Expression;
-import org.apache.lucene.expressions.SimpleBindings;
-import org.apache.lucene.expressions.js.JavascriptCompiler;
+
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.index.StoredDocument;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
-import org.apache.lucene.search.SortField;
import org.apache.lucene.util.BytesRefIterator;
@@ -69,42 +64,7 @@
/**
* Creates a new dictionary with the contents of the fields named field
- * for the terms and computes the corresponding weights of the term by compiling the
- * user-defined weightExpression using the sortFields
- * bindings.
- */
- public DocumentExpressionDictionary(IndexReader reader, String field,
- String weightExpression, Set sortFields) {
- this(reader, field, weightExpression, sortFields, null);
- }
-
- /**
- * Creates a new dictionary with the contents of the fields named field
* for the terms, payloadField for the corresponding payloads
- * and computes the corresponding weights of the term by compiling the
- * user-defined weightExpression using the sortFields
- * bindings.
- */
- public DocumentExpressionDictionary(IndexReader reader, String field,
- String weightExpression, Set sortFields, String payload) {
- super(reader, field, null, payload);
- Expression expression = null;
- try {
- expression = JavascriptCompiler.compile(weightExpression);
- } catch (ParseException e) {
- throw new RuntimeException();
- }
- SimpleBindings bindings = new SimpleBindings();
- for (SortField sortField: sortFields) {
- bindings.add(sortField);
- }
-
- weightsValueSource = expression.getValueSource(bindings);
- }
-
- /**
- * Creates a new dictionary with the contents of the fields named field
- * for the terms, payloadField for the corresponding payloads
* and uses the weightsValueSource supplied to determine the
* score.
*/
Index: lucene/suggest/src/test/org/apache/lucene/search/suggest/DocumentExpressionDictionaryTest.java
===================================================================
--- lucene/suggest/src/test/org/apache/lucene/search/suggest/DocumentExpressionDictionaryTest.java (revision 1553029)
+++ lucene/suggest/src/test/org/apache/lucene/search/suggest/DocumentExpressionDictionaryTest.java (working copy)
@@ -18,6 +18,7 @@
*/
import java.io.IOException;
+import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -32,11 +33,15 @@
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
+import org.apache.lucene.expressions.Expression;
+import org.apache.lucene.expressions.SimpleBindings;
+import org.apache.lucene.expressions.js.JavascriptCompiler;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
+import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.queries.function.valuesource.DoubleConstValueSource;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.spell.Dictionary;
@@ -52,6 +57,20 @@
static final String WEIGHT_FIELD_NAME_2 = "w2";
static final String WEIGHT_FIELD_NAME_3 = "w3";
static final String PAYLOAD_FIELD_NAME = "p1";
+
+ public ValueSource fromExpression(String weightExpression, Set sortFields) {
+ Expression expression = null;
+ try {
+ expression = JavascriptCompiler.compile(weightExpression);
+ } catch (ParseException e) {
+ throw new RuntimeException();
+ }
+ SimpleBindings bindings = new SimpleBindings();
+ for (SortField sortField : sortFields) {
+ bindings.add(sortField);
+ }
+ return expression.getValueSource(bindings);
+ }
private Map generateIndexDocuments(int ndocs) {
Map docs = new HashMap<>();
@@ -86,7 +105,7 @@
sortFields.add(new SortField(WEIGHT_FIELD_NAME_1, SortField.Type.LONG));
sortFields.add(new SortField(WEIGHT_FIELD_NAME_2, SortField.Type.LONG));
sortFields.add(new SortField(WEIGHT_FIELD_NAME_3, SortField.Type.LONG));
- Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, "((w1 + w2) - w3)", sortFields, PAYLOAD_FIELD_NAME);
+ Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, fromExpression("((w1 + w2) - w3)", sortFields), PAYLOAD_FIELD_NAME);
InputIterator inputIterator = (InputIterator) dictionary.getWordsIterator();
assertNull(inputIterator.next());
@@ -115,7 +134,7 @@
sortFields.add(new SortField(WEIGHT_FIELD_NAME_1, SortField.Type.LONG));
sortFields.add(new SortField(WEIGHT_FIELD_NAME_2, SortField.Type.LONG));
sortFields.add(new SortField(WEIGHT_FIELD_NAME_3, SortField.Type.LONG));
- Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, "((w1 + w2) - w3)", sortFields, PAYLOAD_FIELD_NAME);
+ Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, fromExpression("((w1 + w2) - w3)", sortFields), PAYLOAD_FIELD_NAME);
InputIterator inputIterator = (InputIterator) dictionary.getWordsIterator();
BytesRef f;
while((f = inputIterator.next())!=null) {
@@ -150,7 +169,7 @@
sortFields.add(new SortField(WEIGHT_FIELD_NAME_1, SortField.Type.LONG));
sortFields.add(new SortField(WEIGHT_FIELD_NAME_2, SortField.Type.LONG));
sortFields.add(new SortField(WEIGHT_FIELD_NAME_3, SortField.Type.LONG));
- Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, "w1 + (0.2 * w2) - (w3 - w1)/2", sortFields);
+ Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, fromExpression("w1 + (0.2 * w2) - (w3 - w1)/2", sortFields));
InputIterator inputIterator = (InputIterator) dictionary.getWordsIterator();
BytesRef f;
while((f = inputIterator.next())!=null) {
@@ -205,7 +224,7 @@
Set sortFields = new HashSet();
sortFields.add(new SortField(WEIGHT_FIELD_NAME_1, SortField.Type.LONG));
sortFields.add(new SortField(WEIGHT_FIELD_NAME_2, SortField.Type.LONG));
- Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, "w2-w1", sortFields, PAYLOAD_FIELD_NAME);
+ Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, fromExpression("w2-w1", sortFields), PAYLOAD_FIELD_NAME);
InputIterator inputIterator = (InputIterator) dictionary.getWordsIterator();
BytesRef f;
while((f = inputIterator.next())!=null) {