gpt4 book ai didi

elasticsearch - 将对象键与Elasticsearch匹配

转载 作者:行者123 更新时间:2023-12-02 22:25:52 25 4
gpt4 key购买 nike

我有包含如下字段的文档:

...
placements: [
"<id0>",
"<id1>",
"<id2>"
]
...

为了获取其位置包含id0的所有文档,我可以这样做:
{
"constant_score" : {
"filter" : {
"term" : { "placements" : "<id0>"}
}
}
}

现在,我需要将此数组更改为一个对象,以便将id映射到状态(字符串):
...
placements: {
"<id0>": "active",
"<id1>": "active",
"<id2>": "paused"
}
...

我想知道是否仍然有一种方法可以检查给定的id是否为places对象的键?

最佳答案

我认为exists filter是您想要的。

我创建了一个索引并添加了两个文档,其中一个具有"<id2>"属性,而另一个则没有:

curl -XPUT "http://localhost:9200/test_index/"

curl -XPUT "http://localhost:9200/test_index/docs/1" -d'
{
"name": "doc1",
"placements": {
"<id0>": "active",
"<id1>": "active",
"<id2>": "paused"
}
}'

curl -XPUT "http://localhost:9200/test_index/docs/2" -d'
{
"name": "doc2",
"placements": {
"<id0>": "paused",
"<id1>": "active"
}
}'

然后我可以使用 "exists"过滤器进行查询:
curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
"query": {
"constant_score": {
"filter": {
"exists": {
"field": "<id2>"
}
}
}
}
}'

我得到正确的结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "docs",
"_id": "1",
"_score": 1,
"_source": {
"name": "doc1",
"placements": {
"<id0>": "active",
"<id1>": "active",
"<id2>": "paused"
}
}
}
]
}
}

这是一个可以运行的示例:

http://sense.qbox.io/gist/e727360fc472a9c0a6a2a1cf7050d1b42f6cd773

关于elasticsearch - 将对象键与Elasticsearch匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21437205/

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