mCs
mCs

Reputation: 2921

How to query a specific shard for a document

I can use the preference to query ES shard #2 for tweet document with id 1 with URL like this:

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

How can I do this with Java API?

EDIT This:

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

lists the document while the both below returns NPE:

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

and

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

EDIT 2:

Why then if I have only two shards for this index for

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

I get NPE and for

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

I get "found:false":

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

and

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

gets the actual document with id1 and the complete _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: For:

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

I get the following responce

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

Upvotes: 1

Views: 1636

Answers (1)

Val
Val

Reputation: 217554

You can do it like this using the setPreference() call:

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

Upvotes: 4

Related Questions