gpt4 book ai didi

elasticsearch - Elasticsearch ,对对象数组进行 bool 查询

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

该文档具有这样的字段。

"device_versions": [
{
"name": "android",
"min_major_ver": 3,
"min_minor_ver": 54,
"min_patch_ver": 0
},
{
"name": "ios",
"min_major_ver": 2,
"min_minor_ver": 59,
"min_patch_ver": 0
}
]

我想写这样的查询

(device_versions.name ='android'AND device_versions.min_major_ver = 3 AND device_versions.min_minor_ver = 55)

我写了以下查询-
"must": [
{
"term": {
"device_versions.name": "android"
}
},
{
"range": {
"device_versions.min_major_ver": {
"from": 3,
"include_lower": true,
"include_upper": true,
"to": null
}
}
},
{
"range": {
"device_versions.min_minor_ver": {
"from": 55,
"include_lower": true,
"include_upper": true,
"to": null
}
}
}
]

上述查询的问题是:最后一个范围查询与设备名称为'ios'的数组元素匹配

我需要的是:在同一数组元素上应用3个条件。设备=“android” AND min_major_ver = 55 AND min_minor_ver = 55

最佳答案

您的映射应启用嵌套,否则对象将变平

PUT my_index/_doc/1
{
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}

将存储为
{
"group" : "fans",
"user.first" : [ "alice", "john" ],
"user.last" : [ "smith", "white" ]
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

启用嵌套后,您的查询应为
{
"query": {
"nested": {
"path": "device_versions",
"query": {
"bool": {
"must": [
{
"term": {
"device_versions.name": "android"
}
},
{
"range": {
"device_versions.min_major_ver": {
"from": 3,
"include_lower": true,
"include_upper": true,
"to": null
}
}
},
{
"range": {
"device_versions.min_minor_ver": {
"from": 55,
"include_lower": true,
"include_upper": true,
"to": null
}
}
}
]
}
}
}
}
}

关于elasticsearch - Elasticsearch ,对对象数组进行 bool 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59489238/

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