【发布时间】:2020-06-26 08:03:13
【问题描述】:
当我使用 curl 执行完成提示请求时,在响应中我还可以找到文档
curl -X GET 'localhost:9200/shop/_search?pretty&size=20' -H 'Content-Type: application/json' -d'{
"suggest": {
"product-suggest" : {
"prefix" : "phone",
"completion" : {
"field" : "title",
"skip_duplicates": true
}
}
}
}'
回复
{
"took" : 15,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "phone",
"offset" : 0,
"length" : 12,
"options" : [
{
"text" : "Phone",
"_index" : "shop",
"_type" : "_doc",
"_id" : "a02fd264-c25c-4634-8bd9-e275a293ce1d",
"_score" : 1.0,
"_source" : {
"title" : "Phone",
"price" : 219.99
}
}
]
}
]
}
}
因此,在响应中,我可以在“_source”字段中找到文档。
使用 Java API
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.suggest(new SuggestBuilder()
.addSuggestion("product-suggest",
new CompletionSuggestionBuilder("title").prefix("phone").skipDuplicates(true)));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
searchResponse.getSuggest().getSuggestion("product-suggest").getEntries().get(0).getOptions(.get(0).getText();
所以,我可以找到检索文本getText的方法,但是没有检索源的方法。
有没有办法检索source?
【问题讨论】:
标签: elasticsearch elasticsearch-java-api