【问题标题】:How to query a specific shard for a document如何查询文档的特定分片
【发布时间】:2016-09-01 11:21:51
【问题描述】:

我可以使用preference 查询 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
}

【问题讨论】:

  • 您确定您的网址吗?您是引用单个文档(id=1)还是愿意进行搜索(即/_search)?你不能两者都做。
  • 我想在单个分片中搜索具有给定 ID 的 tweet 文档。可行吗?
  • 您需要删除_search 端点。我修改了答案

标签: elasticsearch elasticsearch-java-api


【解决方案1】:

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

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

【讨论】:

  • 谢谢你,这就是我要找的 :) 赞成并接受
  • hmm with GET /twitter/tweet/_search?preference=_shards:2 我得到了文档,但是当我尝试运行 response.getSource() 时。我得到NPE。可能是什么问题?
  • 您是在搜索而不是获取文档?使用您正在使用的确切 Java 代码更新您的问题
  • 但是,如果我搜索一个文档并使用我所追求的文档进行搜索响应,那么该文档的 id 字段上的 get 是否也应该给我带来相同的结果文件?
  • 您可能确实需要将setRouting("route1") 添加到您的通话中
猜你喜欢
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
相关资源
最近更新 更多