From 6eda60ad52c8c8cb64d6e236d7dcef68329f7dbd Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Sun, 18 Nov 2012 17:13:01 +0000 Subject: [PATCH] add tests git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4547@1410931 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/TestDemoDocValue.java | 96 +++++++++++++++++++ .../lucene/index/RandomIndexWriter.java | 2 + 2 files changed, 98 insertions(+) diff --git a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java index bde37935713..c881105b658 100644 --- a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java +++ b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java @@ -213,6 +213,102 @@ public class TestDemoDocValue extends LuceneTestCase { directory.close(); } + public void testDemoThreeFieldsMixed() 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("dv1", new BytesRef("hello hello"))); + doc.add(new PackedLongDocValuesField("dv2", 5)); + doc.add(new StraightBytesDocValuesField("dv3", 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); + BytesRef scratch = new BytesRef(); + // 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; + SortedDocValues dv = ireader.leaves().get(0).reader().getSortedDocValues("dv1"); + int ord = dv.getOrd(0); + dv.lookupOrd(ord, scratch); + assertEquals(new BytesRef("hello hello"), scratch); + NumericDocValues dv2 = ireader.leaves().get(0).reader().getNumericDocValues("dv2"); + assertEquals(5, dv2.get(hits.scoreDocs[i].doc)); + BinaryDocValues dv3 = ireader.leaves().get(0).reader().getBinaryDocValues("dv3"); + dv3.get(hits.scoreDocs[i].doc, scratch); + assertEquals(new BytesRef("hello world"), scratch); + } + + ireader.close(); + directory.close(); + } + + public void testDemoThreeFieldsMixed2() 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("dv1", new BytesRef("hello world"))); + doc.add(new SortedBytesDocValuesField("dv2", new BytesRef("hello hello"))); + doc.add(new PackedLongDocValuesField("dv3", 5)); + 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); + BytesRef scratch = new BytesRef(); + // 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; + SortedDocValues dv = ireader.leaves().get(0).reader().getSortedDocValues("dv2"); + int ord = dv.getOrd(0); + dv.lookupOrd(ord, scratch); + assertEquals(new BytesRef("hello hello"), scratch); + NumericDocValues dv2 = ireader.leaves().get(0).reader().getNumericDocValues("dv3"); + assertEquals(5, dv2.get(hits.scoreDocs[i].doc)); + BinaryDocValues dv3 = ireader.leaves().get(0).reader().getBinaryDocValues("dv1"); + dv3.get(hits.scoreDocs[i].doc, scratch); + assertEquals(new BytesRef("hello world"), scratch); + } + + ireader.close(); + directory.close(); + } + public void testTwoDocumentsNumeric() throws IOException { Analyzer analyzer = new MockAnalyzer(random()); diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/RandomIndexWriter.java b/lucene/test-framework/src/java/org/apache/lucene/index/RandomIndexWriter.java index cf6d3a8a0aa..7d1f102be28 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/index/RandomIndexWriter.java +++ b/lucene/test-framework/src/java/org/apache/lucene/index/RandomIndexWriter.java @@ -61,6 +61,8 @@ public class RandomIndexWriter implements Closeable { private boolean getReaderCalled; private final int fixedBytesLength; private final long docValuesFieldPrefix; + // nocommit: remove this test coverage hack once we have a + // good set of real tests. private volatile boolean doDocValues; private final Codec codec; // sugar