[英]AND query in elasticsearch with curl
Let's inject some data in Elasticsearch 让我们在Elasticsearch中注入一些数据
curl -XPUT 'localhost:9200/customer/external/1' -d '{ "author": "John", "published_from":"2016-08-03" }'
curl -XPUT 'localhost:9200/customer/external/2' -d '{ "author": "Jeanne", "published_from":"2016-08-03" }'
curl -XPUT 'localhost:9200/customer/external/3' -d '{ "author": "Jean", "published_from":"2016-08-05" }'
I am trying to query document with published_from=2016-08-03 and author=John. 我正在尝试使用Published_from = 2016-08-03和author = John查询文档。 I try to do it with this curl command:
我尝试使用以下curl命令来做到这一点:
curl -g "localhost:9200/customer/external/_search?pretty&filter_path=hits.hits._source.author&q=+author:John+published_from:2016-08-03"
Yet, the output displays Jeanne 但是,输出显示Jeanne
{
"hits" : {
"hits" : [
{
"_source" : {
"author" : "John"
}
},
{
"_source" : {
"author" : "Jeanne"
}
}
]
}
}
When I try this curl command : 当我尝试这个curl命令时:
curl "localhost:9200/customer/external/_search?pretty&filter_path=hits.hits._source.author&q=%2Bauthor%3AJohn+%2Bpublished_from%3A2016-08-03"
The output is exactly what I want. 输出正是我想要的。
{
"hits" : {
"hits" : [
{
"_source" : {
"author" : "John"
}
}
]
}
}
Why is the first command not working as expected ? 为什么第一个命令无法按预期运行?
The +
signs in the first URL: 第一个URL中的
+
号 :
...&q=+author:John+published_from:2016-08-03
are interpreted (on server-side), in accordance to the percent-encoding rules as spaces . 根据百分比编码规则(在服务器端)被解释为space 。 The space is usually encoded as
%20
, but for historical reasons, +
is also a valid encoding of the space character. 通常将空格编码为
%20
,但是由于历史原因, +
也是空格字符的有效编码。
That means the query string that ElasticSearch gets looks like: 这意味着ElasticSearch获取的查询字符串如下所示:
author:John published_from:2016-08-03
According to query string syntax , it will find any document that contains one or more of author:John
or published_from:2016-08-03
. 根据查询字符串语法 ,它将查找包含一个或多个
author:John
或published_from:2016-08-03
任何文档。
When you properly encode the Elastic's +
operator (in the second URL), the query received is: 当您正确编码Elastic的
+
运算符(在第二个URL中)时,收到的查询为:
+author:John +published_from:2016-08-03
Note the +
was decoded as space 注意
+
被解码为空格 , and
%2B
as +
. ,
%2B
为+
。
curl
curl
简单的查询参数编码 To avoid manual (percent-)encoding of queries, you can use the --data-urlencode
option, and provide raw key=value
pairs. 为避免对查询进行手动(百分比)编码,可以使用
--data-urlencode
选项,并提供原始的key=value
对。
For example: 例如:
curl -G 'localhost:9200/customer/external/_search?pretty&filter_path=hits.hits._source.author' --data-urlencode 'q=+author:John +published_from:2016-08-03'
Here curl
will combine the query parameters from the URL with those provided with the -d
/ --data-urlencode
options. 在这里
curl
会将URL中的查询参数与-d
/ --data-urlencode
选项提供的参数结合在一起。 We need -G
to force a GET
request, since -d
/ --data-urlencode
defaults to a POST
request. 我们需要
-G
来强制执行GET
请求,因为-d
/ --data-urlencode
默认为POST
请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.