简体   繁体   English

ElasticSearch / 使用逻辑或的 OpenSearch 术语搜索

[英]ElasticSearch / OpenSearch term search with logical OR

I have been scratching my head for a while looking at OpenSearch documentation and stackoverflow questions.我一直在挠头看 OpenSearch 文档和 stackoverflow 问题。 How can I do something like this:我怎样才能做这样的事情:

Select documents WHERE studentId in [1234, 5678] OR applicationId in [2468, 1357] . Select 文档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.只要studentId与提供的值之一完全匹配,或者applicationId与提供的值之一完全匹配,那么该文档就应该包含在响应中。

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] .这会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:如果我尝试添加条件以也在applicationId in [2468, 1357]则以下内容将不起作用:

{
    "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.我不能使用should因为这会返回不相关的结果。 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.我发现返回的结果实际上并不匹配,尽管这是一个terms搜索。

Can you try with following query..你能试试下面的查询吗..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM