ElasticSearch:使用单个查询在单个字段上查询列表的每个元素

ElasticSearch : Query Each element of list on a single field using a single Query

我对 Elastic Search 还很陌生。我有一个用例,我想在弹性搜索中的单个字段上搜索列表的所有元素,如果存在其中任何一个,则检索文档。

例如:

List["Shruti","shreya","shreshtha"]

并在

上搜索

"user.Name" 认为返回具有这些名称之一的所有文档。

你能帮我解答一下吗。 提前致谢!

这是一个简单的例子。

我创建了一个带有显式映射的简单索引,如下所示:

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "name": {
               "type": "string"
            }
         }
      }
   }
}

然后用bulk index操作添加了几个文档:

POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"name":"Shruti"}
{"index":{"_id":2}}
{"name":"shreya"}
{"index":{"_id":3}}
{"name":"shreshtha"}
{"index":{"_id":4}}
{"name":"some other name"}
{"index":{"_id":5}}
{"name":"yet another"}

现在我可以通过几种不同的方式进行查询。像这样的默认 match query 将起作用:

POST /test_index/_search
{
   "query": {
      "match": {
         "name": "Shruti shreya shreshtha"
      }
   }
}

您也可以使用 terms filter as shown below. But be careful here. Notice that the terms are all lower-case. This is because, since we didn't specify an analyzer in the mapping, the standard analyzer was used,它将标记转换为小写。

POST /test_index/_search
{
   "filter": {
      "terms": {
         "name": [
            "shruti",
            "shreya",
            "shreshtha"
         ]
      }
   }
}

这是我在示例中使用的代码:

http://sense.qbox.io/gist/e4f5a276aa59fb62e2e0207eb6199070014f0650