Elasticsearch:在一个请求中获取多个指定文档?

时间:2015-03-14 22:42:03

标签: elasticsearch

我是Elasticsearch的新手,希望知道这是否可行。

基本上,我在"代码"中有值。多个文件的财产。每个文档在此属性中都具有唯一值。现在我有多个文档的代码,希望通过提供多个代码在一个请求中检索它们。

这在Elasticsearch中是否可行?

问候。

修改

这是该字段的映射:

"code" : { "type" : "string", "store": "yes", "index": "not_analyzed"},

此属性的两个示例值:

0Qr7EjzE943Q
GsPVbMMbVr4s

在一个请求中检索两个文档的ES语法是什么?

1 个答案:

答案 0 :(得分:1)

首先,您可能不希望映射中显示"store":"yes",除非您_source已停用(请参阅this post)。

所以,我创建了一个这样的简单索引:

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "code": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

使用bulk API添加了两个文档:

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"code":"0Qr7EjzE943Q"}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"code":"GsPVbMMbVr4s"}

我可以通过多种方式检索这两个文档。最直接的,特别是因为没有分析该字段,可能是terms query

POST /test_index/_search
{
    "query": {
        "terms": {
           "code": [
              "0Qr7EjzE943Q",
              "GsPVbMMbVr4s"
           ]
        }
    }
}

返回两份文件:

{
   "took": 21,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.04500804,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.04500804,
            "_source": {
               "code": "0Qr7EjzE943Q"
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 0.04500804,
            "_source": {
               "code": "GsPVbMMbVr4s"
            }
         }
      ]
   }
}

以下是我使用的代码:

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