gpt4 book ai didi

elasticsearch - 如何在 ElasticSearch 中做嵌套的 AND 和 OR 过滤器?

转载 作者:行者123 更新时间:2023-11-29 02:44:29 25 4
gpt4 key购买 nike

我的过滤器按类别组合在一起。我想检索文档,其中文档可以匹配类别中的任何过滤器,但如果设置了两个(或更多)类别,则文档必须匹配所有类别中的任何过滤器。

如果用伪 SQL 编写,它将是:

SELECT * FROM Documents WHERE (CategoryA = 'A') AND (CategoryB = 'B' OR CategoryB = 'C')

我试过这样的嵌套过滤器:

{
"sort": [{
"orderDate": "desc"
}],
"size": 25,
"query": {
"match_all": {}
},
"filter": {
"and": [{
"nested": {
"path":"hits._source",
"filter": {
"or": [{
"term": {
"progress": "incomplete"
}
}, {
"term": {
"progress": "completed"
}
}]
}
}
}, {
"nested": {
"path":"hits._source",
"filter": {
"or": [{
"term": {
"paid": "yes"
}
}, {
"term": {
"paid": "no"
}
}]
}
}
}]
}
}

但显然我不太了解 ES 语法。这是在正确的轨道上还是我需要使用其他过滤器?

最佳答案

这应该是它(从给定的伪 SQL 翻译而来)

{
"sort": [
{
"orderDate": "desc"
}
],
"size": 25,
"query":
{
"filtered":
{
"filter":
{
"and":
[
{ "term": { "CategoryA":"A" } },
{
"or":
[
{ "term": { "CategoryB":"B" } },
{ "term": { "CategoryB":"C" } }
]
}
]
}
}
}
}

我知道你没有提到方面,只是为了完整起见:

您也可以使用 filter 作为基础(就像您所做的那样)而不是 filtered query(就像我所做的那样)。生成的 json 几乎相同,区别在于:

  • 过滤查询将同时过滤主要结果和方面
  • 过滤器将只过滤主要结果而不是方面。

最后,嵌套过滤器(您尝试使用的)并不像您认为的那样与“嵌套过滤器”相关,而是与嵌套文档(父子)的过滤有关

关于elasticsearch - 如何在 ElasticSearch 中做嵌套的 AND 和 OR 过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22811364/

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