Solrj with Solr Suggester

2019-02-17 07:26发布

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?

标签: java solr solrj
3条回答
我命由我不由天
2楼-- · 2019-02-17 08:00

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

SpellCheckResponse spellCheckResponse=response.getSpellCheckResponse();

Check this link for more details

Ridiculous、
4楼-- · 2019-02-17 08:14

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();
    }
查看更多
登录 后发表回答