How can I transform SolrQuery(SOLRJ) to URL?

≯℡__Kan透↙ 提交于 2019-12-01 17:52:19

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<Object> 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

  }

SolrJ (tested version 6.6.0 ) it is:

@Test
public void solrQueryToURL() {
  SolrQuery query = new SolrQuery("query");
  Assert.assertEquals("?q=query", query.toQueryString());
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!