簡體   English   中英

如何在 Elasticsearch 中使用 multi_match 查詢傳遞多個值以在多個記錄中搜索多個字段

[英]How to pass multiple values to search in multiple fields over multiple records using multi_match query in Elasticsearch

{
 "query": {
  "bool": {
    "must": {
      "bool": {
        "must": [
          {
            "multi_match": {
              "query": [
                "869336","45345345"
              ],
              "type": "phrase_prefix",
              "fields": [
                "id",
                "accountNo",
                "moblileNo"
              ]
            }
          }
        ],
        "should": [],
        "must_not": []
      }
    },
    "filter": {
      "bool": {
        "must": {
          "bool": {
            "must": [],
            "should": [],
            "must_not": []
          }
        }
      }
    }
  }
}

}

只需要使用多重匹配查詢。我在查詢中提到了一些示例字段。當我在 postman 上運行它時出現以下錯誤:[multi_match] unknown token [START_ARRAY] after [query]

對於錯誤,因為query只需要一個輸入字符串。

它可以有多個值,例如"query": "869336 45345345" ,但是值之間用空格分隔。 這是如何工作的,您可能可以通過此鏈接go 。

現在研究您的場景,假設您想要對這兩個值(即86933645345345 )應用短語匹配查詢,您只需將其單獨的多重匹配查詢中的值分開即可。

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": {
        "bool": {
          "must": [
            {
              "multi_match": {
                "query": "869336",
                "type": "phrase_prefix",
                "fields": [
                  "accountNo",
                  "moblileNo"
                ]
              }
            },
            {
              "multi_match": {
                "query": "45345345",
                "type": "phrase_prefix",
                "fields": [
                  "accountNo",
                  "moblileNo"
                ]
              }
            }
          ],
          "should": [],
          "must_not": []
        }
      },
      "filter": {
        "bool": {
          "must": {
            "bool": {
              "must": [],
              "should": [],
              "must_not": []
            }
          }
        }
      }
    }
  }
}

現在,如果您不想應用phrase_prefix ,而是想返回在任何字段中具有兩個值的所有文檔,您可以簡單地編寫如下查詢:

POST my-multimatch-index/_search
{
  "query": {
    "bool": {
      "must": {
        "bool": {
          "must": [
            {
              "multi_match": {
                "query": "869336 45345345",
                "fields": [
                  "accountNo",
                  "moblileNo"
                ],
                "type": "cross_fields",              <--- Note this
                "operator": "and"                    <--- This would mean only return those documents having both these value. 
              } 
            }
          ],
          "should": [],
          "must_not": []
        }
      },
      "filter": {
        "bool": {
          "must": {
            "bool": {
              "must": [],
              "should": [],
              "must_not": []
            }
          }
        }
      }
    }
  }
}

請注意上述評論以及我如何使用cross-fields 最好通過 go 的鏈接獲得更好的理解。

無論如何,請隨時提出問題,我希望這會有所幫助!

暫無
暫無

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

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