让我们在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"
然而,输出显示珍妮
{ "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" } } ] } }
为什么第一个命令没有按预期工作?
解决方法
第一个网址中的标志:
...&q=+author:John+published_from:2016-08-03
被解释(在服务器端),根据percent-encoding规则作为空格.该空间通常被编码为,但由于历史原因,它也是空格字符的有效编码.
这意味着ElasticSearch获取的查询字符串如下所示:
author:John published_from:2016-08-03
根据query string syntax,它将找到包含一个或多个作者的任何文件:John或published_from:2016-08-03.
正确编码Elastic的运算符(在第二个URL中)时,收到的查询是:
+author:John +published_from:2016-08-03
注意,它被解码为空格,+为.
curl中的简单查询参数编码
要避免手动(百分比)编码查询,可以使用--data-urlencode
选项,并提供原始键=值对.
例如:
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请求.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。