gpt4 book ai didi

elasticsearch - 如何获取一条记录中多个匹配字段的命中数?

转载 作者:行者123 更新时间:2023-12-03 02:01:41 26 4
gpt4 key购买 nike

我有类似的记录

{
"who": "John",
"hobby": [
{"name": "gardening",
"skills": 2
},
{"name": "sleeping",
"skills": 3
},
{"name": "darts",
"skills": 2
}
]
}
,
{
"who": "Mary",
"hobby": [
{"name": "gardening",
"skills": 2
},
{"name": "volleyball",
"skills": 3
},
{"name": "kung-fu",
"skills": 2
}
]
}

我正在考虑构建一个可以回答以下问题的查询:“我们有多少技能=2 的爱好?”

上面例子的答案是 3 ( "gardening" 对两者都是通用的,并且每个都有另一个独特的)。

"query""query" + "aggs"我试过返回 ['hits']['hits']或 [ 'aggregations']['sources']['buckets']匹配数 文件 ,在上述情况下是两个(一个用于 "John" 和一个用于 "Mary" ,它们每个都满足查询)。

有没有办法构建一个查询,以便它返回与该查询匹配的字段总数(在上面的示例中:列表 "hobby" 的元素)? (字段,而不是文档)

注意:如果我的文件是平的:
{"who": "John", "name": "gardening", "skills": 2},
{"who": "John", "name": "sleeping", "skills": 3},
(...)
{"who": "Mary", "name": "kung-fu", "skills": 2}

然后是一个简单的 "query"匹配“ skills": 2 + "name" 上的聚合将完成工作

最佳答案

是的,您可以使用 nested type 来实现此目的。并使用 inner_hits 和/或 nested aggregations .

所以这是您应该使用的映射:

curl -XPUT localhost:9200/hobbies -d '{
"mappings": {
"hob": {
"properties": {
"who": {
"type": "string"
},
"hobby": {
"type": "nested", <--- the hobby list is of type nested
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
},
"skills": {
"type": "integer"
}
}
}
}
}
}
}

然后我们可以使用 _bulk 插入您的两个示例文档。像这样的端点:
curl -XPOST localhost:9200/hobbies/hob/_bulk -d '
{"index":{}}
{"who":"John", "hobby":[{"name": "gardening","skills": 2},{"name": "sleeping","skills": 3},{"name": "darts","skills": 2}]}
{"index":{}}
{"who":"Mary", "hobby":[{"name": "gardening","skills": 2},{"name": "volley-ball","skills": 3},{"name": "kung-fu","skills": 2}]}
'

最后,我们可以查询你有多少爱好的索引 skills: 2像这样:
curl -XPOST localhost:9200/hobbies/hob/_search -d '{
"_source": false,
"query": {
"nested": {
"path": "hobby",
"query": {
"term": {
"hobby.skills": 2
}
},
"inner_hits": {} <---- this will return only the matching nested fields with skills=2
}
},
"aggs": {
"hobbies": {
"nested": {
"path": "hobby"
},
"aggs": {
"skills": {
"filter": {
"term": {
"hobby.skills": 2
}
},
"aggs": {
"by_field": { <--- this will return a breakdown of the fields with skills=2
"terms": {
"field": "name"
}
}
}
}
}
}
}
}'

此查询将返回给您的是
  • hits部分,具有 skills: 2 的四个字段
  • aggs部分,具有 skills: 2 的 3 个不同字段的分割
  • 关于elasticsearch - 如何获取一条记录中多个匹配字段的命中数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31895541/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com