簡體   English   中英

如何使用 Elasticsearch 編寫條件查詢?

[英]How to write a conditional query with Elasticsearch?

我有一個簡單的 Elasticsearch JSON 查詢,如下所示:

"query": {
  "bool": {
    "must": { "match": { "id": "1" }} ,
    "must": { "match": { "tags.name": "a1"}}
   }
}

僅當值(在本例中為“a1”)不為空時,我如何才能執行第二個“必須”條件?

您可以使用以下方法實現它 -

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "id": "1"
          }
        },
        {
          "bool": {
            "should": [
              {
                "missing": {
                  "field": "tags.name"
                }
              },
              {
                "match": {
                  "tags.name": "a1"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

我認為在最新版本的 elasticsearch 中不能使用“missing”。 但是可以改用條件從句。 https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-template.html#_conditional_clauses

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM