Elasticsearch / OpenSearch术语搜索逻辑或

发布于 2025-01-26 15:59:03 字数 1257 浏览 3 评论 0原文

我一直在挠头一段时间,查看OpenSearch文档和Stackoverflow问题。我该怎么做类似的事情:

选择文档在[1234,5678]中的studentId或[2468,1357]中的applicationID中。

只要StudentId与提供的值之一完全匹配,或者applicationId与所提供的值匹配之一,则该文档应包含在响应中。

当我想为单个字段搜索多个值并获得以下工作的确切匹配时:

{
    "must":[
        {
            "terms": {
                "studentId":["1234", "5678"]
            }
        }
    ]
}

这将在[1234,5678] 中找到studentId上的完全匹配。

如果我尝试添加条件,以便在[2468,1357] 中查找(逻辑或)applicationID,那么以下内容将无效:

{
    "must":[
        {
            "terms": {
                "studentId":["1234", "5678"]
            }
        },
        {
            "terms": {
                "applicationId":["2468", "1357"]
            }
        }
    ]
}

因为这将进行逻辑和在两个查询中。我想要逻辑或

我不能使用应该,因为这会返回不相关的结果。以下内容对我不起作用:

{
    "should":[
        {
            "terms": {
                "studentId":["1234", "5678"]
            }
        },
        {
            "terms": {
                "applicationId":["2468", "1357"]
            }
        }
    ]
}

这似乎返回所有结果,并以相关性排名。我发现,尽管这是术语搜索,但返回的结果实际上不匹配。

I have been scratching my head for a while looking at OpenSearch documentation and stackoverflow questions. How can I do something like this:

Select documents WHERE studentId in [1234, 5678] OR applicationId in [2468, 1357].

As long as studentId exactly matches one of the supplied values, or applicationId exactly matches one of the supplied values, then that document should be included in the response.

When I want to search for multiple values for a single field and get an exact match the following works:

{
    "must":[
        {
            "terms": {
                "studentId":["1234", "5678"]
            }
        }
    ]
}

This will find me exact matches on studentId in [1234, 5678].

If I try to add the condition to also look for (logical or) applicationId in [2468, 1357] then the following will not work:

{
    "must":[
        {
            "terms": {
                "studentId":["1234", "5678"]
            }
        },
        {
            "terms": {
                "applicationId":["2468", "1357"]
            }
        }
    ]
}

because this will do a logical and on the two queries. I want logical or.

I cannot use should because this returns irrelevant results. The following does not work for me:

{
    "should":[
        {
            "terms": {
                "studentId":["1234", "5678"]
            }
        },
        {
            "terms": {
                "applicationId":["2468", "1357"]
            }
        }
    ]
}

This seems to return all results, ranked by relevance. I find that the returned results do not actually match, despite the fact that this is a terms search.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2025-02-02 15:59:03

您可以尝试以下查询。

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "studentId":["1234", "5678"]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "applicationId":["2468", "1357"]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Can you try with following query..

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "studentId":["1234", "5678"]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "applicationId":["2468", "1357"]
                }
              }
            ]
          }
        }
      ]
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文