'Terms query in java spring boot

I'm trying to add terms in the should clause of ES query in java spring boot. I need it to look like this:

"should": [
    {
      "terms": {
        "segment_status.nse_status": [
          2,
          3
        ],
        "boost": 1
      }
    },
    {
      "terms": {
        "segment_status.pml_status": [
          2,
          3
        ],
        "boost": 1
      }
    }
  ]

I'm able to add only one terms condition for each should clause, but I need to append multiple terms in the same should clause. My code currently:

TermsQueryBuilder termsQuery = termsQuery(SEGMENT_STATUS + DOT + "bse_status", failedUserStatues);
BoolQueryBuilder includeCriteria = boolQuery();
includeCriteria.should(termsQuery);
NestedQueryBuilder nestedQueryForCriteria = nestedQuery(SEGMENT_STATUS, includeCriteria, ScoreMode.Avg);
searchQuery.must(nestedQueryForCriteria);

I'm using spring boot and ES version 6.4



Solution 1:[1]

I tried this and it worked as expected. Iterate using a loop and create a terms query in each iteration and call should(termsQuery) in each iteration.

Solution 2:[2]

We can add multiple terms in single should clause by using below code

BoolQueryBuilder query = boolQuery()
            .should(termQuery("segment_status.nse_status", new int[]{2,3}).boost(1f))
            .should(termQuery("segment_status.pml_status", new int[]{2,3}).boost(1f));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aditya K
Solution 2