【发布时间】:2023-04-03 00:51:01
【问题描述】:
我有一个具有以下结构的索引。
{
"title": "Your top FIY tips",
"content": "Fix It Yourself in April 2012.",
"tags": [
{
"tagName": "Fix it yourself"
},
{
"tagName": "customer tips"
},
{
"tagName": "competition"
}
]
}
映射看起来像
{
"articles": {
"mappings": {
"article": {
"properties": {
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tags": {
"type": "nested",
"properties": {
"tagName": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
我正在使用以下 DSL 查询来搜索“内容”和“标题”字段,并将结果缩小到某个“标签名称”。然后使用聚合来计算该查询中的标记名。
GET /articles/_search
{
"from": 1,
"size": 10,
"aggs": {
"tags": {
"nested": {
"path": "tags"
},
"aggs": {
"tags-tagnames": {
"terms": {
"field": "tags.tagName.raw"
}
}
}
}
},
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "FIY",
"fields": [
"title",
"content"
]
}
},
{
"nested": {
"query": {
"terms": {
"tags.tagName": [
"competition"
]
}
},
"path": "tags"
}
}
]
}
}
}
“tagNames”的搜索查询和过滤器工作正常。然而,聚合并不完全有效。它似乎没有在结果中包含嵌套查询数据。返回的聚合结果只是基于多重匹配搜索。
如何在聚合中包含嵌套查询。
示例文档位于
https://gist.github.com/anonymous/83bc2b1bfa0ac0d295d42297e1d76c00
【问题讨论】:
-
索引中的映射是什么样的,即
GET {index}/_mapping返回什么?tags是否映射为nested类型? -
@RussCam 标签被映射为嵌套。已更新问题以包含映射
-
你有小例子可以重现吗?看起来您希望获取标签名称中包含术语
competition并匹配标题或内容中的FIY的标签的原始标签名称?这不是你看到的吗? -
聚合是根据标题或内容中匹配的 FIY 返回结果,它没有过滤标签竞争。查询工作正常。聚合不正确
-
@RussCam 使用最新版本 5.2。已将文档添加到 gist gist.github.com/anonymous/83bc2b1bfa0ac0d295d42297e1d76c00
标签: elasticsearch nest elasticsearch-dsl