Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

url - Different result when using GET/POST in elastic search

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"}}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...