コード例 #1
0
  @Test
  public void passQueryOrFilterAsJSONStringTest() throws Exception {
    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (Exception e) {
      // ignore
    }

    client
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(ImmutableSettings.settingsBuilder().put("number_of_shards", 1))
        .execute()
        .actionGet();

    client
        .prepareIndex("test", "type1", "1")
        .setSource("field1", "value1_1", "field2", "value2_1")
        .setRefresh(true)
        .execute()
        .actionGet();

    WrapperQueryBuilder wrapper =
        new WrapperQueryBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }");
    SearchResponse searchResponse = client.prepareSearch().setQuery(wrapper).execute().actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo(1l));

    BoolQueryBuilder bool = new BoolQueryBuilder();
    bool.must(wrapper);
    bool.must(new TermQueryBuilder("field2", "value2_1"));

    searchResponse = client.prepareSearch().setQuery(wrapper).execute().actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo(1l));

    WrapperFilterBuilder wrapperFilter =
        new WrapperFilterBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }");

    searchResponse = client.prepareSearch().setFilter(wrapperFilter).execute().actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo(1l));
  }