How can I transform SolrQuery(SOLRJ) to URL?

后端 未结 2 1516
温柔的废话
温柔的废话 2021-01-18 07:41

While using SOLRJ I would like to know how can I convert SolrQuery object to its URL representation with SOLR query syntax. I tried to use .toString() method but it doesnt

2条回答
  •  醉话见心
    2021-01-18 08:18

    I recommend ClientUtils.toQueryString for this matter.

    @Test
    public void solrQueryToURL() {
      SolrQuery tmpQuery = new SolrQuery("some query");
      Assert.assertEquals("?q=some+query", ClientUtils.toQueryString(tmpQuery, false));
    }
    

    Within the source code of HttpSolrServer you can see that this is used by the Solrj code itself for this reason.

    public NamedList request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException {
    
      // ... other code left out
    
      if( SolrRequest.METHOD.GET == request.getMethod() ) {
        if( streams != null ) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!" );
        }
        method = new HttpGet( baseUrl + path + ClientUtils.toQueryString( params, false ) );
    
      // ... other code left out
    
      }
    
        

    提交回复
    热议问题