【问题标题】:Retrieve documents from Completion Suggester using Elasticsearch Java API使用 Elasticsearch Java API 从 Completion Suggester 检索文档
【发布时间】: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


    【解决方案1】:
    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());
         }
    }
    

    有两个问题:

    1. 您使用了错误的建议者名称 search,但它是 product-suggest。
    2. 你没有施放它。因此,您只能从建议者那里获得父类选项。一旦你将它分配给一个合适的类型,你会得到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"));
    }
    

    这个是给低级客户的。

    【讨论】:

    • SearchResponse 对象没有getEntity 方法
    • 没有方法searchResponse.getEntity(),您的示例中searchResponse是哪种类型?
    • @user13674325 您仍在引用较旧的答案。请检查第一个代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2021-01-24
    • 2015-07-08
    • 2019-07-28
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多