Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different result when using GET/POST in elastic search

Tags:

I am experimenting with elastic search via the Elastic Search Head plugin.

The results are as expected when I submit a query via POST.

However when I try the same query using GET, I always get back all values in the index.

So : how to pass the query to the elastic search server via GET so I can use the search string in an URL?

like image 239
Peter Avatar asked Aug 30 '12 10:08

Peter


1 Answers

If you send a GET the body is probably not even sent to elasticsearch, so you are basically sending no query to the _search endpoint, which is why you are getting everything back (of course only the first 10 results based on the default size parameter).

Have a look at the URI request, which allows you to send basic queries using the q parameter within the URI. You can use the Lucene query syntax and specify some other parameters listed in the linked page. If you then want to execute more advanced queries, you might want to express them as JSON queries in order to get all the benefits of the elasticsearch Query DSL, but you'd need to provide them as body of the request.

UPDATE
Looking deeper at the elasticsearch head plugin, the query is not sent as the request body when you select the GET method but within the URL itself and without specifying the name for the parameter, like this:

http://localhost:9200/_search&{"query":{"term":{"text":"john"}}} 

That is probably a bug in the plugin itself and elasticsearch can't find the query, that's why you get all the results back. That means that only the POST method works while sending queries with elasticsearch head.

Elasticsearch allows to use both GET and POST for executing queries. If you use GET you can either send the query as body or use the source parameter like this:

http://localhost:9200/_search?source={"query":{"term":{"text":"john"}}} 
like image 139
javanna Avatar answered Sep 28 '22 04:09

javanna