Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch POST with json search body vs GET with json in url

According to the ES documentation, those 2 search request should get the same results:

GET

http://localhost:9200/app/users/_search?source={"query": {"term": {"email":"[email protected]"}}}

POST

http://localhost:9200/app/users/_search

Post body :

{
    "query":  {
         "term": {
               "email":"[email protected]"
          }
    }
}

But the first one gives no result while the second one gives me the expected result. I use ES version 0.19.10 Did anybody else have the same behavior ? Is this a bug ?

like image 314
Go4It Avatar asked Jan 15 '13 14:01

Go4It


Video Answer


2 Answers

source is not a valid query string argument according to URI Search

Elasticsearch allows three ways to perform a search request...

GET with request body:

curl -XGET "http://localhost:9200/app/users/_search" -d '{
  "query": {
    "term": {
      "email": "[email protected]"
    }
  }
}'

POST with request body:

Since not all clients support GET with body, POST is allowed as well.

curl -XPOST "http://localhost:9200/app/users/_search" -d '{
  "query": {
    "term": {
      "email": "[email protected]"
    }
  }
}'

GET without request body:

curl -XGET "http://localhost:9200/app/users/_search?q=email:[email protected]"

or (if you want to manually URL encode your query string)

curl -XGET "http://localhost:9200/app/users/_search?q=email%3Afoo%40gmail.com"
like image 162
Andrew Macheret Avatar answered Sep 22 '22 15:09

Andrew Macheret


You should URL encode your query in the first case:

http://localhost:9200/app/users/_search?source=%7b%22query%22%3a+%7b%22term%22%3a+%7b%22email%22%3a%22foo%40gmail.com%22%7d%7d%7d
like image 36
imotov Avatar answered Sep 18 '22 15:09

imotov