@Override
 protected BoolQueryBuilder doCreateTestQueryBuilder() {
   BoolQueryBuilder query = new BoolQueryBuilder();
   if (randomBoolean()) {
     query.adjustPureNegative(randomBoolean());
   }
   if (randomBoolean()) {
     query.disableCoord(randomBoolean());
   }
   if (randomBoolean()) {
     query.minimumNumberShouldMatch(randomMinimumShouldMatch());
   }
   int mustClauses = randomIntBetween(0, 3);
   for (int i = 0; i < mustClauses; i++) {
     query.must(RandomQueryBuilder.createQuery(random()));
   }
   int mustNotClauses = randomIntBetween(0, 3);
   for (int i = 0; i < mustNotClauses; i++) {
     query.mustNot(RandomQueryBuilder.createQuery(random()));
   }
   int shouldClauses = randomIntBetween(0, 3);
   for (int i = 0; i < shouldClauses; i++) {
     query.should(RandomQueryBuilder.createQuery(random()));
   }
   int filterClauses = randomIntBetween(0, 3);
   for (int i = 0; i < filterClauses; i++) {
     query.filter(RandomQueryBuilder.createQuery(random()));
   }
   return query;
 }
 @Override
 protected WrapperQueryBuilder doCreateTestQueryBuilder() {
   QueryBuilder wrappedQuery = RandomQueryBuilder.createQuery(random());
   switch (randomInt(2)) {
     case 0:
       return new WrapperQueryBuilder(wrappedQuery.toString());
     case 1:
       return new WrapperQueryBuilder(((ToXContentToBytes) wrappedQuery).buildAsBytes().toBytes());
     case 2:
       return new WrapperQueryBuilder(((ToXContentToBytes) wrappedQuery).buildAsBytes());
     default:
       throw new UnsupportedOperationException();
   }
 }
 /** @return a {@link HasChildQueryBuilder} with random values all over the place */
 @Override
 protected HasChildQueryBuilder doCreateTestQueryBuilder() {
   int min = randomIntBetween(0, Integer.MAX_VALUE / 2);
   int max = randomIntBetween(min, Integer.MAX_VALUE);
   InnerHitsBuilder.InnerHit innerHit =
       new InnerHitsBuilder.InnerHit().setSize(100).addSort(STRING_FIELD_NAME, SortOrder.ASC);
   return new HasChildQueryBuilder(
       CHILD_TYPE,
       RandomQueryBuilder.createQuery(random()),
       max,
       min,
       RandomPicks.randomFrom(random(), ScoreMode.values()),
       randomBoolean() ? null : new QueryInnerHits("inner_hits_name", innerHit));
 }
  public void testIllegalValues() throws IOException {
    QueryBuilder query = RandomQueryBuilder.createQuery(random());
    IllegalArgumentException e =
        expectThrows(
            IllegalArgumentException.class, () -> QueryBuilders.hasParentQuery(null, query, false));
    assertThat(e.getMessage(), equalTo("[has_parent] requires 'type' field"));

    e =
        expectThrows(
            IllegalArgumentException.class, () -> QueryBuilders.hasParentQuery("foo", null, false));
    assertThat(e.getMessage(), equalTo("[has_parent] requires 'query' field"));

    QueryShardContext context = createShardContext();
    HasParentQueryBuilder qb =
        QueryBuilders.hasParentQuery("just_a_type", new MatchAllQueryBuilder(), false);
    QueryShardException qse = expectThrows(QueryShardException.class, () -> qb.doToQuery(context));
    assertThat(
        qse.getMessage(), equalTo("[has_parent] no child types found for type [just_a_type]"));
  }
 /** @return a {@link HasChildQueryBuilder} with random values all over the place */
 @Override
 protected HasParentQueryBuilder doCreateTestQueryBuilder() {
   QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random());
   if (randomBoolean()) {
     requiresRewrite = true;
     innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString());
   }
   HasParentQueryBuilder hqb =
       new HasParentQueryBuilder(PARENT_TYPE, innerQueryBuilder, randomBoolean());
   if (randomBoolean()) {
     hqb.innerHit(
         new InnerHitBuilder()
             .setName(randomAsciiOfLengthBetween(1, 10))
             .setSize(randomIntBetween(0, 100))
             .addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC)));
   }
   hqb.ignoreUnmapped(randomBoolean());
   return hqb;
 }
  public void testValidate() {
    QueryBuilder innerQuery = RandomQueryBuilder.createQuery(random());
    IllegalArgumentException e =
        expectThrows(
            IllegalArgumentException.class,
            () -> QueryBuilders.nestedQuery(null, innerQuery, ScoreMode.Avg));
    assertThat(e.getMessage(), equalTo("[nested] requires 'path' field"));

    e =
        expectThrows(
            IllegalArgumentException.class,
            () -> QueryBuilders.nestedQuery("foo", null, ScoreMode.Avg));
    assertThat(e.getMessage(), equalTo("[nested] requires 'query' field"));

    e =
        expectThrows(
            IllegalArgumentException.class,
            () -> QueryBuilders.nestedQuery("foo", innerQuery, null));
    assertThat(e.getMessage(), equalTo("[nested] requires 'score_mode' field"));
  }
  public void testIllegalValues() {
    QueryBuilder query = RandomQueryBuilder.createQuery(random());
    try {
      new HasChildQueryBuilder(null, query);
      fail("must not be null");
    } catch (IllegalArgumentException ex) {

    }

    try {
      new HasChildQueryBuilder("foo", null);
      fail("must not be null");
    } catch (IllegalArgumentException ex) {

    }
    HasChildQueryBuilder foo = new HasChildQueryBuilder("foo", query); // all good
    try {
      foo.scoreMode(null);
      fail("must not be null");
    } catch (IllegalArgumentException ex) {

    }
    final int positiveValue = randomIntBetween(0, Integer.MAX_VALUE);
    try {
      foo.minChildren(randomIntBetween(Integer.MIN_VALUE, -1));
      fail("must not be negative");
    } catch (IllegalArgumentException ex) {

    }
    foo.minChildren(positiveValue);
    assertEquals(positiveValue, foo.minChildren());
    try {
      foo.maxChildren(randomIntBetween(Integer.MIN_VALUE, -1));
      fail("must not be negative");
    } catch (IllegalArgumentException ex) {

    }

    foo.maxChildren(positiveValue);
    assertEquals(positiveValue, foo.maxChildren());
  }
 /** @return a {@link HasChildQueryBuilder} with random values all over the place */
 @Override
 protected NestedQueryBuilder doCreateTestQueryBuilder() {
   QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random());
   if (randomBoolean()) {
     requiresRewrite = true;
     innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString());
   }
   NestedQueryBuilder nqb =
       new NestedQueryBuilder(
           "nested1", innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values()));
   nqb.ignoreUnmapped(randomBoolean());
   if (randomBoolean()) {
     nqb.innerHit(
         new InnerHitBuilder()
             .setName(randomAsciiOfLengthBetween(1, 10))
             .setSize(randomIntBetween(0, 100))
             .addSort(new FieldSortBuilder(INT_FIELD_NAME).order(SortOrder.ASC)),
         nqb.ignoreUnmapped());
   }
   return nqb;
 }