Solrj with Solr Suggester

强颜欢笑 提交于 2019-12-01 02:42:28

问题


What is the correct way of getting results from solrj using Solr Suggester?

This is my request:

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);

However I found it extremely difficult to get the response. The way I got the response is with this:

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

This seems to assume a lot about the objects I am getting from the response and will be difficult to anticipate errors.

Is there a better and cleaner way than this?


回答1:


In new versions have a SuggesterResponse:

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




回答2:


Best option is to get it as List, below code worked for me

    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();
    }



回答3:


You can get the suggestions via the SpellCheckResponse by doing the following

SpellCheckResponse spellCheckResponse=response.getSpellCheckResponse();

Check this link for more details



来源:https://stackoverflow.com/questions/28486038/solrj-with-solr-suggester

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!