如何查询文档的特定分片

How to query a specific shard for a document

我可以使用 preference 查询 ES 分片 #2 以获取 ID 为 1 和 URL 的推文文档,如下所示:

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

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

编辑 这个:

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

列出文档,同时 returns 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

我得到 "found: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"
            }
         }
      ]
   }
}

编辑 3: 对于:

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
}

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

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