Create a Java Query from a DSL terms query

Given the following Query

{
    "query":{
    "bool" : {
        "must" : [ {                   
        "terms" : {
            "_id" : ["8606874","21387518","16704862","23947520","23897437","1050114","24967566","5035697","12354041","17037068","20316927","23405734","12594216","2252927","17434782","23872254","24177201","4557946","1015260","3164537","6369537","10790781","4224488","23117230","1050119","12525387","23692179","5585321","16644872","3727442","20378284","21506949","1015271","2490589","1047006","1015228","10782526","12280902","23465365","789184"]
     }
   }, {
        "terms" : {
            "cuisine.key" : ["10345"]
     }
   } ]
 }
}}

What would be the correct Java API implementation. Any pointer in the right direction would be really helpful.

Thanks!

Hi @Edgar_Osorio

Look this example:

List<FieldValue> ids = new ArrayList<FieldValue>();
ids.add(FieldValue.of("1"));
ids.add(FieldValue.of("2"));

List<FieldValue> cousines = new ArrayList<FieldValue>();
cousines.add(FieldValue.of("1"));

Query termCuisine = TermsQuery.of(ts -> ts
    .field("_id)
    .terms(TermsQueryField.of(t -> t.value(ids))))._toQuery();

Query termIds = TermsQuery.of(ts -> ts
    .field("cuisine.key"")
    .terms(TermsQueryField.of(t -> t.value(cousines))))._toQuery();

var boolQuery = new BoolQuery.Builder().must(termIds, termCuisine).build();
1 Like

It did work like a charm

Thank you so much for your help!

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.