public virtual void TestRangeFilterRand() { IndexReader reader = signedIndexReader; IndexSearcher search = NewSearcher(reader); string minRP = Pad(signedIndexDir.minR); string maxRP = Pad(signedIndexDir.maxR); int numDocs = reader.NumDocs; Assert.AreEqual(numDocs, 1 + maxId - minId, "num of docs"); ScoreDoc[] result; Query q = new TermQuery(new Term("body", "body")); // test extremes, bounded on both ends result = search.Search(q, TermRangeFilter.NewStringRange("rand", minRP, maxRP, T, T), numDocs).ScoreDocs; Assert.AreEqual(numDocs, result.Length, "find all"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", minRP, maxRP, T, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 1, result.Length, "all but biggest"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", minRP, maxRP, F, T), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 1, result.Length, "all but smallest"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", minRP, maxRP, F, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 2, result.Length, "all but extremes"); // unbounded result = search.Search(q, TermRangeFilter.NewStringRange("rand", minRP, null, T, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs, result.Length, "smallest and up"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", null, maxRP, F, T), numDocs).ScoreDocs; Assert.AreEqual(numDocs, result.Length, "biggest and down"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", minRP, null, F, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 1, result.Length, "not smallest, but up"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", null, maxRP, F, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 1, result.Length, "not biggest, but down"); // very small sets result = search.Search(q, TermRangeFilter.NewStringRange("rand", minRP, minRP, F, F), numDocs).ScoreDocs; Assert.AreEqual(0, result.Length, "min,min,F,F"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", maxRP, maxRP, F, F), numDocs).ScoreDocs; Assert.AreEqual(0, result.Length, "max,max,F,F"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", minRP, minRP, T, T), numDocs).ScoreDocs; Assert.AreEqual(1, result.Length, "min,min,T,T"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", null, minRP, F, T), numDocs).ScoreDocs; Assert.AreEqual(1, result.Length, "nul,min,F,T"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", maxRP, maxRP, T, T), numDocs).ScoreDocs; Assert.AreEqual(1, result.Length, "max,max,T,T"); result = search.Search(q, TermRangeFilter.NewStringRange("rand", maxRP, null, T, F), numDocs).ScoreDocs; Assert.AreEqual(1, result.Length, "max,nul,T,T"); }
public override void BeforeClass() // LUCENENET specific - renamed from TestBefore() to ensure calling order vs base class { base.BeforeClass(); // create an index Directory indexStore = NewDirectory(); RandomIndexWriter writer = new RandomIndexWriter(Random, indexStore); long now = J2N.Time.CurrentTimeMilliseconds(); Document doc = new Document(); // add time that is in the past doc.Add(NewStringField("datefield", DateTools.TimeToString(now - 1000, DateResolution.MILLISECOND), Field.Store.YES)); doc.Add(NewTextField("body", "Today is a very sunny day in New York City", Field.Store.YES)); writer.AddDocument(doc); IndexReader reader = writer.GetReader(); writer.Dispose(); IndexSearcher searcher = NewSearcher(reader); // filter that should preserve matches // DateFilter df1 = DateFilter.Before("datefield", now); TermRangeFilter df1 = TermRangeFilter.NewStringRange("datefield", DateTools.TimeToString(now - 2000, DateResolution.MILLISECOND), DateTools.TimeToString(now, DateResolution.MILLISECOND), false, true); // filter that should discard matches // DateFilter df2 = DateFilter.Before("datefield", now - 999999); TermRangeFilter df2 = TermRangeFilter.NewStringRange("datefield", DateTools.TimeToString(0, DateResolution.MILLISECOND), DateTools.TimeToString(now - 2000, DateResolution.MILLISECOND), true, false); // search something that doesn't exist with DateFilter Query query1 = new TermQuery(new Term("body", "NoMatchForthis")); // search for something that does exists Query query2 = new TermQuery(new Term("body", "sunny")); ScoreDoc[] result; // ensure that queries return expected results without DateFilter first result = searcher.Search(query1, null, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query2, null, 1000).ScoreDocs; Assert.AreEqual(1, result.Length); // run queries with DateFilter result = searcher.Search(query1, df1, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query1, df2, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query2, df1, 1000).ScoreDocs; Assert.AreEqual(1, result.Length); result = searcher.Search(query2, df2, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); reader.Dispose(); indexStore.Dispose(); }
public virtual void TestBefore() { // create an index Directory indexStore = NewDirectory(); RandomIndexWriter writer = new RandomIndexWriter(Random(), indexStore, Similarity, TimeZone); long now = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond; Document doc = new Document(); // add time that is in the past doc.Add(NewStringField("datefield", DateTools.TimeToString(now - 1000, DateTools.Resolution.MILLISECOND), Field.Store.YES)); doc.Add(NewTextField("body", "Today is a very sunny day in New York City", Field.Store.YES)); writer.AddDocument(doc); IndexReader reader = writer.Reader; writer.Dispose(); IndexSearcher searcher = NewSearcher(reader); // filter that should preserve matches // DateFilter df1 = DateFilter.Before("datefield", now); TermRangeFilter df1 = TermRangeFilter.NewStringRange("datefield", DateTools.TimeToString(now - 2000, DateTools.Resolution.MILLISECOND), DateTools.TimeToString(now, DateTools.Resolution.MILLISECOND), false, true); // filter that should discard matches // DateFilter df2 = DateFilter.Before("datefield", now - 999999); TermRangeFilter df2 = TermRangeFilter.NewStringRange("datefield", DateTools.TimeToString(0, DateTools.Resolution.MILLISECOND), DateTools.TimeToString(now - 2000, DateTools.Resolution.MILLISECOND), true, false); // search something that doesn't exist with DateFilter Query query1 = new TermQuery(new Term("body", "NoMatchForthis")); // search for something that does exists Query query2 = new TermQuery(new Term("body", "sunny")); ScoreDoc[] result; // ensure that queries return expected results without DateFilter first result = searcher.Search(query1, null, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query2, null, 1000).ScoreDocs; Assert.AreEqual(1, result.Length); // run queries with DateFilter result = searcher.Search(query1, df1, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query1, df2, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query2, df1, 1000).ScoreDocs; Assert.AreEqual(1, result.Length); result = searcher.Search(query2, df2, 1000).ScoreDocs; Assert.AreEqual(0, result.Length); reader.Dispose(); indexStore.Dispose(); }
public virtual void TestCSQ() { Query q1 = new ConstantScoreQuery(new TermQuery(new Term("a", "b"))); Query q2 = new ConstantScoreQuery(new TermQuery(new Term("a", "c"))); Query q3 = new ConstantScoreQuery(TermRangeFilter.NewStringRange("a", "b", "c", true, true)); QueryUtils.Check(q1); QueryUtils.Check(q2); QueryUtils.CheckEqual(q1, q1); QueryUtils.CheckEqual(q2, q2); QueryUtils.CheckEqual(q3, q3); QueryUtils.CheckUnequal(q1, q2); QueryUtils.CheckUnequal(q2, q3); QueryUtils.CheckUnequal(q1, q3); QueryUtils.CheckUnequal(q1, new TermQuery(new Term("a", "b"))); }
public virtual void TestRangeFilterId() { IndexReader reader = signedIndexReader; IndexSearcher search = NewSearcher(reader); int medId = ((maxId - minId) / 2); string minIP = Pad(minId); string maxIP = Pad(maxId); string medIP = Pad(medId); int numDocs = reader.NumDocs; Assert.AreEqual(numDocs, 1 + maxId - minId, "num of docs"); ScoreDoc[] result; Query q = new TermQuery(new Term("body", "body")); // test id, bounded on both ends result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, maxIP, T, T), numDocs).ScoreDocs; Assert.AreEqual(numDocs, result.Length, "find all"); result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, maxIP, T, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 1, result.Length, "all but last"); result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, maxIP, F, T), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 1, result.Length, "all but first"); result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, maxIP, F, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 2, result.Length, "all but ends"); result = search.Search(q, TermRangeFilter.NewStringRange("id", medIP, maxIP, T, T), numDocs).ScoreDocs; Assert.AreEqual(1 + maxId - medId, result.Length, "med and up"); result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, medIP, T, T), numDocs).ScoreDocs; Assert.AreEqual(1 + medId - minId, result.Length, "up to med"); // unbounded id result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, null, T, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs, result.Length, "min and up"); result = search.Search(q, TermRangeFilter.NewStringRange("id", null, maxIP, F, T), numDocs).ScoreDocs; Assert.AreEqual(numDocs, result.Length, "max and down"); result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, null, F, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 1, result.Length, "not min, but up"); result = search.Search(q, TermRangeFilter.NewStringRange("id", null, maxIP, F, F), numDocs).ScoreDocs; Assert.AreEqual(numDocs - 1, result.Length, "not max, but down"); result = search.Search(q, TermRangeFilter.NewStringRange("id", medIP, maxIP, T, F), numDocs).ScoreDocs; Assert.AreEqual(maxId - medId, result.Length, "med and up, not max"); result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, medIP, F, T), numDocs).ScoreDocs; Assert.AreEqual(medId - minId, result.Length, "not min, up to med"); // very small sets result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, minIP, F, F), numDocs).ScoreDocs; Assert.AreEqual(0, result.Length, "min,min,F,F"); result = search.Search(q, TermRangeFilter.NewStringRange("id", medIP, medIP, F, F), numDocs).ScoreDocs; Assert.AreEqual(0, result.Length, "med,med,F,F"); result = search.Search(q, TermRangeFilter.NewStringRange("id", maxIP, maxIP, F, F), numDocs).ScoreDocs; Assert.AreEqual(0, result.Length, "max,max,F,F"); result = search.Search(q, TermRangeFilter.NewStringRange("id", minIP, minIP, T, T), numDocs).ScoreDocs; Assert.AreEqual(1, result.Length, "min,min,T,T"); result = search.Search(q, TermRangeFilter.NewStringRange("id", null, minIP, F, T), numDocs).ScoreDocs; Assert.AreEqual(1, result.Length, "nul,min,F,T"); result = search.Search(q, TermRangeFilter.NewStringRange("id", maxIP, maxIP, T, T), numDocs).ScoreDocs; Assert.AreEqual(1, result.Length, "max,max,T,T"); result = search.Search(q, TermRangeFilter.NewStringRange("id", maxIP, null, T, F), numDocs).ScoreDocs; Assert.AreEqual(1, result.Length, "max,nul,T,T"); result = search.Search(q, TermRangeFilter.NewStringRange("id", medIP, medIP, T, T), numDocs).ScoreDocs; Assert.AreEqual(1, result.Length, "med,med,T,T"); }