gpt4 book ai didi

elasticsearch - 使用 Elasticsearch Java API 从 Completion Suggester 中检索文档

转载 作者:行者123 更新时间:2023-12-02 23:44:17 25 4
gpt4 key购买 nike

当我使用 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 .
  • 你不是在类型转换它。因此,您只能从建议者那里获得父类选项。将其分配给适当的类型后,您将获得 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"));
    }
    这是针对低级别客户的。

    关于elasticsearch - 使用 Elasticsearch Java API 从 Completion Suggester 中检索文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62590489/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com