当尝试使用/ suggest处理程序运行solrj程序时,出现类强制转换异常。我能够从Solr Admin UI中获得建议响应,但是当从solrj客户端尝试相同的响应时,就会出现上述异常。
solr-config.xml条目如下:
<searchComponent name="suggest" class="solr.SuggestComponent">
<lst name="suggester">
<str name="name">mySuggester</str>
<str name="lookupImpl">FuzzyLookupFactory</str>
<str name="dictionaryImpl">DocumentDictionaryFactory</str>
<str name="field">suggestfield</str>
<str name="suggestAnalyzerFieldType">text_general</str>
</lst>
</searchComponent>
<requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy">
<lst name="defaults">
<str name="suggest">true</str>
<str name="suggest.count">10</str>
<str name="suggest.dictionary">mySuggester</str>
</lst>
<arr name="components">
<str>suggest</str>
</arr>
</requestHandler>
schema.xml包含字段
<field name="suggestfield" type="text_general" indexed="true" stored="true"/>
这是多个字段的copyField。
Java代码如下:
String url = "http://localhost:8983/solr/testwidget";
HttpSolrClient client = new HttpSolrClient.Builder(url).build();
client.setParser(new XMLResponseParser());
SolrQuery query = new SolrQuery();
query.setRequestHandler("/suggest");
query.setParam("suggest.build", true);
query.setParam("suggest", true);
query.setParam("suggest.dictionary", "mySuggester");
query.setParam("suggest.q", "acc");
QueryResponse response = client.query(query);
System.out.println(response.getSuggesterResponse().getSuggestedTerms());
异常如下:
Exception in thread "main" java.lang.ClassCastException: org.apache.solr.common.util.SimpleOrderedMap cannot be cast to java.util.Map
at org.apache.solr.client.solrj.response.QueryResponse.setResponse(QueryResponse.java:161)
at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:194)
at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:942)
at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:957)
at com.intellectdesign.canvas.solr.SolrjTest.main(SolrjTest.java:41)
最佳答案
我不确定为什么要设置XMLResponseParser
然后再请求wt=json
。我认为https://github.com/apache/lucene-solr/blob/master/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestSuggesterResponse.java可以用作开始的示例。