如何将 SolrQuery(SOLRJ) 转换为 URL? [英] How can I transform SolrQuery(SOLRJ) to URL?

查看:23
本文介绍了如何将 SolrQuery(SOLRJ) 转换为 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 SOLRJ 时,我想知道如何使用 SOLR 查询语法将 SolrQuery 对象转换为其 URL 表示.我尝试使用 .toString() 方法,但它没有返回正确的查询表示.还有其他方法吗?

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 return proper query representation. Is there some other way how to do it?

推荐答案

我推荐 ClientUtils.toQueryString 就此事而言.

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));
}

HttpSolrServer 你可以看到这是 Solrj 代码本身出于这个原因使用的.

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

  }

这篇关于如何将 SolrQuery(SOLRJ) 转换为 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆