Apache Solr open-source search software
testDemoBytes
Robert Muir40cb0384d297
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.javaindex d2a3ec95c8b..d0ddce2ef01 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@@ -431,7 +431,8 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {Source source = loadDirectSource();final byte[][] values = new byte[maxDoc][];for(int docID=0;docID<maxDoc;docID++) {- BytesRef value = source.getBytes(docID, null);+ // nocommit: who passes null!!!+ BytesRef value = source.getBytes(docID, new BytesRef());byte[] bytes = new byte[value.length];System.arraycopy(value.bytes, value.offset, bytes, 0, value.length);values[docID] = bytes;@@ -518,14 +519,6 @@ 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("trying to parse number: " + scratch.utf8ToString());- // nocommit- long seekPos = field.dataStartFilePointer;- byte wholeFile[] = new byte[(int)(in.length()-seekPos)];- IndexInput foo = in.clone();- foo.seek(seekPos);- foo.readBytes(wholeFile, 0, wholeFile.length);- System.out.println("rest: " + new String(wholeFile, 0, wholeFile.length, "UTF-8"));return field.minValue + decoder.parse(scratch.utf8ToString(), pos).longValue();} catch (IOException ioe) {throw new RuntimeException(ioe);diff --git a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.javaindex 46b2e4381a2..11b9e5884a5 100644--- a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java+++ b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java@@ -24,6 +24,7 @@ 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.StraightBytesDocValuesField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.DocValues;import org.apache.lucene.index.IndexReader;@@ -33,6 +34,7 @@ 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;import org.apache.lucene.util.LuceneTestCase;/**@@ -86,4 +88,48 @@ public class TestDemoDocValue extends LuceneTestCase {ireader.close();directory.close();}++ public void testDemoBytes() 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 StraightBytesDocValuesField("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();+ }}