gpt4 book ai didi

elasticsearch - bool 并在ElasticSearch中搜索属性

转载 作者:行者123 更新时间:2023-12-03 00:36:10 25 4
gpt4 key购买 nike

我在ES中放了一个非常小的文档数据集:

    {"id":1, "name": "John", "team":{"code":"red", "position":"P"}}

{"id":2, "name": "Jack", "team":{"code":"red", "position":"S"}}

{"id":3, "name": "Emily", "team":{"code":"green", "position":"P"}}

{"id":4, "name": "Grace", "team":{"code":"green", "position":"P"}}

{"id":5, "name": "Steven", "team":[
{"code":"green", "position":"S"},
{"code":"red", "position":"S"}]}

{"id":6, "name": "Josephine", "team":{"code":"red", "position":"S"}}

{"id":7, "name": "Sydney", "team":[
{"code":"red", "position":"S"},
{"code":"green", "position":"P"}]}

我想向ES查询在红色团队中职位为P的人员。
随着要求
curl -XPOST 'http://localhost:9200/teams/aff/_search' -d '{
"query": {
"bool": {
"must": [
{
"match": {
"team.code": "red"
}
},
{
"match": {
"team.position": "P"
}
}
]
}
}
}'

我有一个 错误的结果。
ES给
"name": "John",
"team":
{ "code": "red", "position": "P" }
and
"name": "Sydney",
"team":
[
{ "code": "red", "position": "S"},
{ "code": "green", "position": "P"}
]

对于最后一个条目,ES在第一条记录中使用属性代码= red,在第二条记录中使用属性position = P。

如何指定搜索必须匹配(在嵌套记录列表之内或之内)同一记录 中的两个两个词?

实际上,好的答案只有John的文档1。

这是创建数据集的要点:
https://gist.github.com/flrt/4633ef59b9b9ec43d68f

提前致谢

最佳答案

当您将文档编入索引时

{
"name": "Sydney",
"team": [
{"code": "red", "position": "S"},
{"code": "green","position": "P"}
]
}

ES为您的字段隐式创建 inner object(在特定示例中为 team)并将其展平为类似
{
'team.code': ['red', 'green'],
'team.position: ['S', 'P']
}

这样您会失去订单。为了避免这种情况,您需要显式放置 nested 映射,一如既往地为文档建立索引,并使用 nested 查询来查询它们

所以这
PUT so/nest/_mapping
{
"nest": {
"properties": {
"team": {
"type": "nested"
}
}
}
}

PUT so/nest/
{
"name": "Sydney",
"team": [
{
"code": "red",
"position": "S"
},
{
"code": "green",
"position": "P"
}
]
}

GET so/nest/_search
{
"query": {
"nested": {
"path": "team",
"query": {
"bool": {
"must": [
{
"match": {
"team.code": "red"
}
},
{
"match": {
"team.position": "P"
}
}
]
}
}
}
}
}

将导致空点击。

有关关系管理的进一步阅读: https://www.elastic.co/blog/managing-relations-inside-elasticsearch

关于elasticsearch - bool 并在ElasticSearch中搜索属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31923240/

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