[英]Using POST HTTP method to read data from Solr using SolrJ
We are using the SolrJ client to query documents from Solr. 我们使用SolrJ客户端从Solr查询文档。 The query that we are building is large and Solr is rejecting the query (with error response
414 URI Too Long
). 我们正在构建的查询很大,Solr拒绝查询(错误响应
414 URI Too Long
)。
However, I can directly call Solr's /query
endpoint and post the query as the body using a POST request. 但是,我可以直接调用Solr的
/query
端点,并使用POST请求将查询作为正文发布。 Is there a way to do this using the SolrJ client? 有没有办法使用SolrJ客户端?
You can use post method in SolrJ using QueryRequest which has options to set HTTP methods. 您可以使用QueryRequest在SolrJ中使用post方法,该方法具有设置HTTP方法的选项。
public QueryResponse query(SolrQuery parameters){
QueryResponse resp=null;
try {
QueryRequest queryRequest=new QueryRequest(parameters);
queryRequest.setMethod(SolrRequest.METHOD.POST);
NamedList nl=solrClient.request(queryRequest);
resp=new QueryResponse(nl, solrClient);
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
If you perform queries by instantiating a SolrQuery
and asking the SolrClient
to execute it, you can simply pass the HTTP method to use as an additional parameter . 如果通过实例化
SolrQuery
并要求SolrClient
执行它来执行查询,则可以简单地传递HTTP方法以用作附加 参数 。 Here is a quick example: 这是一个简单的例子:
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest.METHOD;
import org.apache.solr.client.solrj.response.QueryResponse;
/* ... */
HttpSolrClient client = new HttpSolrClient.
Builder("http://localhost:8983/solr").
withConnectionTimeout(1000).
withSocketTimeout(60 * 1000).
build();
SolrQuery query = new SolrQuery("*:*");
QueryResponse response = client.query(query, METHOD.POST);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.