public virtual void Collect(int doc) { Explanation exp; // LUCENENET: IDE0059: Remove unnecessary value assignment doc = doc + @base; try { exp = s.Explain(q, doc); } catch (Exception e) when(e.IsIOException()) { throw RuntimeException.Create("exception in hitcollector of [[" + d + "]] for #" + doc, e); } Assert.IsNotNull(exp, "Explanation of [[" + d + "]] for #" + doc + " is null"); CheckHits.VerifyExplanation(d, doc, scorer.GetScore(), deep, exp); Assert.IsTrue(exp.IsMatch, "Explanation of [[" + d + "]] for #" + doc + " does not indicate match: " + exp.ToString()); }
public virtual void Collect(int doc) { Explanation exp = null; doc = doc + @base; try { exp = s.Explain(q, doc); } catch (IOException e) { throw new Exception("exception in hitcollector of [[" + d + "]] for #" + doc, e); } Assert.IsNotNull(exp, "Explanation of [[" + d + "]] for #" + doc + " is null"); CheckHits.VerifyExplanation(d, doc, scorer.GetScore(), deep, exp); Assert.IsTrue(exp.IsMatch, "Explanation of [[" + d + "]] for #" + doc + " does not indicate match: " + exp.ToString()); }
private void AssertNext(Scorer expected, Scorer actual) { if (actual == null) { Assert.AreEqual(DocIdSetIterator.NO_MORE_DOCS, expected.NextDoc()); return; } int doc; while ((doc = expected.NextDoc()) != DocIdSetIterator.NO_MORE_DOCS) { Assert.AreEqual(doc, actual.NextDoc()); Assert.AreEqual(expected.Freq, actual.Freq); float expectedScore = expected.GetScore(); float actualScore = actual.GetScore(); Assert.AreEqual(expectedScore, actualScore, CheckHits.ExplainToleranceDelta(expectedScore, actualScore)); } Assert.AreEqual(DocIdSetIterator.NO_MORE_DOCS, actual.NextDoc()); }
public virtual void QueriesTest(System.String queryText, int[] expDocNrs) { //System.out.println(); //System.out.println("Query: " + queryText); Query query1 = MakeQuery(queryText); TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, false); searcher.Search(query1, null, collector, null); ScoreDoc[] hits1 = collector.TopDocs().ScoreDocs; Query query2 = MakeQuery(queryText); // there should be no need to parse again... collector = TopScoreDocCollector.Create(1000, true); searcher.Search(query2, null, collector, null); ScoreDoc[] hits2 = collector.TopDocs().ScoreDocs; Assert.AreEqual(mulFactor * collector.internalTotalHits, bigSearcher.Search(query1, 1, null).TotalHits); CheckHits.CheckHitsQuery(query2, hits1, hits2, expDocNrs); }
private void AssertAdvance(Scorer expected, Scorer actual, int amount) { if (actual == null) { Assert.AreEqual(DocIdSetIterator.NO_MORE_DOCS, expected.NextDoc()); return; } int prevDoc = 0; int doc; while ((doc = expected.Advance(prevDoc + amount)) != DocIdSetIterator.NO_MORE_DOCS) { Assert.AreEqual(doc, actual.Advance(prevDoc + amount)); Assert.AreEqual(expected.Freq(), actual.Freq()); float expectedScore = expected.Score(); float actualScore = actual.Score(); Assert.AreEqual(expectedScore, actualScore, CheckHits.ExplainToleranceDelta(expectedScore, actualScore)); prevDoc = doc; } Assert.AreEqual(DocIdSetIterator.NO_MORE_DOCS, actual.Advance(prevDoc + amount)); }
public virtual void QueriesTest(System.String queryText, int[] expDocNrs) { //System.out.println(); //System.out.println("Query: " + queryText); try { Query query1 = MakeQuery(queryText); BooleanQuery.SetAllowDocsOutOfOrder(true); ScoreDoc[] hits1 = searcher.Search(query1, null, 1000).scoreDocs; Query query2 = MakeQuery(queryText); // there should be no need to parse again... BooleanQuery.SetAllowDocsOutOfOrder(false); ScoreDoc[] hits2 = searcher.Search(query2, null, 1000).scoreDocs; CheckHits.CheckHitsQuery(query2, hits1, hits2, expDocNrs); } finally { // even when a test fails. BooleanQuery.SetAllowDocsOutOfOrder(false); } }
/// <summary> /// Deep check that explanations of a query 'score' correctly. </summary> public static void CheckExplanations(Query q, IndexSearcher s) { CheckHits.CheckExplanations(q, null, s, true); }
private void AssertSubsetOfSameScores(Query q, TopDocs top1, TopDocs top2) { // The constrained query // should be a subset to the unconstrained query. if (top2.TotalHits > top1.TotalHits) { Assert.Fail("Constrained results not a subset:\n" + CheckHits.TopDocsString(top1, 0, 0) + CheckHits.TopDocsString(top2, 0, 0) + "for query:" + q.ToString()); } for (int hit = 0; hit < top2.TotalHits; hit++) { int id = top2.ScoreDocs[hit].Doc; float score = top2.ScoreDocs[hit].Score; bool found = false; // find this doc in other hits for (int other = 0; other < top1.TotalHits; other++) { if (top1.ScoreDocs[other].Doc == id) { found = true; float otherScore = top1.ScoreDocs[other].Score; // check if scores match Assert.AreEqual(score, otherScore, CheckHits.ExplainToleranceDelta(score, otherScore), "Doc " + id + " scores don't match\n" + CheckHits.TopDocsString(top1, 0, 0) + CheckHits.TopDocsString(top2, 0, 0) + "for query:" + q.ToString()); } } // check if subset if (!found) { Assert.Fail("Doc " + id + " not found\n" + CheckHits.TopDocsString(top1, 0, 0) + CheckHits.TopDocsString(top2, 0, 0) + "for query:" + q.ToString()); } } }
public virtual void TestRandomQueries() { System.Random rnd = NewRandom(); System.String field = "data"; System.String[] vals = new System.String[] { "1", "2", "3", "4", "5", "6", "A", "Z", "B", "Y", "Z", "X", "foo" }; int maxLev = 4; // callback object to set a random setMinimumNumberShouldMatch TestBoolean2.Callback minNrCB = new AnonymousClassCallback(rnd, this); // increase number of iterations for more complete testing for (int i = 0; i < 1000; i++) { int lev = rnd.Next(maxLev); long seed = rnd.Next(System.Int32.MaxValue); BooleanQuery q1 = TestBoolean2.RandBoolQuery(new System.Random((System.Int32)seed), true, lev, field, vals, null); // BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB); BooleanQuery q2 = TestBoolean2.RandBoolQuery(new System.Random((System.Int32)seed), true, lev, field, vals, null); // only set minimumNumberShouldMatch on the top level query since setting // at a lower level can change the score. minNrCB.PostCreate(q2); // Can't use Hits because normalized scores will mess things // up. The non-sorting version of search() that returns TopDocs // will not normalize scores. TopDocs top1 = s.Search(q1, null, 100, null); TopDocs top2 = s.Search(q2, null, 100, null); QueryUtils.Check(q1, s); QueryUtils.Check(q2, s); // The constrained query // should be a superset to the unconstrained query. Assert.IsFalse(top2.TotalHits > top1.TotalHits, "Constrained results not a subset:\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2); for (int hit = 0; hit < top2.TotalHits; hit++) { int id = top2.ScoreDocs[hit].Doc; float score = top2.ScoreDocs[hit].Score; bool found = false; // find this doc in other hits for (int other = 0; other < top1.TotalHits; other++) { if (top1.ScoreDocs[other].Doc == id) { found = true; float otherScore = top1.ScoreDocs[other].Score; // check if scores match Assert.IsFalse(Math.Abs(otherScore - score) > 1.0e-6f, "Doc " + id + " scores don't match\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2); } } // check if subset Assert.IsTrue(found, "Doc " + id + " not found\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2); } } // System.out.println("Total hits:"+tot); }
/// <summary> Overrides superclass to ignore matches and focus on non-matches /// /// </summary> /// <seealso cref="CheckHits.checkNoMatchExplanations"> /// </seealso> public override void Qtest(Query q, int[] expDocNrs) { CheckHits.CheckNoMatchExplanations(q, FIELD, searcher, expDocNrs); }
/// <summary>check the expDocNrs first, then check the query (and the explanations) </summary> public virtual void Qtest(Query q, int[] expDocNrs) { CheckHits.CheckHitCollector(q, FIELD, searcher, expDocNrs); }
public virtual void TestRandomQueries() { string[] vals = new string[] { "w1", "w2", "w3", "w4", "w5", "xx", "yy", "zzz" }; int tot = 0; BooleanQuery q1 = null; try { // increase number of iterations for more complete testing int num = AtLeast(20); for (int i = 0; i < num; i++) { int level = Random.Next(3); q1 = RandBoolQuery(new Random(Random.Next()), Random.NextBoolean(), level, field, vals, null); // Can't sort by relevance since floating point numbers may not quite // match up. Sort sort = Sort.INDEXORDER; QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, q1, Searcher); // baseline sim try { // a little hackish, QueryUtils.check is too costly to do on bigSearcher in this loop. Searcher.Similarity = BigSearcher.Similarity; // random sim QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, q1, Searcher); } finally { Searcher.Similarity = new DefaultSimilarity(); // restore } TopFieldCollector collector = TopFieldCollector.Create(sort, 1000, false, true, true, true); Searcher.Search(q1, null, collector); ScoreDoc[] hits1 = collector.GetTopDocs().ScoreDocs; collector = TopFieldCollector.Create(sort, 1000, false, true, true, false); Searcher.Search(q1, null, collector); ScoreDoc[] hits2 = collector.GetTopDocs().ScoreDocs; tot += hits2.Length; CheckHits.CheckEqual(q1, hits1, hits2); BooleanQuery q3 = new BooleanQuery(); q3.Add(q1, Occur.SHOULD); q3.Add(new PrefixQuery(new Term("field2", "b")), Occur.SHOULD); TopDocs hits4 = BigSearcher.Search(q3, 1); Assert.AreEqual(MulFactor * collector.TotalHits + NUM_EXTRA_DOCS / 2, hits4.TotalHits); } } catch (Exception) { // For easier debugging Console.WriteLine("failed query: " + q1); throw; } // System.out.println("Total hits:"+tot); }
public virtual void TestRandomQueries() { string[] vals = new string[] { "w1", "w2", "w3", "w4", "w5", "xx", "yy", "zzz" }; int tot = 0; BooleanQuery q1 = null; try { // increase number of iterations for more complete testing int num = AtLeast(20); for (int i = 0; i < num; i++) { int level = Random.Next(3); q1 = RandBoolQuery(new J2N.Randomizer(Random.NextInt64()), Random.NextBoolean(), level, field, vals, null); // Can't sort by relevance since floating point numbers may not quite // match up. Sort sort = Sort.INDEXORDER; QueryUtils.Check(Random, q1, searcher); // baseline sim try { // a little hackish, QueryUtils.check is too costly to do on bigSearcher in this loop. searcher.Similarity = bigSearcher.Similarity; // random sim QueryUtils.Check(Random, q1, searcher); } finally { searcher.Similarity = new DefaultSimilarity(); // restore } TopFieldCollector collector = TopFieldCollector.Create(sort, 1000, false, true, true, true); searcher.Search(q1, null, collector); ScoreDoc[] hits1 = collector.GetTopDocs().ScoreDocs; collector = TopFieldCollector.Create(sort, 1000, false, true, true, false); searcher.Search(q1, null, collector); ScoreDoc[] hits2 = collector.GetTopDocs().ScoreDocs; tot += hits2.Length; CheckHits.CheckEqual(q1, hits1, hits2); BooleanQuery q3 = new BooleanQuery(); q3.Add(q1, Occur.SHOULD); q3.Add(new PrefixQuery(new Term("field2", "b")), Occur.SHOULD); TopDocs hits4 = bigSearcher.Search(q3, 1); Assert.AreEqual(mulFactor * collector.TotalHits + NUM_EXTRA_DOCS / 2, hits4.TotalHits); } } catch (Exception e) when(e.IsException()) { // For easier debugging Console.WriteLine("failed query: " + q1); throw; // LUCENENET: CA2200: Rethrow to preserve stack details (https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200-rethrow-to-preserve-stack-details) } // System.out.println("Total hits:"+tot); }
/// <summary> /// check the expDocNrs first, then check the query (and the explanations) </summary> public virtual void Qtest(Query q, int[] expDocNrs) { CheckHits.CheckHitCollector(Random(), q, FIELD, Searcher, expDocNrs, Similarity); }