查询SOLR

时间:2019-03-08 19:46:13

标签: java exception solr solrj

尝试查询我的SOLR 7.5.0独立集合时出现以下异常。我不确定问题出在哪里或如何调试它。我目前在我的Maven项目中使用solr 7.5.0依赖关系,并且尝试使用7.6.0和7.4.0,它们会产生相同的异常。我正在寻找其他方法来尝试找出问题所在以及如何解决。

导致异常的行是这个

QueryResponse resp = httpSolrClient.query(query, METHOD.GET );

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.solr.common.SolrDocumentList
    at org.apache.solr.client.solrj.response.QueryResponse.setResponse(QueryResponse.java:126)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:194)
    at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:974)
    at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:990)
    at com.leidos.textanalysis.solr.QuerySolr.main(QuerySolr.java:28)

下面是导致它的代码,对它的修补没有产生任何结果变化。

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    NoOpResponseParser responseParser = new NoOpResponseParser();
    responseParser.setWriterType("xml");

    HttpSolrClient.Builder builder = new HttpSolrClient.Builder();
    builder.withBaseSolrUrl("http://host:8983/solr/collection");

    RequestWriter writer = new RequestWriter();
    HttpSolrClient httpSolrClient = builder. build();
    httpSolrClient.setRequestWriter(writer);
    httpSolrClient.setParser(responseParser); 
    //message score
    SolrQuery query =new SolrQuery("*:*"); 

    QueryResponse resp = httpSolrClient.query(query, METHOD.GET ); //Exception thrown here

    SolrDocumentList theList =  resp.getResults();
    SolrDocument test = theList.get(0);
    System.out.println(test.toString());
}

在solr中查看QueryResponse的源代码,我们可以看到问题出在这里,它强制转换为SolrDocumentList。我找不到解决此问题或解决它的方法。我正在寻找可以尝试的方法和更多故障排除方法。谢谢。

public void setResponse( NamedList<Object> res )  {
super.setResponse( res );

// Look for known things
for( int i=0; i<res.size(); i++ ) {
  String n = res.getName( i );
  if( "responseHeader".equals( n ) ) {
    _header = (NamedList<Object>) res.getVal( i );
  }
  else if( "response".equals( n ) ) {
    _results = (SolrDocumentList) res.getVal( i ); //Exception thrown Here 
  }
  else if( "sort_values".equals( n ) ) {
    _sortvalues = (NamedList<ArrayList>) res.getVal( i );
  }
  else if( "facet_counts".equals( n ) ) {
    _facetInfo = (NamedList<Object>) res.getVal( i );
    // extractFacetInfo inspects _results, so defer calling it
    // in case it hasn't been populated yet.
  }
  else if( "debug".equals( n ) ) {
    _debugInfo = (NamedList<Object>) res.getVal( i );
    extractDebugInfo( _debugInfo );
  }
  else if( "grouped".equals( n ) ) {
    _groupedInfo = (NamedList<Object>) res.getVal( i );
    extractGroupedInfo( _groupedInfo );
  }
  else if("expanded".equals(n)) {
    NamedList map = (NamedList) res.getVal(i);
    _expandedResults = map.asMap(1);
  }
  else if( "highlighting".equals( n ) ) {
    _highlightingInfo = (NamedList<Object>) res.getVal( i );
    extractHighlightingInfo( _highlightingInfo );
  }
  else if ( "spellcheck".equals( n ) )  {
    _spellInfo = (NamedList<Object>) res.getVal( i );
    extractSpellCheckInfo( _spellInfo );
  }
  else if ("clusters".equals(n)) {
    _clusterInfo = (ArrayList<NamedList<Object>>) res.getVal(i);
    extractClusteringInfo(_clusterInfo);
  }
  else if ( "suggest".equals( n ) )  {
    _suggestInfo = (Map<String,NamedList<Object>>) res.getVal( i );
    extractSuggesterInfo(_suggestInfo);
  }
  else if ( "stats".equals( n ) )  {
    _statsInfo = (NamedList<Object>) res.getVal( i );
    extractStatsInfo( _statsInfo );
  }
  else if ( "terms".equals( n ) ) {
    _termsInfo = (NamedList<NamedList<Object>>) res.getVal( i );
    extractTermsInfo( _termsInfo );
  }
  else if ( "moreLikeThis".equals( n ) ) {
    _moreLikeThisInfo = (NamedList<SolrDocumentList>) res.getVal( i );
  }
  else if ( CursorMarkParams.CURSOR_MARK_NEXT.equals( n ) ) {
    _cursorMarkNext = (String) res.getVal( i );
  }
}
if(_facetInfo != null) extractFacetInfo( _facetInfo );

}

1 个答案:

答案 0 :(得分:0)

贷方向MatsLindh指出正确的方向。 NoOpResponseParser将响应放入NamedList对象中,而实际上未返回Response对象。参见this for more information on the NoOpResponseParser。 这里有一些解析器选项可以代替使用。它们都扩展了ResponseParser

BinaryResponseParser, DelegationTokenResponse.JsonMapResponseParser, InputStreamResponseParser, NoOpResponseParser, XMLResponseParser