public virtual void TestInclusive() { Query query = TermRangeQuery.NewStringRange("content", "A", "C", true, true); InitializeIndex(new string[] { "A", "B", "C", "D" }); IndexReader reader = DirectoryReader.Open(Dir); IndexSearcher searcher = NewSearcher(reader); ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length, "A,B,C,D - A,B,C in range"); reader.Dispose(); InitializeIndex(new string[] { "A", "B", "D" }); reader = DirectoryReader.Open(Dir); searcher = NewSearcher(reader); hits = searcher.Search(query, null, 1000).ScoreDocs; Assert.AreEqual(2, hits.Length, "A,B,D - A and B in range"); reader.Dispose(); AddDoc("C"); reader = DirectoryReader.Open(Dir); searcher = NewSearcher(reader); hits = searcher.Search(query, null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length, "C added - A, B, C in range"); reader.Dispose(); }
public virtual void TestInclusiveLowerNull() { //http://issues.apache.org/jira/browse/LUCENE-38 Analyzer analyzer = new SingleCharAnalyzer(); Query query = TermRangeQuery.NewStringRange("content", null, "C", true, true); InitializeIndex(new string[] { "A", "B", "", "C", "D" }, analyzer); IndexReader reader = DirectoryReader.Open(Dir); IndexSearcher searcher = NewSearcher(reader); int numHits = searcher.Search(query, null, 1000).TotalHits; // When Lucene-38 is fixed, use the assert on the next line: Assert.AreEqual(4, numHits, "A,B,<empty string>,C,D => A,B,<empty string>,C in range"); // until Lucene-38 is fixed, use this assert //Assert.AreEqual(3, hits.Length(), "A,B,<empty string>,C,D => A,B,<empty string>,C in range"); reader.Dispose(); InitializeIndex(new string[] { "A", "B", "", "D" }, analyzer); reader = DirectoryReader.Open(Dir); searcher = NewSearcher(reader); numHits = searcher.Search(query, null, 1000).TotalHits; // When Lucene-38 is fixed, use the assert on the next line: Assert.AreEqual(3, numHits, "A,B,<empty string>,D - A, B and <empty string> in range"); // until Lucene-38 is fixed, use this assert //Assert.AreEqual(2, hits.Length(), "A,B,<empty string>,D => A, B and <empty string> in range"); reader.Dispose(); AddDoc("C"); reader = DirectoryReader.Open(Dir); searcher = NewSearcher(reader); numHits = searcher.Search(query, null, 1000).TotalHits; // When Lucene-38 is fixed, use the assert on the next line: Assert.AreEqual(4, numHits, "C added => A,B,<empty string>,C in range"); // until Lucene-38 is fixed, use this assert //Assert.AreEqual(3, hits.Length(), "C added => A,B,<empty string>,C in range"); reader.Dispose(); }
public virtual void TestBooleanOrderUnAffected() { // NOTE: uses index build in *this* setUp IndexSearcher search = NewSearcher(Reader); // first do a regular TermRangeQuery which uses term expansion so // docs with more terms in range get higher scores Query rq = TermRangeQuery.NewStringRange("data", "1", "4", T, T); ScoreDoc[] expected = search.Search(rq, null, 1000).ScoreDocs; int numHits = expected.Length; // now do a boolean where which also contains a // ConstantScoreRangeQuery and make sure hte order is the same BooleanQuery q = new BooleanQuery(); q.Add(rq, Occur.MUST); // T, F); q.Add(Csrq("data", "1", "6", T, T), Occur.MUST); // T, F); ScoreDoc[] actual = search.Search(q, null, 1000).ScoreDocs; AssertEquals("wrong numebr of hits", numHits, actual.Length); for (int i = 0; i < numHits; i++) { AssertEquals("mismatch in docid for hit#" + i, expected[i].Doc, actual[i].Doc); } }
private void TRangeQuery(bool useRandomAccess) { TermRangeQuery rq = TermRangeQuery.NewStringRange("sorter", "b", "d", true, true); Query filteredquery = new FilteredQuery(rq, Filter, RandomFilterStrategy(Random(), useRandomAccess)); ScoreDoc[] hits = Searcher.Search(filteredquery, null, 1000).ScoreDocs; Assert.AreEqual(2, hits.Length); QueryUtils.Check(Random(), filteredquery, Searcher, Similarity); }
/// <summary> /// macro for readability </summary> public static Query Csrq(string f, string l, string h, bool il, bool ih) { TermRangeQuery query = TermRangeQuery.NewStringRange(f, l, h, il, ih); query.MultiTermRewriteMethod = (MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); if (VERBOSE) { Console.WriteLine("TEST: query=" + query); } return(query); }
public static Query Csrq(string f, string l, string h, bool il, bool ih, MultiTermQuery.RewriteMethod method) { TermRangeQuery query = TermRangeQuery.NewStringRange(f, l, h, il, ih); query.MultiTermRewriteMethod = (method); if (VERBOSE) { Console.WriteLine("TEST: query=" + query + " method=" + method); } return(query); }
public virtual void TestSparse() { for (int i = 0; i < 10; i++) { int id_start = Random.Next(ir.MaxDoc - 1); int id_end = id_start + 1; Query query = TermRangeQuery.NewStringRange("id", Convert.ToString(id_start), Convert.ToString(id_end), true, true); Filter expected = new QueryWrapperFilter(query); Filter actual = new CachingWrapperFilter(expected); AssertFilterEquals(expected, actual); } }
public virtual void TestMultiValuedNRQ() { Directory directory = NewDirectory(); RandomIndexWriter writer = new RandomIndexWriter(Random(), directory, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetMaxBufferedDocs(TestUtil.NextInt(Random(), 50, 1000))); //DecimalFormat format = new DecimalFormat("00000000000", new DecimalFormatSymbols(Locale.ROOT)); NumberFormatInfo f = new NumberFormatInfo(); f.NumberDecimalSeparator = "."; f.NumberDecimalDigits = 0; int num = AtLeast(500); for (int l = 0; l < num; l++) { Document doc = new Document(); for (int m = 0, c = Random().Next(10); m <= c; m++) { int value = Random().Next(int.MaxValue); doc.Add(NewStringField("asc", value.ToString(f), Field.Store.NO)); doc.Add(new IntField("trie", value, Field.Store.NO)); } writer.AddDocument(doc); } IndexReader reader = writer.Reader; writer.Dispose(); IndexSearcher searcher = NewSearcher(reader); num = AtLeast(50); for (int i = 0; i < num; i++) { int lower = Random().Next(int.MaxValue); int upper = Random().Next(int.MaxValue); if (lower > upper) { int a = lower; lower = upper; upper = a; } TermRangeQuery cq = TermRangeQuery.NewStringRange("asc", lower.ToString(f), upper.ToString(f), true, true); NumericRangeQuery <int> tq = NumericRangeQuery.NewIntRange("trie", lower, upper, true, true); TopDocs trTopDocs = searcher.Search(cq, 1); TopDocs nrTopDocs = searcher.Search(tq, 1); Assert.AreEqual(trTopDocs.TotalHits, nrTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal"); } reader.Dispose(); directory.Dispose(); }
private void TRangeQuery(bool useRandomAccess) { TermRangeQuery rq = TermRangeQuery.NewStringRange("sorter", "b", "d", true, true); Query filteredquery = new FilteredQuery(rq, filter, RandomFilterStrategy(Random, useRandomAccess)); ScoreDoc[] hits = searcher.Search(filteredquery, null, 1000).ScoreDocs; Assert.AreEqual(2, hits.Length); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, filteredquery, searcher); }
public virtual void TestMultiValuedNRQ() { Directory directory = NewDirectory(); RandomIndexWriter writer = new RandomIndexWriter(Random, directory, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetMaxBufferedDocs(TestUtil.NextInt32(Random, 50, 1000))); const string format = "D11"; int num = AtLeast(500); for (int l = 0; l < num; l++) { Document doc = new Document(); for (int m = 0, c = Random.Next(10); m <= c; m++) { int value = Random.Next(int.MaxValue); doc.Add(NewStringField("asc", value.ToString(format), Field.Store.NO)); doc.Add(new Int32Field("trie", value, Field.Store.NO)); } writer.AddDocument(doc); } IndexReader reader = writer.GetReader(); writer.Dispose(); IndexSearcher searcher = NewSearcher(reader); num = AtLeast(50); for (int i = 0; i < num; i++) { int lower = Random.Next(int.MaxValue); int upper = Random.Next(int.MaxValue); if (lower > upper) { int a = lower; lower = upper; upper = a; } TermRangeQuery cq = TermRangeQuery.NewStringRange("asc", lower.ToString(format), upper.ToString(format), true, true); NumericRangeQuery <int> tq = NumericRangeQuery.NewInt32Range("trie", lower, upper, true, true); TopDocs trTopDocs = searcher.Search(cq, 1); TopDocs nrTopDocs = searcher.Search(tq, 1); Assert.AreEqual(trTopDocs.TotalHits, nrTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal"); } reader.Dispose(); directory.Dispose(); }
private void CheckNoMaxClauseLimitation(MultiTermQuery.RewriteMethod method) { int savedMaxClauseCount = BooleanQuery.MaxClauseCount; BooleanQuery.MaxClauseCount = 3; MultiTermQuery mtq = TermRangeQuery.NewStringRange("data", "2", "7", true, true); mtq.MultiTermRewriteMethod = (method); try { MultiSearcherDupls.Rewrite(mtq); } finally { BooleanQuery.MaxClauseCount = savedMaxClauseCount; } }
public virtual void TestEqualsHashcode() { Query query = TermRangeQuery.NewStringRange("content", "A", "C", true, true); query.Boost = 1.0f; Query other = TermRangeQuery.NewStringRange("content", "A", "C", true, true); other.Boost = 1.0f; Assert.AreEqual(query, query, "query equals itself is true"); Assert.AreEqual(query, other, "equivalent queries are equal"); Assert.AreEqual(query.GetHashCode(), other.GetHashCode(), "hashcode must return same value when equals is true"); other.Boost = 2.0f; Assert.IsFalse(query.Equals(other), "Different boost queries are not equal"); other = TermRangeQuery.NewStringRange("notcontent", "A", "C", true, true); Assert.IsFalse(query.Equals(other), "Different fields are not equal"); other = TermRangeQuery.NewStringRange("content", "X", "C", true, true); Assert.IsFalse(query.Equals(other), "Different lower terms are not equal"); other = TermRangeQuery.NewStringRange("content", "A", "Z", true, true); Assert.IsFalse(query.Equals(other), "Different upper terms are not equal"); query = TermRangeQuery.NewStringRange("content", null, "C", true, true); other = TermRangeQuery.NewStringRange("content", null, "C", true, true); Assert.AreEqual(query, other, "equivalent queries with null lowerterms are equal()"); Assert.AreEqual(query.GetHashCode(), other.GetHashCode(), "hashcode must return same value when equals is true"); query = TermRangeQuery.NewStringRange("content", "C", null, true, true); other = TermRangeQuery.NewStringRange("content", "C", null, true, true); Assert.AreEqual(query, other, "equivalent queries with null upperterms are equal()"); Assert.AreEqual(query.GetHashCode(), other.GetHashCode(), "hashcode returns same value"); query = TermRangeQuery.NewStringRange("content", null, "C", true, true); other = TermRangeQuery.NewStringRange("content", "C", null, true, true); Assert.IsFalse(query.Equals(other), "queries with different upper and lower terms are not equal"); query = TermRangeQuery.NewStringRange("content", "A", "C", false, false); other = TermRangeQuery.NewStringRange("content", "A", "C", true, true); Assert.IsFalse(query.Equals(other), "queries with different inclusive are not equal"); }
private void CheckDuplicateTerms(MultiTermQuery.RewriteMethod method) { MultiTermQuery mtq = TermRangeQuery.NewStringRange("data", "2", "7", true, true); mtq.MultiTermRewriteMethod = (method); Query q1 = Searcher.Rewrite(mtq); Query q2 = MultiSearcher.Rewrite(mtq); Query q3 = MultiSearcherDupls.Rewrite(mtq); if (VERBOSE) { Console.WriteLine(); Console.WriteLine("single segment: " + q1); Console.WriteLine("multi segment: " + q2); Console.WriteLine("multi segment with duplicates: " + q3); } Assert.IsTrue(q1.Equals(q2), "The multi-segment case must produce same rewritten query"); Assert.IsTrue(q1.Equals(q3), "The multi-segment case with duplicates must produce same rewritten query"); CheckBooleanQueryOrder(q1); CheckBooleanQueryOrder(q2); CheckBooleanQueryOrder(q3); }
public virtual void TestAllDocs() { InitializeIndex(new string[] { "A", "B", "C", "D" }); IndexReader reader = DirectoryReader.Open(Dir); IndexSearcher searcher = NewSearcher(reader); TermRangeQuery query = new TermRangeQuery("content", null, null, true, true); Terms terms = MultiFields.GetTerms(searcher.IndexReader, "content"); Assert.IsFalse(query.GetTermsEnum(terms) is TermRangeTermsEnum); Assert.AreEqual(4, searcher.Search(query, null, 1000).ScoreDocs.Length); query = new TermRangeQuery("content", null, null, false, false); Assert.IsFalse(query.GetTermsEnum(terms) is TermRangeTermsEnum); Assert.AreEqual(4, searcher.Search(query, null, 1000).ScoreDocs.Length); query = TermRangeQuery.NewStringRange("content", "", null, true, false); Assert.IsFalse(query.GetTermsEnum(terms) is TermRangeTermsEnum); Assert.AreEqual(4, searcher.Search(query, null, 1000).ScoreDocs.Length); // and now anothe one query = TermRangeQuery.NewStringRange("content", "B", null, true, false); Assert.IsTrue(query.GetTermsEnum(terms) is TermRangeTermsEnum); Assert.AreEqual(3, searcher.Search(query, null, 1000).ScoreDocs.Length); reader.Dispose(); }
public virtual void TestTopTermsRewrite() { InitializeIndex(new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K" }); IndexReader reader = DirectoryReader.Open(Dir); IndexSearcher searcher = NewSearcher(reader); TermRangeQuery query = TermRangeQuery.NewStringRange("content", "B", "J", true, true); CheckBooleanTerms(searcher, query, "B", "C", "D", "E", "F", "G", "H", "I", "J"); int savedClauseCount = BooleanQuery.MaxClauseCount; try { BooleanQuery.MaxClauseCount = 3; CheckBooleanTerms(searcher, query, "B", "C", "D"); } finally { BooleanQuery.MaxClauseCount = savedClauseCount; } reader.Dispose(); }
private void CheckMaxClauseLimitation(MultiTermQuery.RewriteMethod method, [CallerMemberName] string memberName = "") { int savedMaxClauseCount = BooleanQuery.MaxClauseCount; BooleanQuery.MaxClauseCount = 3; MultiTermQuery mtq = TermRangeQuery.NewStringRange("data", "2", "7", true, true); mtq.MultiTermRewriteMethod = (method); try { MultiSearcherDupls.Rewrite(mtq); Assert.Fail("Should throw BooleanQuery.TooManyClauses"); } catch (BooleanQuery.TooManyClausesException e) { // Maybe remove this assert in later versions, when internal API changes: Assert.AreEqual("CheckMaxClauseCount", new StackTrace(e, false).GetFrames()[0].GetMethod().Name); //, "Should throw BooleanQuery.TooManyClauses with a stacktrace containing checkMaxClauseCount()"); } finally { BooleanQuery.MaxClauseCount = savedMaxClauseCount; } }
/// <summary> /// Returns a random filter over the document set /// </summary> protected internal virtual Filter RandomFilter() { return(new QueryWrapperFilter(TermRangeQuery.NewStringRange("field", "a", "" + RandomChar(), true, true))); }