[英]Convert elasticsearch bool query to Java
GET feeds/_search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "comment",
"query": {
"match": {
"comment.c_text": "This is mateen"
}
},"inner_hits": {}
}
},
{
"term": {
"title.keyword": {
"value": "This is mateen"
}
}
},
{
"term": {
"body.keyword": {
"value": "This is mateen"
}
}
}
]
}
}
}
Mapping is as follows:映射如下:
PUT feeds
{
"mappings": {
"properties": {
"comment":{
"type": "nested"
}
}
}
}
I am using Elasticsearch 7.17.3.我正在使用 Elasticsearch 7.17.3。 For searching all documents of Elasticsearch in my springboot I have written the following code that gives me the exact output:为了在我的 springboot 中搜索 Elasticsearch 的所有文档,我编写了以下代码,它为我提供了准确的 output:
public List<feed> searchAllDocuments() throws IOException {
SearchRequest searchRequest = SearchRequest.of(s -> s.index(indexName));
SearchResponse searchResponse = elasticsearchClient.search(searchRequest, feed.class);
List<Hit> hits = searchResponse.hits().hits();
List<feed> feeds = new ArrayList<>();
feed f=null;
for (Hit object : hits) {
f = (feed) object.source();
feeds.add(f);
}
return feeds;
}
Can anyone help me convert the query into springboot application?谁能帮我将查询转换为 springboot 应用程序? I am new to it and need your guidance我是新手,需要您的指导
If you use the new Java Api Client try the code bellow:如果您使用新的 Java Api 客户端,请尝试以下代码:
Query nestedQuery = NestedQuery.of(nq ->
nq.path("comment")
.innerHits(InnerHits.of(ih -> ih))
.query(MatchQuery.of(mq -> mq.field("comment.c_text").query("This is mateen"))._toQuery())
)._toQuery();
Query termQueryTitle = TermQuery.of(
tq -> tq.field("title.keyword").value("This is mateen")
)._toQuery();
Query termQueryBody = TermQuery.of(
tq -> tq.field("body.keyword").value("This is mateen")
)._toQuery();
Query boolQuery = BoolQuery.of(bq -> bq.should(nestedQuery, termQueryBody, termQueryTitle))._toQuery();
SearchRequest searchRequest = SearchRequest.of(
s -> s.index("idx_name").query(boolQuery)
);
var response = client.search(searchRequest, Feed.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.