1

我可以使用首选项来查询 ES shard #2 以获取带有 id 的推文文档,1其 URL 如下所示:

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

如何使用 Java API 做到这一点?

编辑这个:

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

我得到“找到:假”:

{
   "_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
}
4

1 回答 1

4

您可以使用以下setPreference()调用来执行此操作:

response = client()
    .prepareGet("twitter", "tweet", "1")
    .setRouting("route1")
    .setPreference("_shards:2")
    .execute().actionGet();
于 2016-09-01T11:32:33.327 回答