0

I am passing here some parameters via get to limit query result and also query_string is passed in url. Although, I am also giving request body to filter results.

curl -XGET 'http://localhost:9200/books/fantasy/_search?from=0&size=10&q=%2A' -d '{
    "query":{
        "filtered":{
            "filter":{
                "exists":{
                    "field":"speacial.ean"
                }
            }
        }
    }
}'

I just want to check is this approach okay? is there any downsides doing it like this? Or should I pass any parameters in url when body is used?

This seems to work, but is it bad practice?

lehtu
  • 868
  • 1
  • 12
  • 27
  • The problem is that a GET request does not use the body (i.e it has no body). Perhaps curl transforms -d data to url parameters when you use it with the GET method. Can you check this in the server logs? – Peter Paul Kiefer Nov 04 '15 at 14:04
  • Are you sure? "Both HTTP GET and HTTP POST can be used to execute search with body. Since not all clients support GET with body, POST is allowed as well." - https://www.elastic.co/guide/en/elasticsearch/reference/1.3/search-request-body.html – lehtu Nov 04 '15 at 14:06
  • Also worth noting that tools like Sense will give a warning `Browsers do not support GET requests with a body. This will be executed as a POST` and silently transform a GET into a POST when a payload is sent. ES endpoints accept both, but the correct HTTP way of doing it is to never send a payload with a GET... I've seen weird bugs when doing so and debugging them is not always straight-forward. – Val Nov 04 '15 at 14:07
  • Yes I'm sure. http://www.w3schools.com/tags/ref_httpmethods.asp. (See: compare GET vs POST) I've had this problem many times before. Each time I want to query with large filter sets. Then I had to use a POST request, because the url should be less the 2048 character long. But some clients "e.g. curl" append the "body"-data to the url. This works as long as the url will not be longer than 2048 characters. – Peter Paul Kiefer Nov 04 '15 at 14:13
  • In fact, I just noticed that this doesn't even work. When you have `q` parameter in URL it doesn't care about the body at all – lehtu Nov 06 '15 at 10:01

1 Answers1

0

GET requests are not supposed to use a body ( more information on this here). While curl might convert your GET requests with a body to POST, many tools might simply drop the body, or it might be sent to Elastic but ignored because you used GET.

When executing this query in my SENSE, I get all the documents instead of just the document matching my query, proving that the body has been ignored:

GET myIndex/_search
{
    "query": {
        "match": {
            "zlob": true
        }
    }
}

This example shows that you should avoid to use GET to make requests with a body, because the result will depend on the tool you use for your rest queries.

Community
  • 1
  • 1
Heschoon
  • 2,915
  • 9
  • 26
  • 55