如何在 elasticsearch 中使用 multi_match 和索引?

How to use multi_match with indices in elasticsearch?

我有 2 个索引 USERURL。我想 运行 基于索引对不同字段进行查询。

USER 索引中,查询应该在 nameid 字段中搜索。 但在 URL 中,必须在 titleid 字段上执行搜索。

POST /_search    
    {  
       "query":{  
          "indices":[  
             {  
                "indices":[  
                   "URL"
                ],
                "query":{  
                   "multi_match":{  
                      "query":"SMU ",
                      "fields":[  
                         "title",
                         "id"
                      ]
                   }
                }
             },
             {  
                "indices":[  
                   "USER"
                ],
                "query":{  
                   "multi_match":{  
                      "query":"SMU ",
                      "fields":[  
                         "name",
                         "id"
                      ]
                   }
                }
             }
          ]
       }
    }

以上查询无效。使其工作所需的更改是什么。 如何将 multi_match 搜索与索引搜索合并?

indices 查询在 ES 5 中已弃用,但它仍然有效,只是你的结构不好,即你需要将每个 indices 查询放在 bool/filter 条款。

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "indices": {
            "indices": [
              "URL"
            ],
            "query": {
              "multi_match": {
                "query": "SMU ",
                "fields": [
                  "title",
                  "id"
                ]
              }
            }
          }
        },
        {
          "indices": {
            "indices": [
              "USER"
            ],
            "query": {
              "multi_match": {
                "query": "SMU ",
                "fields": [
                  "name",
                  "id"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

由于 indices 查询已弃用,新想法是在 _index 字段上使用简单的 term 查询。试试这个:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "_index": "URL"
                }
              },
              {
                "multi_match": {
                  "query": "SMU ",
                  "fields": [
                    "title",
                    "id"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "_index": "USER"
                }
              },
              {
                "multi_match": {
                  "query": "SMU ",
                  "fields": [
                    "name",
                    "id"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}