簡體   English   中英

ElasticSearch / 使用邏輯或的 OpenSearch 術語搜索

[英]ElasticSearch / OpenSearch term search with logical OR

我一直在撓頭看 OpenSearch 文檔和 stackoverflow 問題。 我怎樣才能做這樣的事情:

Select 文檔WHERE studentId in [1234, 5678] OR applicationId in [2468, 1357]

只要studentId與提供的值之一完全匹配,或者applicationId與提供的值之一完全匹配,那么該文檔就應該包含在響應中。

當我想為單個字段搜索多個值並獲得完全匹配時,可以執行以下操作:

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

這會studentId in [1234, 5678]完全匹配的內容。

如果我嘗試添加條件以也在applicationId in [2468, 1357]則以下內容將不起作用:

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

因為這將對兩個查詢執行邏輯和操作。 我想要邏輯或

我不能使用should因為這會返回不相關的結果。 以下對我不起作用:

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

這似乎返回所有結果,按相關性排序。 我發現返回的結果實際上並不匹配,盡管這是一個terms搜索。

你能試試下面的查詢嗎..

{
  "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