如何在弹性搜索中使用multi_match与索引? [英] How to use multi_match with indices in elasticsearch?

查看:270
本文介绍了如何在弹性搜索中使用multi_match与索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个索引 USER 网址。我想根据索引在不同的字段上运行查询。



USER 索引中,查询应在名称 id 字段中搜索。
但必须在标题 ID 字段中执行 URL 搜索。

  POST / _search 
{
查询:{
indices:[
{
:[
URL
],
查询:{
multi_match:{
查询:SMU,
fields:[
title,
id
]
}
}
},
{
index:[
USER
],
query:{
multi_match:{
query:SMU,
fields:[
name,
id
]
}
}
}
]
}
}
/ pre>

上述查询不起作用。什么是使其工作所需的更改。
如何将多重搜索与索引搜索合并?

解决方案

索引查询在ES 5中已被弃用,但它仍然有效,您的结构不好,即需要将索引查询放在code> bool / filter 子句。

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

由于索引查询已被弃用,新的​​想法是使用简单的术语查询 _index 字段。尝试这样:

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


I have 2 indices USER and URL. I want to run a query on different fields based on the index.

In USER index, query should search in name and id field. But in URL search has to be performed on title and id field.

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

The above query is not working. What is the change required to make it work. How can I merge multi_match search with indices search?

解决方案

The indices query is deprecated in ES 5, but it still works, you just have a bad structure in yours, i.e. you need to put each indices query in a bool/filter clause.

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

Since the indices query is deprecated, the new idea is to use a simple term query on the _index field instead. Try this:

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

这篇关于如何在弹性搜索中使用multi_match与索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆