blob: a1655505b319979431689a2eb0d25efbd03d7705 [file] [log] [blame]
From 7570ff1e3fb6c49991f92b1409bdb210b4c4b951 Mon Sep 17 00:00:00 2001
From: Varun Thacker <varunthacker1989@gmail.com>
Date: Fri, 18 Jul 2014 09:06:56 +0530
Subject: [PATCH] SOLR-6210
diff --git lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentDictionary.java lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentDictionary.java
index ce1d60e..b4114f0 100644
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentDictionary.java
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentDictionary.java
@@ -17,7 +17,9 @@ package org.apache.lucene.search.suggest;
* limitations under the License.
*/
import java.io.IOException;
+import java.util.ArrayList;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import org.apache.lucene.index.IndexReader;
@@ -115,6 +117,7 @@ public class DocumentDictionary implements Dictionary {
private BytesRef currentPayload = null;
private Set<BytesRef> currentContexts;
private final NumericDocValues weightValues;
+ private List<BytesRef> terms = new ArrayList<>();
/**
* Creates an iterator over term, weight and payload fields from the lucene
@@ -138,7 +141,11 @@ public class DocumentDictionary implements Dictionary {
@Override
public BytesRef next() throws IOException {
while (currentDocId < docCount) {
- currentDocId++;
+ //currentDocId++;
+ if(terms.size() == 0) {
+ currentDocId++;
+ }
+
if (liveDocs != null && !liveDocs.get(currentDocId)) {
continue;
}
@@ -168,11 +175,24 @@ public class DocumentDictionary implements Dictionary {
}
}
- StorableField fieldVal = doc.getField(field);
- if (fieldVal == null || (fieldVal.binaryValue() == null && fieldVal.stringValue() == null)) {
+
+
+ if(terms.size() == 0) {
+ //List is empty - Add all terms
+ StorableField[] fieldVals = doc.getFields(field);
+ for(StorableField fieldVal : fieldVals) {
+ if (fieldVal != null && (fieldVal.binaryValue() != null || fieldVal.stringValue() != null)) {
+ terms.add((fieldVal.stringValue() != null) ? new BytesRef(fieldVal.stringValue()) : fieldVal.binaryValue());
+ }
+ }
+ }
+
+ if(terms.size() != 0) {
+ tempTerm = terms.remove(0);
+ } else {
continue;
}
- tempTerm = (fieldVal.stringValue() != null) ? new BytesRef(fieldVal.stringValue()) : fieldVal.binaryValue();
+
currentPayload = tempPayload;
currentContexts = tempContexts;
diff --git lucene/suggest/src/test/org/apache/lucene/search/suggest/DocumentDictionaryTest.java lucene/suggest/src/test/org/apache/lucene/search/suggest/DocumentDictionaryTest.java
index 9cb7944..4474747 100644
--- lucene/suggest/src/test/org/apache/lucene/search/suggest/DocumentDictionaryTest.java
+++ lucene/suggest/src/test/org/apache/lucene/search/suggest/DocumentDictionaryTest.java
@@ -23,7 +23,6 @@ import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.StorableField;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.spell.Dictionary;
-import org.apache.lucene.search.suggest.DocumentDictionary;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LuceneTestCase;
@@ -165,6 +164,38 @@ public class DocumentDictionaryTest extends LuceneTestCase {
ir.close();
dir.close();
}
+
+ @Test
+ public void testMultiValuedBasic() throws IOException {
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()));
+ iwc.setMergePolicy(newLogMergePolicy());
+ RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
+ List<String> values = new ArrayList<>();
+ values.add("foo");
+ values.add("bar");
+
+ Document doc = new Document();
+ doc.add(new TextField(FIELD_NAME, values.get(0), Field.Store.YES) );
+ doc.add(new TextField(FIELD_NAME, values.get(1), Field.Store.YES) );
+ doc.add(new StoredField(WEIGHT_FIELD_NAME, 100d));
+ writer.addDocument(doc);
+
+ writer.commit();
+ writer.shutdown();
+
+ IndexReader ir = DirectoryReader.open(dir);
+ Dictionary dictionary = new DocumentDictionary(ir, FIELD_NAME, WEIGHT_FIELD_NAME, PAYLOAD_FIELD_NAME);
+ InputIterator inputIterator = dictionary.getEntryIterator();
+ BytesRef f;
+ int i=0;
+ while((f = inputIterator.next())!=null) {
+ assertEquals(values.get(i), f.utf8ToString());
+ }
+
+ ir.close();
+ dir.close();
+ }
@Test
public void testWithoutPayload() throws IOException {
--
1.8.5.2 (Apple Git-48)