Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

get basic sorted bytes example working

Michael McCandlessaedd8f11c6a5
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java
index 48e42ed4d34..021d43ca24e 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java
@@ -67,8 +67,8 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {
final static BytesRef MAXLENGTH = new BytesRef(" maxlength ");
final static BytesRef LENGTH = new BytesRef("length ");
// used for sorted bytes
- final static BytesRef NUMVALUES = new BytesRef(" numvalues");
- final static BytesRef ORDPATTERN = new BytesRef(" ordpattern");
+ final static BytesRef NUMVALUES = new BytesRef(" numvalues ");
+ final static BytesRef ORDPATTERN = new BytesRef(" ordpattern ");
@Override
public SimpleDVConsumer fieldsConsumer(SegmentWriteState state) throws IOException {
@@ -287,7 +287,7 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {
@Override
public void addDoc(int ord) throws IOException {
- SimpleTextUtil.write(data, encoder.format(ord), scratch);
+ SimpleTextUtil.write(data, ordEncoder.format(ord), scratch);
SimpleTextUtil.writeNewline(data);
}
};
@@ -397,6 +397,7 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {
assert startsWith(ORDPATTERN);
field.ordPattern = stripPrefix(ORDPATTERN);
field.dataStartFilePointer = data.getFilePointer();
+ data.seek(data.getFilePointer() + (9+field.pattern.length()+field.maxLength) * field.numValues + (1+field.ordPattern.length())*maxDoc);
// nocommit: we need to seek past the data section!!!!
} else if (DocValues.isFloat(dvType)) {
// nocommit
@@ -424,7 +425,6 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {
DocValues.Type dvType = field.fieldInfo.getDocValuesType();
if (DocValues.isNumber(dvType)) {
Source source = loadDirectSource();
- System.out.println(maxDoc);
long[] values = new long[maxDoc];
for(int docID=0;docID<maxDoc;docID++) {
values[docID] = source.getInt(docID);
@@ -522,7 +522,7 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {
// value from the wrong field ...
in.seek(field.dataStartFilePointer + (1+field.pattern.length())*docID);
SimpleTextUtil.readLine(in, scratch);
- System.out.println("parsing delta: " + scratch.utf8ToString());
+ //System.out.println("parsing delta: " + scratch.utf8ToString());
BigDecimal bd = (BigDecimal) decoder.parse(scratch.utf8ToString(), new ParsePosition(0));
return BigInteger.valueOf(field.minValue).add(bd.toBigIntegerExact()).longValue();
} catch (IOException ioe) {
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/SortedDocValuesConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/SortedDocValuesConsumer.java
index 4bb306ddf02..f0b3befe88d 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/SortedDocValuesConsumer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/SortedDocValuesConsumer.java
@@ -57,6 +57,11 @@ public abstract class SortedDocValuesConsumer {
int ord = -1;
SortedSource source;
BytesRef scratch = new BytesRef();
+
+ // nocommit can we factor out the compressed fields
+ // compression? ie we have a good idea "roughly" what
+ // the ord should be (linear projection) so we only
+ // need to encode the delta from that ...:
int[] segOrdToMergedOrd;
public BytesRef nextTerm() {
diff --git a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java
index 32e8d692cc6..3df68d0bb59 100644
--- a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java
+++ b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java
@@ -24,15 +24,16 @@ import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.PackedLongDocValuesField;
+import org.apache.lucene.document.SortedBytesDocValuesField;
import org.apache.lucene.document.StraightBytesDocValuesField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.StoredDocument;
import org.apache.lucene.index.Term;
-import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
@@ -196,4 +197,48 @@ public class TestDemoDocValue extends LuceneTestCase {
ireader.close();
directory.close();
}
+
+ public void testDemoSortedBytes() throws IOException {
+ Analyzer analyzer = new MockAnalyzer(random());
+
+ // Store the index in memory:
+ Directory directory = newDirectory();
+ // To store an index on disk, use this instead:
+ // Directory directory = FSDirectory.open(new File("/tmp/testindex"));
+ // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!1
+ IndexWriter iwriter = new IndexWriter(directory, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
+ Document doc = new Document();
+ String longTerm = "longtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongterm";
+ String text = "This is the text to be indexed. " + longTerm;
+ doc.add(newTextField("fieldname", text, Field.Store.YES));
+ doc.add(new SortedBytesDocValuesField("dv", new BytesRef("hello world")));
+ iwriter.addDocument(doc);
+ iwriter.close();
+
+ // Now search the index:
+ IndexReader ireader = DirectoryReader.open(directory); // read-only=true
+ IndexSearcher isearcher = new IndexSearcher(ireader);
+
+ assertEquals(1, isearcher.search(new TermQuery(new Term("fieldname", longTerm)), 1).totalHits);
+ Query query = new TermQuery(new Term("fieldname", "text"));
+ TopDocs hits = isearcher.search(query, null, 1);
+ assertEquals(1, hits.totalHits);
+ // Iterate through the results:
+ for (int i = 0; i < hits.scoreDocs.length; i++) {
+ StoredDocument hitDoc = isearcher.doc(hits.scoreDocs[i].doc);
+ assertEquals(text, hitDoc.get("fieldname"));
+ assert ireader.leaves().size() == 1;
+ DocValues dv = ireader.leaves().get(0).reader().docValues("dv");
+ assertEquals(new BytesRef("hello world"), dv.getSource().getBytes(hits.scoreDocs[i].doc, new BytesRef()));
+ }
+
+ // Test simple phrase query
+ PhraseQuery phraseQuery = new PhraseQuery();
+ phraseQuery.add(new Term("fieldname", "to"));
+ phraseQuery.add(new Term("fieldname", "be"));
+ assertEquals(1, isearcher.search(phraseQuery, null, 1).totalHits);
+
+ ireader.close();
+ directory.close();
+ }
}