Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use POST to run a query in Solr (/select)

I have queries that I am running against out solr index that sometimes have very long query parameters, I get errors when i run these queries, which i assume are do to the limit of a GET query parameters.

Here is the method I use to query (JSON), this is to show I am using the Http Extensions (the client i use is a thin wrapper for HttpClient) not an end to end solution. 90% of the queries run fine, it is just when the params are large i get the 500 error from solr. I have read somewhere you can use POSt's when doing the select command but have not found examples of how to do it. Any help would be fantastic!

    public string GetJson(HttpQueryString qs)
    {
        using (var client = new DAC.US.Web.XmlHttpServiceClient(this.Uri))
        {
            client.Client.DefaultHeaders.Authorization = new Microsoft.Http.Headers.Credential("Basic", DAC.US.Encryption.Hash.WebServiceCredintials);
            qs.Add("wt", "json");

            if (!String.IsNullOrEmpty(this.Version))
                qs.Add("version", this.Version);

            using (var response = client.Get(new Uri(@"select/", UriKind.Relative), qs))
            {
                return response.Content.ReadAsString();
            }
        }
    }
like image 745
RyanFetz Avatar asked Jun 08 '10 11:06

RyanFetz


3 Answers

  1. Don't assume. Check the Solr log to confirm the reason of this error.
  2. /select accepts POST requests without issues. You can try this with curl:

    curl -d "q=*:*&rows=1" http://localhost:8983/solr/select
    

    I can't comment on XmlHttpServiceClient as it seems to be some proprietary code, but see this page for an example of POSTing using HttpWebRequest.

BTW: there are .net libraries that implement communicating with Solr, no need to roll your own unless you have some very weird requirements.

like image 158
Mauricio Scheffer Avatar answered Nov 16 '22 18:11

Mauricio Scheffer


Be sure to set Content type: application/x-www-form-urlencoded or you will get a status code of 500.

Curl does this by default.

It would not surprise me if your XmlHttpServiceClient was hard-coded/defaulting to use text/xml as the Content type. HttpWebRequest is more appropriate.

like image 32
Jacob Wu Avatar answered Nov 16 '22 20:11

Jacob Wu


Solr Supports HTTP GET and HTTP POST.

While doing HTTP POST set the content type correctly. You can verify it using Postman or Fiddler.

Correct content type: Content-Type:application/x-www-form-urlencoded.

Without the correct content type you will get the error message as :The remote server returned an error: (400) Bad Request

like image 3
Bishnubrata Panigrahi Avatar answered Nov 16 '22 18:11

Bishnubrata Panigrahi