22

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?

javanna
  • 59,145
  • 14
  • 144
  • 125
Peter
  • 47,963
  • 46
  • 132
  • 181

1 Answers1

36

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"}}}
javanna
  • 59,145
  • 14
  • 144
  • 125
  • The httpheader states that I'm indeed sending the query, but not in Lucene query syntax. Just strange you can form a query and are presented with the GET option, when it's not working.... – Peter Aug 30 '12 at 18:28
  • where do you find in the docs the "source" key? I don't find it and it doesn't work, downvoting – knocte Feb 13 '13 at 08:36
  • 1
    @knocte that's tricky. You go to the [Search API](http://www.elasticsearch.org/guide/reference/api/search/), then you click on the [request body](http://www.elasticsearch.org/guide/reference/api/search/request-body.html) link on top and you look at the bottom of the page. ;) – javanna Feb 13 '13 at 18:35
  • You are a lifesaver. – HelloWorld Sep 09 '20 at 02:59
  • get also need param source_content_type=application/json – Pegasus Dec 27 '21 at 05:20