【发布时间】: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