Elasticsearch JAVA Query with nesting

We have two fields in our Elasticsearch index, one is called 'Command' and the other is called 'Code'.

What I'm looking to do is have a 'terms' match for 2 possible values for Command. Then, for each of the 2 Commands , have a must match for them.

So, something like:

QueryBuilder qb = QueryBuilders
.boolQuery()
.must(termsQuery("Command.keyword", "TypeA", "TypeB"))
.must(termsQuery("Code.keyword", "TypeACodeA", "TypeACodeB", "TypeBCodeA", "TypeBCodeB"))

The problem is, I believe that I will get too many potential matches - ones I don't want, such as a Command of 'TypeA' to a Code of 'TypeBCodeA'. What I'm looking for is query results that ONLY include:

Command TypeA matched to TypeACodeA OR TypeACodeB,
OR,
Command TypeB matched to TypeBCodeA or TypeBCodeB

I know that there's nested queries, sub queries, etc, although for these other techniques how I would apply them. Any suggestions would be great.

  • David H.

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