enyoucky
enyoucky

Reputation: 123

Build terms_set query in elasticsearch java api

I try to find out how to build the following query with elasticsearch java api

{
  "query": {
    "bool": {
      "must": [
        {
          "terms_set": {
            "names": {
              "minimum_should_match_field": "some_match_field",
              "terms": [
                "Ala",
                "Bob"
              ]
            }
          }
        }
      ]
    }
  }
}

I tried to build this query with following code, but there is no termsSetQuery method as well as minimumShouldMatchField in api.

        NativeSearchQuery build = new NativeSearchQueryBuilder()
            .withQuery(boolQuery()
                    .must(termsQuery("names", List.of("Ala", "Bob"))))
            .build();

but it results as below

{
  "bool": {
    "must": [
      {
        "terms": {
          "names": [
            "Ala",
            "Bob"
          ]
        }
      }
    ]
  }
}

Upvotes: 0

Views: 468

Answers (1)

Sagar Patel
Sagar Patel

Reputation: 5486

You need to use TermsSetQueryBuilder for creating query like below:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
List<String> terms = new ArrayList<>();
terms.add("Ala");
searchSourceBuilder.query(QueryBuilders.boolQuery()
                .must(new TermsSetQueryBuilder("names", terms).setMinimumShouldMatchField("some_match_field")));

Upvotes: 3

Related Questions