I am totally new to Spring Data Elasticsearch and trying to build a query like the one below; Any help would be greatly appreciated.
{
"query":{
"bool":{
"must":[
{
"bool":{
"should":[
{
"match_phrase":{
"firstPhrase":"A"
}
},
{
"match_phrase":{
"secondPhrase":"B"
}
},
{
"nested":{
"path":"things",
"query":{
"bool":{
"should":[
{
"match_phrase":{
"another.phraseOne":"C"
}
},
{
"match_phrase":{
"another.phraseTwo":"D"
}
}
]
}
}
}
}
]
}
},
{
"match_phrase":{
"otherPhase":"E"
}
}
]
}
}
}
I started with creating the first two boolean queries;
BoolQueryBuilder firstBool = QueryBuilders.boolQuery()
.should(QueryBuilders.matchPhraseQuery("firstPhrase", "A"))
.should(QueryBuilders.matchPhraseQuery("secondPhrase", "B"));
BoolQueryBuilder secondBool = QueryBuilders.boolQuery()
.should(QueryBuilders.matchPhraseQuery("another.phraseOne", "C"))
.should(QueryBuilders.matchPhraseQuery("another.phraseTwo", "D"));
Then I tried to create a nested query (Not sure how to use ScoreMode);
QueryBuilder nestedQuery= nestedQuery("things",secondBool, ScoreMode.None);
I created another match-phrase which is in the must;
QueryBuilder third = QueryBuilders.matchPhraseQuery("otherPhrase", "E");
I am not sure how to add them all to create the structure I have and most importantly not sure how to add the nested query to the "must".
QueryBuilder allQueries = new BoolQueryBuilder()
.must(first)
.must(second)
.must(third);
And finally, using them all at once;
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(allQueries).build();
Would this be a right approach to achive what I am trying to generate?