private void assertFilterEquals(Filter f1, Filter f2) throws Exception { Query query = new MatchAllDocsQuery(); TopDocs hits1 = is.search(query, f1, ir.maxDoc()); TopDocs hits2 = is.search(query, f2, ir.maxDoc()); assertEquals(hits1.totalHits, hits2.totalHits); CheckHits.checkEqual(query, hits1.scoreDocs, hits2.scoreDocs); // now do it again to confirm caching works TopDocs hits3 = is.search(query, f1, ir.maxDoc()); TopDocs hits4 = is.search(query, f2, ir.maxDoc()); assertEquals(hits3.totalHits, hits4.totalHits); CheckHits.checkEqual(query, hits3.scoreDocs, hits4.scoreDocs); }
void checkSorts(Query query, Sort sort) throws Exception { int size = TestUtil.nextInt(random(), 1, searcher.getIndexReader().maxDoc() / 5); TopDocs expected = searcher.search(query, size, sort, random().nextBoolean(), random().nextBoolean()); Sort mutatedSort = convertSortToSortable(sort); TopDocs actual = searcher.search(query, size, mutatedSort, random().nextBoolean(), random().nextBoolean()); CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs); if (size < actual.totalHits) { expected = searcher.searchAfter(expected.scoreDocs[size - 1], query, size, sort); actual = searcher.searchAfter(actual.scoreDocs[size - 1], query, size, mutatedSort); CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs); } }
@Test public void testRandomQueries() throws Exception { String[] vals = {"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(10); for (int i = 0; i < num; i++) { int level = random.nextInt(3); q1 = randBoolQuery( new Random(random.nextLong()), 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); final Similarity oldSim = searcher.getSimilarity(); try { searcher.setSimilarity(new FunkySimilarity()); QueryUtils.check(random, q1, searcher); } finally { searcher.setSimilarity(oldSim); } TopFieldCollector collector = TopFieldCollector.create(sort, 1000, false, true, true, true); searcher.search(q1, null, collector); ScoreDoc[] hits1 = collector.topDocs().scoreDocs; collector = TopFieldCollector.create(sort, 1000, false, true, true, false); searcher.search(q1, null, collector); ScoreDoc[] hits2 = collector.topDocs().scoreDocs; tot += hits2.length; CheckHits.checkEqual(q1, hits1, hits2); BooleanQuery q3 = new BooleanQuery(); q3.add(q1, BooleanClause.Occur.SHOULD); q3.add(new PrefixQuery(new Term("field2", "b")), BooleanClause.Occur.SHOULD); TopDocs hits4 = bigSearcher.search(q3, 1); assertEquals(mulFactor * collector.totalHits + NUM_EXTRA_DOCS / 2, hits4.totalHits); } } catch (Exception e) { // For easier debugging System.out.println("failed query: " + q1); throw e; } // System.out.println("Total hits:"+tot); }
/** check that the # of hits is the same as from a very simple regexpquery implementation. */ protected void assertSame(String regexp) throws IOException { RegexpQuery smart = new RegexpQuery(new Term(fieldName, regexp), RegExp.NONE); DumbRegexpQuery dumb = new DumbRegexpQuery(new Term(fieldName, regexp), RegExp.NONE); TopDocs smartDocs = searcher1.search(smart, 25); TopDocs dumbDocs = searcher2.search(dumb, 25); CheckHits.checkEqual(smart, smartDocs.scoreDocs, dumbDocs.scoreDocs); }
/** check that the # of hits is the same as from a very simple regexpquery implementation. */ private void assertSame(String regexp) throws IOException { RegexpQuery smart = new RegexpQuery(new Term("field", regexp), RegExp.NONE); DumbRegexpQuery dumb = new DumbRegexpQuery(new Term("field", regexp), RegExp.NONE); // we can't compare the two if automaton rewrites to a simpler enum. // for example: "a\uda07\udcc7?.*?" gets rewritten to a simpler query: // a\uda07* prefixquery. Prefixquery then does the "wrong" thing, which // isn't really wrong as the query was undefined to begin with... but not // automatically comparable. if (!(smart.getTermsEnum(searcher.getIndexReader()) instanceof AutomatonTermsEnum)) return; TopDocs smartDocs = searcher.search(smart, 25); TopDocs dumbDocs = searcher.search(dumb, 25); CheckHits.checkEqual(smart, smartDocs.scoreDocs, dumbDocs.scoreDocs); }
public void queriesTest(String queryText, int[] expDocNrs) throws Exception { // System.out.println(); // System.out.println("Query: " + queryText); Query query = makeQuery(queryText); TopScoreDocCollector collector = TopScoreDocCollector.create(1000, false); searcher.search(query, null, collector); ScoreDoc[] hits1 = collector.topDocs().scoreDocs; collector = TopScoreDocCollector.create(1000, true); searcher.search(query, null, collector); ScoreDoc[] hits2 = collector.topDocs().scoreDocs; assertEquals(mulFactor * collector.totalHits, bigSearcher.search(query, 1).totalHits); CheckHits.checkHitsQuery(query, hits1, hits2, expDocNrs); }
public void testRandomQueries() throws Exception { final Random rnd = newRandom(); String field = "data"; String[] vals = {"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 TestBoolean2.Callback() { public void postCreate(BooleanQuery q) { BooleanClause[] c = q.getClauses(); int opt = 0; for (int i = 0; i < c.length; i++) { if (c[i].getOccur() == BooleanClause.Occur.SHOULD) opt++; } q.setMinimumNumberShouldMatch(rnd.nextInt(opt + 2)); } }; // increase number of iterations for more complete testing for (int i = 0; i < 50; i++) { int lev = rnd.nextInt(maxLev); final long seed = rnd.nextLong(); BooleanQuery q1 = TestBoolean2.randBoolQuery(new Random(seed), true, lev, field, vals, null); // BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB); BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(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); TopDocs top2 = s.search(q2, null, 100); if (i < 100) { QueryUtils.check(q1, s); QueryUtils.check(q2, s); } // The constrained query // should be a superset to the unconstrained query. if (top2.totalHits > top1.totalHits) { TestCase.fail( "Constrained results not a subset:\n" + CheckHits.topdocsString(top1, 0, 0) + CheckHits.topdocsString(top2, 0, 0) + "for query:" + q2.toString()); } for (int hit = 0; hit < top2.totalHits; hit++) { int id = top2.scoreDocs[hit].doc; float score = top2.scoreDocs[hit].score; boolean 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 if (Math.abs(otherScore - score) > 1.0e-6f) { TestCase.fail( "Doc " + id + " scores don't match\n" + CheckHits.topdocsString(top1, 0, 0) + CheckHits.topdocsString(top2, 0, 0) + "for query:" + q2.toString()); } } } // check if subset if (!found) TestCase.fail( "Doc " + id + " not found\n" + CheckHits.topdocsString(top1, 0, 0) + CheckHits.topdocsString(top2, 0, 0) + "for query:" + q2.toString()); } } // System.out.println("Total hits:"+tot); }