internal virtual void LoadDoc(IndexReader ir) { // beware of deleted docs in the future Document doc = ir.Document(rand.Next(ir.MaxDoc()), new AnonymousClassFieldSelector(this)); System.Collections.IList fields = doc.GetFields(); for (int i = 0; i < fields.Count; i++) { Fieldable f = (Fieldable) fields[i]; Enclosing_Instance.ValidateField(f); } }
public virtual void TestBinaryFieldInIndex() { Fieldable binaryFldStored = new Field("binaryStored", System.Text.UTF8Encoding.UTF8.GetBytes(binaryValStored), Field.Store.YES); Fieldable binaryFldCompressed = new Field("binaryCompressed", System.Text.UTF8Encoding.UTF8.GetBytes(binaryValCompressed), Field.Store.COMPRESS); Fieldable stringFldStored = new Field("stringStored", binaryValStored, Field.Store.YES, Field.Index.NO, Field.TermVector.NO); Fieldable stringFldCompressed = new Field("stringCompressed", binaryValCompressed, Field.Store.COMPRESS, Field.Index.NO, Field.TermVector.NO); try { // binary fields with store off are not allowed new Field("fail", System.Text.UTF8Encoding.UTF8.GetBytes(binaryValCompressed), Field.Store.NO); Assert.Fail(); } catch (System.ArgumentException iae) { ; } Document doc = new Document(); doc.Add(binaryFldStored); doc.Add(binaryFldCompressed); doc.Add(stringFldStored); doc.Add(stringFldCompressed); /** test for field count */ Assert.AreEqual(4, doc.fields_ForNUnit.Count); /** add the doc to a ram index */ MockRAMDirectory dir = new MockRAMDirectory(); IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); writer.AddDocument(doc); writer.Close(); /** open a reader and fetch the document */ IndexReader reader = IndexReader.Open(dir); Document docFromReader = reader.Document(0); Assert.IsTrue(docFromReader != null); /** fetch the binary stored field and compare it's content with the original one */ System.String binaryFldStoredTest = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(docFromReader.GetBinaryValue("binaryStored"))); Assert.IsTrue(binaryFldStoredTest.Equals(binaryValStored)); /** fetch the binary compressed field and compare it's content with the original one */ System.String binaryFldCompressedTest = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(docFromReader.GetBinaryValue("binaryCompressed"))); Assert.IsTrue(binaryFldCompressedTest.Equals(binaryValCompressed)); /** fetch the string field and compare it's content with the original one */ System.String stringFldStoredTest = docFromReader.Get("stringStored"); Assert.IsTrue(stringFldStoredTest.Equals(binaryValStored)); /** fetch the compressed string field and compare it's content with the original one */ System.String stringFldCompressedTest = docFromReader.Get("stringCompressed"); Assert.IsTrue(stringFldCompressedTest.Equals(binaryValCompressed)); /** delete the document from index */ reader.DeleteDocument(0); Assert.AreEqual(0, reader.NumDocs()); reader.Close(); dir.Close(); }
//convenience method public static TokenStream GetTokenStream(IndexReader reader, int docId, System.String field, Analyzer analyzer) { Document doc = reader.Document(docId); System.String contents = doc.Get(field); if (contents == null) { throw new System.ArgumentException("Field " + field + " in document #" + docId + " is not stored and cannot be analyzed"); } return analyzer.TokenStream(field, new System.IO.StringReader(contents)); }
public virtual void TestQuery() { RAMDirectory dir = new RAMDirectory(); IndexWriter iw = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED); iw.SetMaxBufferedDocs(2); // force multi-segment AddDoc("one", iw, 1f); AddDoc("two", iw, 20f); AddDoc("three four", iw, 300f); iw.Close(); IndexReader ir = IndexReader.Open(dir, false); IndexSearcher is_Renamed = new IndexSearcher(ir); ScoreDoc[] hits; // assert with norms scoring turned off hits = is_Renamed.Search(new MatchAllDocsQuery(), null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length); Assert.AreEqual(ir.Document(hits[0].Doc).Get("key"), "one"); Assert.AreEqual(ir.Document(hits[1].Doc).Get("key"), "two"); Assert.AreEqual(ir.Document(hits[2].Doc).Get("key"), "three four"); // assert with norms scoring turned on MatchAllDocsQuery normsQuery = new MatchAllDocsQuery("key"); hits = is_Renamed.Search(normsQuery, null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length); Assert.AreEqual(ir.Document(hits[0].Doc).Get("key"), "three four"); Assert.AreEqual(ir.Document(hits[1].Doc).Get("key"), "two"); Assert.AreEqual(ir.Document(hits[2].Doc).Get("key"), "one"); // change norm & retest ir.SetNorm(0, "key", 400f); normsQuery = new MatchAllDocsQuery("key"); hits = is_Renamed.Search(normsQuery, null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length); Assert.AreEqual(ir.Document(hits[0].Doc).Get("key"), "one"); Assert.AreEqual(ir.Document(hits[1].Doc).Get("key"), "three four"); Assert.AreEqual(ir.Document(hits[2].Doc).Get("key"), "two"); // some artificial queries to trigger the use of skipTo(): BooleanQuery bq = new BooleanQuery(); bq.Add(new MatchAllDocsQuery(), Occur.MUST); bq.Add(new MatchAllDocsQuery(), Occur.MUST); hits = is_Renamed.Search(bq, null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length); bq = new BooleanQuery(); bq.Add(new MatchAllDocsQuery(), Occur.MUST); bq.Add(new TermQuery(new Term("key", "three")), Occur.MUST); hits = is_Renamed.Search(bq, null, 1000).ScoreDocs; Assert.AreEqual(1, hits.Length); // delete a document: is_Renamed.IndexReader.DeleteDocument(0); hits = is_Renamed.Search(new MatchAllDocsQuery(), null, 1000).ScoreDocs; Assert.AreEqual(2, hits.Length); // test parsable toString() QueryParser qp = new QueryParser(Util.Version.LUCENE_CURRENT, "key", analyzer); hits = is_Renamed.Search(qp.Parse(new MatchAllDocsQuery().ToString()), null, 1000).ScoreDocs; Assert.AreEqual(2, hits.Length); // test parsable toString() with non default boost Query maq = new MatchAllDocsQuery(); maq.Boost = 2.3f; Query pq = qp.Parse(maq.ToString()); hits = is_Renamed.Search(pq, null, 1000).ScoreDocs; Assert.AreEqual(2, hits.Length); is_Renamed.Close(); ir.Close(); dir.Close(); }