用curl在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" }' 我正在尝试使用published_from = 2016-08-03和author = John查询文档. 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中的简单查询参数编码 要避免手动(百分比)编码查询,可以使用 例如: 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请求. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |