简体   繁体   中英

Using POST HTTP method to read data from Solr using SolrJ

We are using the SolrJ client to query documents from Solr. The query that we are building is large and Solr is rejecting the query (with error response 414 URI Too Long ).

However, I can directly call Solr's /query endpoint and post the query as the body using a POST request. Is there a way to do this using the SolrJ client?

You can use post method in SolrJ using QueryRequest which has options to set HTTP methods.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM