在弹性搜索中使用GET / POST时的不同结果 [英] Different result when using GET/POST in elastic search

查看:122
本文介绍了在弹性搜索中使用GET / POST时的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过弹性搜索头插件进行弹性搜索。



当我通过POST提交查询时,结果符合预期。



但是,当我使用GET尝试相同的查询时,我总是收回索引中的所有值。



所以:如何通过GET将查询传递给弹性搜索服务器,以便我可以在URL中使用搜索字符串?

解决方案

如果你发送一个GET,身体可能甚至没有发送到弹性搜索,所以你基本上不向 _search 端点发送任何查询,这就是为什么你把所有的东西都当然只有前10个结果基于默认大小参数)。



看看 URI请求,它允许您使用 q 参数发送基本查询在URI内。您可以使用 Lucene查询语法,并指定链接页面中列出的其他一些参数。如果您要执行更高级的查询,您可能希望将其表达为JSON查询,以获得弹性搜索的所有好处查询DSL ,但您需要将其作为请求的正文。



更新

更深入地了解elasticsearch头插件,当您选择GET方法但在URL本身中时,查询不会作为请求正文发送,并且不指定参数的名称,如下所示:

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

这可能是插件本身的一个错误,弹性搜索找不到查询,这就是为什么你得到所有的结果。这意味着只有POST方法在使用elasticsearch头发送查询时才起作用。



Elasticsearch允许使用GET和POST来执行查询。如果您使用GET,您可以发送查询作为正文或使用源参数,如下所示:

  http:// localhost: 9200 / _search?source = {query:{term:{text:john}}} 


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?

解决方案

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

这篇关于在弹性搜索中使用GET / POST时的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆