如何进行多次“匹配"或“match_phrase"ElasticSearch 中的值 [英] How to do multiple "match" or "match_phrase" values in ElasticSearch

查看:82
本文介绍了如何进行多次“匹配"或“match_phrase"ElasticSearch 中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以 ElasticSearch 有 terms 查询,它允许我提供多个值并返回一个文档列表,其中字段 X 匹配这些值中的任何一个.

So ElasticSearch has the terms query, which lets me supply multiple values and return a ist of documents where field X matches any of those values.

但我想对 ma​​tch_phrase 做同样的事情 - 即返回字段 X 包含不区分大小写匹配的文档,其中包含空格的术语.我目前使用 or 过滤器(见下文).但考虑到条款已经做了类似的事情,这似乎是我想要的一种非常冗长的方式.

But I want to do the same thing with match_phrase - i.e. return documents where field X contains a case insensitive match for a term with spaces. I currently do it my using an or filter (see below). But that seems like a very verbose way of doing what I want, considering that terms does something similar already.

当前方法

在单个字段中搜索三个值之一的查询应该是 33 行长,这似乎很荒谬.

It seems ridiculous that a query searching a single field for one of three values should be 33 lines long.

{
  "query": {
    "filtered": {
       "filter": {
           "or": {
              "filters": [
                 {
                     "query": {
                         "match_phrase": {
                            "myField1": "My first search phrase"
                         }
                     }
                 },
                 {
                     "query": {
                         "match_phrase": {
                            "myField1": "My second search phrase"
                         }
                     }
                 },
                 {
                     "query": {
                         "match_phrase": {
                            "myField1": "My third search phrase"
                         }
                     }
                 }
              ]
           }
       }
    }
  }
}

推荐答案

经过一个漫长的夜晚试图自己解决这个问题后,我想出了这个:

After a long night trying to figure this out myself I came up with this:

"query" : {
        "bool": {
            "should": [
               {
                   "match_phrase": {
                      "myField1": "My first search phrase"
                   }
               },
               {
                   "match_phrase": {
                      "myField1": "My second search phrase"
                   }
               }
            ]
        }
    }

参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

这篇关于如何进行多次“匹配"或“match_phrase"ElasticSearch 中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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