[英]AND query in elasticsearch with curl
让我们在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" }'
我正在尝试使用Published_from = 2016-08-03和author = John查询文档。 我尝试使用以下curl命令来做到这一点:
curl -g "localhost:9200/customer/external/_search?pretty&filter_path=hits.hits._source.author&q=+author:John+published_from:2016-08-03"
但是,输出显示Jeanne
{
"hits" : {
"hits" : [
{
"_source" : {
"author" : "John"
}
},
{
"_source" : {
"author" : "Jeanne"
}
}
]
}
}
当我尝试这个curl命令时:
curl "localhost:9200/customer/external/_search?pretty&filter_path=hits.hits._source.author&q=%2Bauthor%3AJohn+%2Bpublished_from%3A2016-08-03"
输出正是我想要的。
{
"hits" : {
"hits" : [
{
"_source" : {
"author" : "John"
}
}
]
}
}
为什么第一个命令无法按预期运行?
第一个URL中的+
号 :
...&q=+author:John+published_from:2016-08-03
根据百分比编码规则(在服务器端)被解释为space 。 通常将空格编码为%20
,但是由于历史原因, +
也是空格字符的有效编码。
这意味着ElasticSearch获取的查询字符串如下所示:
author:John published_from:2016-08-03
根据查询字符串语法 ,它将查找包含一个或多个author:John
或published_from:2016-08-03
任何文档。
当您正确编码Elastic的+
运算符(在第二个URL中)时,收到的查询为:
+author:John +published_from:2016-08-03
注意+
被解码为空格 ,
%2B
为+
。
curl
简单的查询参数编码 为避免对查询进行手动(百分比)编码,可以使用--data-urlencode
选项,并提供原始的key=value
对。
例如:
curl -G 'localhost:9200/customer/external/_search?pretty&filter_path=hits.hits._source.author' --data-urlencode 'q=+author:John +published_from:2016-08-03'
在这里curl
会将URL中的查询参数与-d
/ --data-urlencode
选项提供的参数结合在一起。 我们需要-G
来强制执行GET
请求,因为-d
/ --data-urlencode
默认为POST
请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.