用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命令做到这一点:

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请求.

相关文章

文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限...
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Wi...
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Lin...
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,...
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 Open...