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

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

问题描述

我正在通过 Elastic Search Head 插件试验弹性搜索.

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

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

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

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

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

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

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

推荐答案

如果你发送一个 GET,主体甚至可能不会发送到 elasticsearch,所以你基本上不会向 _search 端点发送查询,这就是为什么您要取回所有内容(当然只有基于默认大小参数的前 10 个结果).

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).

看看 URI 请求,它允许您使用 URI 中的 q 参数发送基本查询.您可以使用 Lucene 查询语法 并指定链接页面中列出的其他一些参数.如果您随后想要执行更高级的查询,您可能希望将它们表示为 JSON 查询,以获得 elasticsearch 查询 DSL,但您需要将它们作为请求的正文提供.

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.

更新
深入查看 elasticsearch head 插件,当您选择 GET 方法时,查询不会作为请求正文发送,而是在 URL 本身内发送,并且没有指定参数的名称,如下所示:

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

这可能是插件本身的一个错误,elasticsearch 无法找到查询,这就是您返回所有结果的原因.这意味着在使用 elasticsearch head 发送查询时,只有 POST 方法有效.

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 允许使用 GET 和 POST 来执行查询.如果您使用 GET,您可以将查询作为正文发送或使用如下所示的源参数:

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天全站免登陆