如何查询文档的特定分片

时间:2016-09-01 11:21:52

标签: elasticsearch elasticsearch-java-api

我可以使用preference查询ES分片#2,查询标识为1的推文文档,其网址如下:

GET twitter/tweet/1/_search?preference=_shards:2

如何使用Java API执行此操作?

EDIT 这样:

GET /bookshop/book/_search?preference=_shards:0

列出文档,而下面两者都返回NPE:

GET /bookshop/book/id1?preference=_shards:0 //NPE

GetResponse getResponse =  client()
            .prepareGet(BOOK_INDEX_NAME, BOOK_TYPE_NAME, "id1")
            .setPreference("_shards:0")
            .execute().actionGet();

    System.out.print(getResponse.getSource()); //HERE IS WHERE I GET THE NPE

编辑2:

为什么然后我只有两个分片用于

的索引
 GET /bookshop/book/id1?preference=_shards:0 

我得到NPE和

> GET /bookshop/book/id1?preference=_shards:1

我得到"发现:false":

{
   "_index": "bookshop",
   "_type": "book",
   "_id": "id1",
   "found": false
}

GET /bookshop/book/_search?preference=_shards:0

获取包含id1和完整_source的实际文档?

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "bookshop",
            "_type": "book",
            "_id": "id1",
            "_score": 1,
            "_routing": "route1",
            "_source": {
               "title": "Clean COde",
               "author": "John Smith"
            }
         }
      ]
   }
}

EDIT3: 为:

GET /bookshop/book/id1?preference=_shards:0

我得到以下回应

{
   "error": {
      "root_cause": [
         {
            "type": "remote_transport_exception",
            "reason": "[master-1][127.0.0.1:9304][indices:data/read/get[s]]"
         }
      ],
      "type": "null_pointer_exception",
      "reason": null
   },
   "status": 500
}

1 个答案:

答案 0 :(得分:3)

你可以使用setPreference()调用这样做:

response = client()
    .prepareGet("twitter", "tweet", "1")
    .setRouting("route1")
    .setPreference("_shards:2")
    .execute().actionGet();