public void testToQueryPhraseQueryBoostAndSlop() throws IOException {
   assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
   QueryStringQueryBuilder queryStringQueryBuilder =
       new QueryStringQueryBuilder("\"test phrase\"~2").field(STRING_FIELD_NAME, 5f);
   Query query = queryStringQueryBuilder.toQuery(createShardContext());
   assertThat(query, instanceOf(DisjunctionMaxQuery.class));
   DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query;
   assertThat(disjunctionMaxQuery.getDisjuncts().size(), equalTo(1));
   assertThat(disjunctionMaxQuery.getDisjuncts().get(0), instanceOf(BoostQuery.class));
   BoostQuery boostQuery = (BoostQuery) disjunctionMaxQuery.getDisjuncts().get(0);
   assertThat(boostQuery.getBoost(), equalTo(5f));
   assertThat(boostQuery.getQuery(), instanceOf(PhraseQuery.class));
   PhraseQuery phraseQuery = (PhraseQuery) boostQuery.getQuery();
   assertThat(phraseQuery.getSlop(), Matchers.equalTo(2));
   assertThat(phraseQuery.getTerms().length, equalTo(2));
 }
  public void testToQueryBoosts() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    QueryShardContext shardContext = createShardContext();
    QueryStringQueryBuilder queryStringQuery = queryStringQuery(STRING_FIELD_NAME + ":boosted^2");
    Query query = queryStringQuery.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(2.0f));
    assertThat(boostQuery.getQuery(), instanceOf(TermQuery.class));
    assertThat(
        ((TermQuery) boostQuery.getQuery()).getTerm(),
        equalTo(new Term(STRING_FIELD_NAME, "boosted")));
    queryStringQuery.boost(2.0f);
    query = queryStringQuery.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(2.0f));
    assertThat(boostQuery.getQuery(), instanceOf(BoostQuery.class));
    boostQuery = (BoostQuery) boostQuery.getQuery();
    assertThat(boostQuery.getBoost(), equalTo(2.0f));

    queryStringQuery =
        queryStringQuery(
            "((" + STRING_FIELD_NAME + ":boosted^2) AND (" + STRING_FIELD_NAME + ":foo^1.5))^3");
    query = queryStringQuery.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(3.0f));
    BoostQuery boostQuery1 = assertBooleanSubQuery(boostQuery.getQuery(), BoostQuery.class, 0);
    assertThat(boostQuery1.getBoost(), equalTo(2.0f));
    assertThat(boostQuery1.getQuery(), instanceOf(TermQuery.class));
    assertThat(
        ((TermQuery) boostQuery1.getQuery()).getTerm(),
        equalTo(new Term(STRING_FIELD_NAME, "boosted")));
    BoostQuery boostQuery2 = assertBooleanSubQuery(boostQuery.getQuery(), BoostQuery.class, 1);
    assertThat(boostQuery2.getBoost(), equalTo(1.5f));
    assertThat(boostQuery2.getQuery(), instanceOf(TermQuery.class));
    assertThat(
        ((TermQuery) boostQuery2.getQuery()).getTerm(),
        equalTo(new Term(STRING_FIELD_NAME, "foo")));
    queryStringQuery.boost(2.0f);
    query = queryStringQuery.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(2.0f));
  }
  public void testToQueryBooleanQueryMultipleBoosts() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    int numBoosts = randomIntBetween(2, 10);
    float[] boosts = new float[numBoosts + 1];
    String queryStringPrefix = "";
    String queryStringSuffix = "";
    for (int i = 0; i < boosts.length - 1; i++) {
      float boost = 2.0f / randomIntBetween(3, 20);
      boosts[i] = boost;
      queryStringPrefix += "(";
      queryStringSuffix += ")^" + boost;
    }
    String queryString = queryStringPrefix + "foo bar" + queryStringSuffix;

    float mainBoost = 2.0f / randomIntBetween(3, 20);
    boosts[boosts.length - 1] = mainBoost;
    QueryStringQueryBuilder queryStringQueryBuilder =
        new QueryStringQueryBuilder(queryString)
            .field(STRING_FIELD_NAME)
            .minimumShouldMatch("2")
            .boost(mainBoost);
    Query query = queryStringQueryBuilder.toQuery(createShardContext());

    for (int i = boosts.length - 1; i >= 0; i--) {
      assertThat(query, instanceOf(BoostQuery.class));
      BoostQuery boostQuery = (BoostQuery) query;
      assertThat(boostQuery.getBoost(), equalTo(boosts[i]));
      query = boostQuery.getQuery();
    }

    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) query;
    assertThat(booleanQuery.getMinimumNumberShouldMatch(), equalTo(2));
    assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.SHOULD));
    assertThat(
        booleanQuery.clauses().get(0).getQuery(),
        equalTo(new TermQuery(new Term(STRING_FIELD_NAME, "foo"))));
    assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.SHOULD));
    assertThat(
        booleanQuery.clauses().get(1).getQuery(),
        equalTo(new TermQuery(new Term(STRING_FIELD_NAME, "bar"))));
  }