gpt4 book ai didi

具有多个字段的 Elasticsearch 等效 SQL In Query

转载 作者:行者123 更新时间:2023-12-04 08:33:43 26 4
gpt4 key购买 nike

我正在寻找一个 Elasticsearch 等效的 SQL In 查询,如下所示

SELECT * from table
WHERE (section , class) in (('a','1'), ('b', '2'))
我知道如何在 elasticsearch 中的单个字段中使用查询
SELECT * FROM table WHERE class IN ('1', '2');
Elasticsearch 查询 -
{
"query" : {
"bool" : {
"filter" : {
"terms" : {
"class" : ['1', '2']
}
}
}
}
}
我的实际问题陈述 -
示例索引数据:
[
{
"_index" : "some_index",
"_type" : "_doc",
"_id" : "41",
"_score" : 1.0,
"_source" : {
"class" : "1",
"section" : "a",
"attribute_3" : "hello world"
},
{
"_index" : "some_index",
"_type" : "_doc",
"_id" : "42",
"_score" : 1.0,
"_source" : {
"class" : "2",
"section" : "a",
"attribute_3" : "hello world"
},
{
"_index" : "some_index",
"_type" : "_doc",
"_id" : "43",
"_score" : 1.0,
"_source" : {
"class" : "1",
"section" : "b",
"attribute_3" : "hello world"
},
{
"_index" : "some_index",
"_type" : "_doc",
"_id" : "44",
"_score" : 1.0,
"_source" : {
"class" : "2",
"section" : "b",
"attribute_3" : "hello world"
},
{
"_index" : "some_index",
"_type" : "_doc",
"_id" : "45",
"_score" : 1.0,
"_source" : {
"class" : "3",
"section" : "b",
"attribute_3" : "hello world"
}
]
我想对数据使用过滤器,其中(类为 1 AND 部分为 a)或(类为 2 AND 部分为 b)
注意:我正在动态地准备这个“或”组合,它将是两个以上的组合。
我预期的搜索结果应该是 -
[{
"_index" : "some_index",
"_type" : "_doc",
"_id" : "41",
"_score" : 1.0,
"_source" : {
"class" : "1",
"section" : "a",
"attribute_3" : "hello world"
},
{
"_index" : "some_index",
"_type" : "_doc",
"_id" : "44",
"_score" : 1.0,
"_source" : {
"class" : "2",
"section" : "b",
"attribute_3" : "hello world"
}]

最佳答案

这将转化为:

{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"a": 0
}
},
{
"term": {
"b": 9
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"a": 0
}
},
{
"term": {
"b": 4
}
}
]
}
}
]
}
}
}
但如果 a总是 0正如您在示例中提到的,查询可以改写为:
{
"query": {
"bool": {
"must": [
{
"term": {
"a": 0
}
},
{
"terms": {
"b": [
9,
4
]
}
}
]
}
}
}

关于具有多个字段的 Elasticsearch 等效 SQL In Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64906999/

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