gpt4 book ai didi

elasticsearch - Elasticsearch:如何使用OR条件查询数组字段?

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

我有一些ElasticSearch文档,其中包含一个Array字段。

我希望能够搜索整个索引
要查找首先包含数组字段的文档,
如果有,请找到包含要搜索值的文档。

以下是我的数据-

{
"took" : 633,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "demo",
"_type" : "1",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "anon",
"nick_name" : "an",
"hobbies" : [
"reading",
"writing"
]
}
},
{
"_index" : "demo",
"_type" : "1",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "anon2",
"nick_name" : "an2",
"hobbies" : [
"playing",
"studying"
]
}
},
{
"_index" : "demo",
"_type" : "1",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "anon3",
"nick_name" : "an3",
"hobbies" : [
"playing",
"writing"
]
}
},
{
"_index" : "demo",
"_type" : "1",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "anon4",
"nick_name" : "an4"
}
}
]
}
}

我想获取那些其兴趣爱好数组包含 “正在播放”或“学习” 值的文档
即。文件2和3

以下是我的数据创建查询-
POST /demo/_doc/1
{
"name":"anon",
"nick_name":"an",
"hobbies":["reading","writing"]
}

POST /demo/_doc/2
{
"name":"anon2",
"nick_name":"an2",
"hobbies":["playing","studying"]
}

POST /demo/_doc/3
{
"name":"anon3",
"nick_name":"an3",
"hobbies":["playing","writing"]
}

POST /demo/_doc/4
{
"name":"anon4",
"nick_name":"an4"
}

GET demo/_search
{
"query": {
"match_all": {}
}
}

以下查询可以让我进行一次搜索,但是如何实现“或”条件?
GET demo/_search
{
"query": {
"match": {
"hobbies": "playing"
}
}
}

最佳答案

我现在无法对其进行测试,但是可能是这样的:

GET demo/_search
{
"query": {
"bool": {
"should": [
{
"terms": {
"hobbies": [
"playing",
"studying"
]
}
}
]
}
}
}

如果您的 hobbies字段是 keyword类型。

编辑:
遵循 documentation中的示例,它甚至可能更容易:
GET demo/_search
{
"query" : {
"terms" : {
"hobbies" : ["playing", "studying"]
}
}
}

关于elasticsearch - Elasticsearch:如何使用OR条件查询数组字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57406756/

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