我是Elasticsearch的新手,并提出一个问题,即Elasticsearch嵌套查询是否只返回嵌套字段的匹配嵌套文档。
例如,我有一个名为blog
的类型,其中包含一个名为comments
的嵌套字段
{
"id": 1,
...
"comments":[
{"content":"Michael is a basketball player"},
{"content":"David is a soccer player"}
]
}
{
"id": 2,
...
"comments":[
{"content":"Wayne is a soccer player"},
{"content":"Steven is also a soccer player"},
]
}
和嵌套查询
{"query":{
"nested":{
"path":"comments",
"query":{"match":{"comments.content":"soccer"}}
}
}
我需要的是搜索博客文章,其中包含提及“足球”的评论,每个博客文章中与“足球”匹配的评论数量(在该示例中,它的数量为1,因为另一条评论刚刚提及“篮球”)
{"hits":[
{
"id":1,
...
"count_for_comments_that_matches_query":1,
},
{
"id":2,
...
"count_for_comments_that_matches_query":2,
}
]}
然而,似乎Elasticsearch总是返回完整的文档,所以我怎么能实现它,或者我不能实现它?
答案 0 :(得分:1)
答案就在这里。
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#nested-inner-hits
您需要使用弹性搜索的nested inner hits
功能。
{
"_source": [
"id"
],
"query": {
"bool": {
"must": [
{
"match": {
"id": "1"
}
},
{
"nested": {
"path": "comments",
"query": {
"match": {
"comments.content": "soccer"
}
},
"inner_hits": {}
}
}
]
}
}
}
我认为它会解决问题