Index: lucene/suggest/build.xml
===================================================================
--- lucene/suggest/build.xml (revision 1553029)
+++ lucene/suggest/build.xml (working copy)
@@ -31,13 +31,10 @@
-
-
-
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)
@@ -20,11 +20,9 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
-import java.util.Set;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
@@ -37,8 +35,10 @@
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.queries.function.valuesource.LongFieldSource;
+import org.apache.lucene.queries.function.valuesource.SumFloatFunction;
import org.apache.lucene.search.spell.Dictionary;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
@@ -52,7 +52,7 @@
static final String WEIGHT_FIELD_NAME_2 = "w2";
static final String WEIGHT_FIELD_NAME_3 = "w3";
static final String PAYLOAD_FIELD_NAME = "p1";
-
+
private Map generateIndexDocuments(int ndocs) {
Map docs = new HashMap<>();
for(int i = 0; i < ndocs ; i++) {
@@ -82,11 +82,7 @@
writer.commit();
writer.close();
IndexReader ir = DirectoryReader.open(dir);
- 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));
- 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, new DoubleConstValueSource(10), PAYLOAD_FIELD_NAME);
InputIterator inputIterator = (InputIterator) dictionary.getWordsIterator();
assertNull(inputIterator.next());
@@ -111,11 +107,8 @@
writer.close();
IndexReader ir = DirectoryReader.open(dir);
- 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));
- 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);
+ ValueSource[] toAdd = new ValueSource[] {new LongFieldSource(WEIGHT_FIELD_NAME_1), new LongFieldSource(WEIGHT_FIELD_NAME_2), new LongFieldSource(WEIGHT_FIELD_NAME_3)};
+ Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, new SumFloatFunction(toAdd), PAYLOAD_FIELD_NAME);
InputIterator inputIterator = (InputIterator) dictionary.getWordsIterator();
BytesRef f;
while((f = inputIterator.next())!=null) {
@@ -124,7 +117,7 @@
long w2 = doc.getField(WEIGHT_FIELD_NAME_2).numericValue().longValue();
long w3 = doc.getField(WEIGHT_FIELD_NAME_3).numericValue().longValue();
assertTrue(f.equals(new BytesRef(doc.get(FIELD_NAME))));
- assertEquals(inputIterator.weight(), (w1 + w2) - w3);
+ assertEquals(inputIterator.weight(), (w1 + w2 + w3));
assertTrue(inputIterator.payload().equals(doc.getField(PAYLOAD_FIELD_NAME).binaryValue()));
}
assertTrue(docs.isEmpty());
@@ -146,11 +139,8 @@
writer.close();
IndexReader ir = DirectoryReader.open(dir);
- 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));
- 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);
+ ValueSource[] toAdd = new ValueSource[] {new LongFieldSource(WEIGHT_FIELD_NAME_1), new LongFieldSource(WEIGHT_FIELD_NAME_2), new LongFieldSource(WEIGHT_FIELD_NAME_3)};
+ Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, new SumFloatFunction(toAdd));
InputIterator inputIterator = (InputIterator) dictionary.getWordsIterator();
BytesRef f;
while((f = inputIterator.next())!=null) {
@@ -159,7 +149,7 @@
long w2 = doc.getField(WEIGHT_FIELD_NAME_2).numericValue().longValue();
long w3 = doc.getField(WEIGHT_FIELD_NAME_3).numericValue().longValue();
assertTrue(f.equals(new BytesRef(doc.get(FIELD_NAME))));
- assertEquals(inputIterator.weight(), (long)(w1 + (0.2 * w2) - (w3 - w1)/2));
+ assertEquals(inputIterator.weight(), (w1 + w2 + w3));
assertEquals(inputIterator.payload(), null);
}
assertTrue(docs.isEmpty());
@@ -202,10 +192,9 @@
IndexReader ir = DirectoryReader.open(dir);
assertTrue("NumDocs should be > 0 but was " + ir.numDocs(), ir.numDocs() > 0);
assertEquals(ir.numDocs(), docs.size());
- 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);
+ ValueSource[] toAdd = new ValueSource[] {new LongFieldSource(WEIGHT_FIELD_NAME_1), new LongFieldSource(WEIGHT_FIELD_NAME_2)};
+
+ Dictionary dictionary = new DocumentExpressionDictionary(ir, FIELD_NAME, new SumFloatFunction(toAdd), PAYLOAD_FIELD_NAME);
InputIterator inputIterator = (InputIterator) dictionary.getWordsIterator();
BytesRef f;
while((f = inputIterator.next())!=null) {
@@ -213,7 +202,7 @@
long w1 = doc.getField(WEIGHT_FIELD_NAME_1).numericValue().longValue();
long w2 = doc.getField(WEIGHT_FIELD_NAME_2).numericValue().longValue();
assertTrue(f.equals(new BytesRef(doc.get(FIELD_NAME))));
- assertEquals(inputIterator.weight(), w2-w1);
+ assertEquals(inputIterator.weight(), w2+w1);
assertTrue(inputIterator.payload().equals(doc.getField(PAYLOAD_FIELD_NAME).binaryValue()));
}
assertTrue(docs.isEmpty());