GET feeds/_search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "comment",
"query": {
"match": {
"comment.c_text": "hey"
}
},"inner_hits": {
"highlight": {
"require_field_match": "true"
}
}
}
},
{
"term": {
"title": {
"value": "hey"
}
}
},
{
"term": {
"body": {
"value": "hey"
}
}
}
]
}
}
}
I am new to Elasticsearch, I get my desired result in kibana now I want to use this query in my Java application as I want to use this bool search query into my spring-boot application.
Can anyone tell me how to convert this query into Java?
CodePudding user response:
You can Java Rest client with ES 7.17.3 version using below dependancy
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>7.17.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
Below is Java code:
Query bodyTermQuery = TermQuery.of(tq -> tq.field("body").value("hey"))._toQuery();
Query titleTermQuery = TermQuery.of(tq -> tq.field("title").value("hey"))._toQuery();
Query matchQuery = MatchQuery.of(mq -> mq.field("comment.c_text").query("hey"))._toQuery();
Query nestedQuery = NestedQuery.of(nq -> nq.path("comment").query(matchQuery)
.innerHits(ih -> ih.highlight(h -> h.requireFieldMatch(true))))._toQuery();
SearchResponse<Object> response = client.search(
s -> s.index("feeds").query(q -> q.bool(b -> b.should(nestedQuery, titleTermQuery, bodyTermQuery))),
Object.class);