使用 Elasticsearch 从 Completion Suggester 检索文档 Java API
Retrieve documents from Completion Suggester using Elasticsearch Java API
当我使用 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
吗?
org.elasticsearch.search.suggest.Suggest suggest = searchResponse.getSuggest();
org.elasticsearch.search.suggest.completion.CompletionSuggestion suggestion = suggest.getSuggestion("product-suggest");
for (org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry entry : suggestion.getEntries()) {
for (org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option option : entry) {
SearchHit hit = option.getHit();
System.out.println(hit.getSourceAsString());
}
}
有两个问题:
- 您使用了错误的建议者名称
search
但它是 product-suggest
。
- 你没有施放它。因此,您只能从建议者那里获得父级 class 选项。将其分配给正确的类型后,您将从中获得
hits
_source
较早的回答:
import org.apache.http.util.EntityUtils; //import
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
String responseEntity = EntityUtils.toString(searchResponse.getEntity());
JsonObject obj = new JsonParser().parse(responseEntity).getAsJsonObject();
JsonArray completionSUggesterArr = obj.getAsJsonObject("suggest").getAsJsonArray("completion-suggester");
System.out.println(completionSUggesterArr.get(0).getAsJsonObject().getAsJsonArray("options"));
for(int i = 0; i < completionSUggesterArr.size(); i++) {
JsonArray arr = completionSUggesterArr.get(i).getAsJsonObject().get("options").getAsJsonArray();
System.out.println(arr.get(0).getAsJsonObject().get("_source"));
}
这个是针对低级别客户端的。
当我使用 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
吗?
org.elasticsearch.search.suggest.Suggest suggest = searchResponse.getSuggest();
org.elasticsearch.search.suggest.completion.CompletionSuggestion suggestion = suggest.getSuggestion("product-suggest");
for (org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry entry : suggestion.getEntries()) {
for (org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option option : entry) {
SearchHit hit = option.getHit();
System.out.println(hit.getSourceAsString());
}
}
有两个问题:
- 您使用了错误的建议者名称
search
但它是product-suggest
。 - 你没有施放它。因此,您只能从建议者那里获得父级 class 选项。将其分配给正确的类型后,您将从中获得
hits
_source
较早的回答:
import org.apache.http.util.EntityUtils; //import
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
String responseEntity = EntityUtils.toString(searchResponse.getEntity());
JsonObject obj = new JsonParser().parse(responseEntity).getAsJsonObject();
JsonArray completionSUggesterArr = obj.getAsJsonObject("suggest").getAsJsonArray("completion-suggester");
System.out.println(completionSUggesterArr.get(0).getAsJsonObject().getAsJsonArray("options"));
for(int i = 0; i < completionSUggesterArr.size(); i++) {
JsonArray arr = completionSUggesterArr.get(i).getAsJsonObject().get("options").getAsJsonArray();
System.out.println(arr.get(0).getAsJsonObject().get("_source"));
}
这个是针对低级别客户端的。