public void testBooleanRequiredEqualScores() throws Exception {

    BooleanQuery q = new BooleanQuery();
    {
      DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f);
      q1.add(tq("hed", "albino"));
      q1.add(tq("dek", "albino"));
      q.add(q1, BooleanClause.Occur.MUST); // true,false);
      QueryUtils.check(q1, s);
    }
    {
      DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f);
      q2.add(tq("hed", "elephant"));
      q2.add(tq("dek", "elephant"));
      q.add(q2, BooleanClause.Occur.MUST); // true,false);
      QueryUtils.check(q2, s);
    }

    QueryUtils.check(q, s);

    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;

    try {
      assertEquals("3 docs should match " + q.toString(), 3, h.length);
      float score = h[0].score;
      for (int i = 1; i < h.length; i++) {
        assertEquals("score #" + i + " is not the same", score, h[i].score, SCORE_COMP_THRESH);
      }
    } catch (Error e) {
      printHits("testBooleanRequiredEqualScores1", h, s);
      throw e;
    }
  }
  @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);
  }
  public void testBooleanOptionalNoTiebreaker() throws Exception {

    BooleanQuery q = new BooleanQuery();
    {
      DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f);
      q1.add(tq("hed", "albino"));
      q1.add(tq("dek", "albino"));
      q.add(q1, BooleanClause.Occur.SHOULD); // false,false);
    }
    {
      DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f);
      q2.add(tq("hed", "elephant"));
      q2.add(tq("dek", "elephant"));
      q.add(q2, BooleanClause.Occur.SHOULD); // false,false);
    }
    QueryUtils.check(q, s);

    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;

    try {
      assertEquals("4 docs should match " + q.toString(), 4, h.length);
      float score = h[0].score;
      for (int i = 1; i < h.length - 1; i++) {
        /* note: -1 */
        assertEquals("score #" + i + " is not the same", score, h[i].score, SCORE_COMP_THRESH);
      }
      assertEquals("wrong last", "d1", s.doc(h[h.length - 1].doc).get("id"));
      float score1 = h[h.length - 1].score;
      assertTrue(
          "d1 does not have worse score then others: " + score + " >? " + score1, score > score1);
    } catch (Error e) {
      printHits("testBooleanOptionalNoTiebreaker", h, s);
      throw e;
    }
  }
 @Test
 public void testBasicQuerySanities() {
   Query childQuery = new TermQuery(new Term("field", "value"));
   ScoreType scoreType = ScoreType.values()[random().nextInt(ScoreType.values().length)];
   ParentFieldMapper parentFieldMapper =
       SearchContext.current().mapperService().documentMapper("child").parentFieldMapper();
   ParentChildIndexFieldData parentChildIndexFieldData =
       SearchContext.current().fieldData().getForField(parentFieldMapper);
   BitDocIdSetFilter parentFilter =
       wrapWithBitSetFilter(new TermFilter(new Term(TypeFieldMapper.NAME, "parent")));
   int minChildren = random().nextInt(10);
   int maxChildren = scaledRandomIntBetween(minChildren, 10);
   Query query =
       new ChildrenQuery(
           parentChildIndexFieldData,
           "parent",
           "child",
           parentFilter,
           childQuery,
           scoreType,
           minChildren,
           maxChildren,
           12,
           wrapWithBitSetFilter(NonNestedDocsFilter.INSTANCE));
   QueryUtils.check(query);
 }
 public void verifyNrHits(Query q, int expected) throws Exception {
   ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
   if (expected != h.length) {
     printHits(getName(), h, s);
   }
   assertEquals("result count", expected, h.length);
   QueryUtils.check(q, s);
 }
  public void testSkipToFirsttimeHit() throws IOException {
    final DisjunctionMaxQuery dq = new DisjunctionMaxQuery(0.0f);
    dq.add(tq("dek", "albino"));
    dq.add(tq("dek", "DOES_NOT_EXIST"));

    QueryUtils.check(dq, s);

    final Weight dw = dq.weight(s);
    final Scorer ds = dw.scorer(s.getIndexReader(), true, false);
    assertTrue("firsttime skipTo found no match", ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals("found wrong docid", "d4", r.document(ds.docID()).get("id"));
  }
  public void testBooleanOptionalWithTiebreakerAndBoost() throws Exception {

    BooleanQuery q = new BooleanQuery();
    {
      DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f);
      q1.add(tq("hed", "albino", 1.5f));
      q1.add(tq("dek", "albino"));
      q.add(q1, BooleanClause.Occur.SHOULD); // false,false);
    }
    {
      DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f);
      q2.add(tq("hed", "elephant", 1.5f));
      q2.add(tq("dek", "elephant"));
      q.add(q2, BooleanClause.Occur.SHOULD); // false,false);
    }
    QueryUtils.check(q, s);

    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;

    try {

      assertEquals("4 docs should match " + q.toString(), 4, h.length);

      float score0 = h[0].score;
      float score1 = h[1].score;
      float score2 = h[2].score;
      float score3 = h[3].score;

      String doc0 = s.doc(h[0].doc).get("id");
      String doc1 = s.doc(h[1].doc).get("id");
      String doc2 = s.doc(h[2].doc).get("id");
      String doc3 = s.doc(h[3].doc).get("id");

      assertEquals("doc0 should be d4: ", "d4", doc0);
      assertEquals("doc1 should be d3: ", "d3", doc1);
      assertEquals("doc2 should be d2: ", "d2", doc2);
      assertEquals("doc3 should be d1: ", "d1", doc3);

      assertTrue(
          "d4 does not have a better score then d3: " + score0 + " >? " + score1, score0 > score1);
      assertTrue(
          "d3 does not have a better score then d2: " + score1 + " >? " + score2, score1 > score2);
      assertTrue(
          "d3 does not have a better score then d1: " + score2 + " >? " + score3, score2 > score3);

    } catch (Error e) {
      printHits("testBooleanOptionalWithTiebreakerAndBoost", h, s);
      throw e;
    }
  }
  public void testBooleanOptionalWithTiebreaker() throws Exception {

    BooleanQuery q = new BooleanQuery();
    {
      DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f);
      q1.add(tq("hed", "albino"));
      q1.add(tq("dek", "albino"));
      q.add(q1, BooleanClause.Occur.SHOULD); // false,false);
    }
    {
      DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f);
      q2.add(tq("hed", "elephant"));
      q2.add(tq("dek", "elephant"));
      q.add(q2, BooleanClause.Occur.SHOULD); // false,false);
    }
    QueryUtils.check(q, s);

    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;

    try {

      assertEquals("4 docs should match " + q.toString(), 4, h.length);

      float score0 = h[0].score;
      float score1 = h[1].score;
      float score2 = h[2].score;
      float score3 = h[3].score;

      String doc0 = s.doc(h[0].doc).get("id");
      String doc1 = s.doc(h[1].doc).get("id");
      String doc2 = s.doc(h[2].doc).get("id");
      String doc3 = s.doc(h[3].doc).get("id");

      assertTrue("doc0 should be d2 or d4: " + doc0, doc0.equals("d2") || doc0.equals("d4"));
      assertTrue("doc1 should be d2 or d4: " + doc0, doc1.equals("d2") || doc1.equals("d4"));
      assertEquals("score0 and score1 should match", score0, score1, SCORE_COMP_THRESH);
      assertEquals("wrong third", "d3", doc2);
      assertTrue(
          "d3 does not have worse score then d2 and d4: " + score1 + " >? " + score2,
          score1 > score2);

      assertEquals("wrong fourth", "d1", doc3);
      assertTrue(
          "d1 does not have worse score then d3: " + score2 + " >? " + score3, score2 > score3);

    } catch (Error e) {
      printHits("testBooleanOptionalWithTiebreaker", h, s);
      throw e;
    }
  }
  public void testSkipToFirsttimeMiss() throws IOException {
    final DisjunctionMaxQuery dq = new DisjunctionMaxQuery(0.0f);
    dq.add(tq("id", "d1"));
    dq.add(tq("dek", "DOES_NOT_EXIST"));

    QueryUtils.check(dq, s);

    final Weight dw = dq.weight(s);
    final Scorer ds = dw.scorer(s.getIndexReader(), true, false);
    final boolean skipOk = ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS;
    if (skipOk) {
      fail("firsttime skipTo found a match? ... " + r.document(ds.docID()).get("id"));
    }
  }
  public void testSimpleEqualScores2() throws Exception {

    DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f);
    q.add(tq("dek", "albino"));
    q.add(tq("dek", "elephant"));
    QueryUtils.check(q, s);

    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;

    try {
      assertEquals("3 docs should match " + q.toString(), 3, h.length);
      float score = h[0].score;
      for (int i = 1; i < h.length; i++) {
        assertEquals("score #" + i + " is not the same", score, h[i].score, SCORE_COMP_THRESH);
      }
    } catch (Error e) {
      printHits("testSimpleEqualScores2", h, s);
      throw e;
    }
  }
  public void testSimpleTiebreaker() throws Exception {

    DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.01f);
    q.add(tq("dek", "albino"));
    q.add(tq("dek", "elephant"));
    QueryUtils.check(q, s);

    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;

    try {
      assertEquals("3 docs should match " + q.toString(), 3, h.length);
      assertEquals("wrong first", "d2", s.doc(h[0].doc).get("id"));
      float score0 = h[0].score;
      float score1 = h[1].score;
      float score2 = h[2].score;
      assertTrue(
          "d2 does not have better score then others: " + score0 + " >? " + score1,
          score0 > score1);
      assertEquals("d4 and d1 don't have equal scores", score1, score2, SCORE_COMP_THRESH);
    } catch (Error e) {
      printHits("testSimpleTiebreaker", h, s);
      throw e;
    }
  }
  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);
  }