用于Elasticsearch

时间:2018-07-13 07:33:39

标签: elasticsearch curl urlencode url-encoding

我想让cURL / Elasticsearch理解以普通字符串形式传递的HTTP查询参数,同时通过命令对其进行url编码。

如果我通过cURL运行此HTTP GET,以将查询提交给Elasticsearch:

curl \
  -H 'Content-Type: application/json' \
  -XGET '127.0.0.1:9200/movies/movie/_search?q=%2Byear%3A%3E1980+%2Btitle%3Astar%20wars&pretty'

然后我就可以检索所需的文档。

但是,如果我运行此cURL查询:

curl \
   -H 'Content-Type: application/json' \
   --data-urlencode "pretty" \
   --data-urlencode "q=+year:>1980 +title:star wars&pretty" \
   -XGET '127.0.0.1:9200/movies/movie/_search'

然后我得到这个错误:

{
    "error": {
        "root_cause": [{
            "type": "json_parse_exception",
            "reason": "Unrecognized token 'pretty': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@7856627; line: 1, column: 8]"
        }],
        "type": "json_parse_exception",
        "reason": "Unrecognized token 'pretty': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@7856627; line: 1, column: 8]"
    },
    "status": 500
}

我正在使用:

  • cURL版本7.47.0,该版本应了解命令参数--data-urlencode
  • Elasticsearch 6.3.1

1 个答案:

答案 0 :(得分:1)

--data-urlencode将发送POST和URL编码正文。您必须使用-G or --get来发送GET请求并在URL中附加用--data-urlencode指定的数据:

curl -G -v \
     -H 'Content-Type: application/json' \
     --data-urlencode "pretty=true" \
     --data-urlencode "q=+year:>1980 +title:star wars" \
     '127.0.0.1:9200/movies/movie/_search'