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);
    }
  }
Esempio n. 3
0
  @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);
  }
Esempio n. 4
0
  /** 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);
  }