Solrj 与 Solr Suggester

Solrj with Solr Suggester

使用 Solr Suggester 从 solrj 获取结果的正确方法是什么?

这是我的要求:

SolrQuery query = new SolrQuery();
query.setRequestHandler("/suggest");
query.setParam("suggest", "true");
query.setParam("suggest.build", "true");
query.setParam("suggest.dictionary", "mySuggester");
query.setParam("suggest.q", "So");
QueryResponse response = server.query(query);

然而我发现很难得到回应。我得到回应的方式是这样的:

NamedList obj = (NamedList)((Map)response.getResponse().get("suggest")).get("mySuggester");
SimpleOrderedMap obj2 = (SimpleOrderedMap) obj.get("So");
List<SimpleOrderedMap> obj3 = (List<SimpleOrderedMap>) obj2.get("suggestions");

这似乎对我从响应中获得的对象做了很多假设,并且很难预测错误。

还有比这更好更干净的方法吗?

您可以通过 SpellCheckResponse 获得建议,方法是执行以下操作

SpellCheckResponse spellCheckResponse=response.getSpellCheckResponse();

查看此 link 了解更多详情

在新版本中有一个 SuggesterResponse:

https://lucene.apache.org/solr/5_3_1/solr-solrj/org/apache/solr/client/solrj/response/SuggesterResponse.html

最好的选择是将其作为列表获取,下面的代码对我有用

    HttpSolrClient solrClient = new HttpSolrClient(solrURL);
    SolrQuery query = new SolrQuery();
    query.setRequestHandler("/suggest");
    query.setParam("suggest.q", "Ins");
    query.setParam("wt", "json");
    try {

        QueryResponse response = solrClient.query(query);
        System.out.println(response.getSuggesterResponse().getSuggestedTerms());
        List<String> types=response.getSuggesterResponse().getSuggestedTerms().get("infixSuggester");
        System.out.println(types);
    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
    }