如何从命令行获取 Elasticsearch 服务器版本

How to get JUST the Elasticsearch server version from the command line

有没有办法只获取 Elasticsearch 服务器的版本号。我知道你得到了 JSON 请求数据,但是有没有办法解析该请求只获取版本号。

卷曲localhost:9200

{
    ...
    "version": {
        ...
        "number": "2.1.1"
    }
}

如果您有 jq 实用程序,您可以使用它来解析 json 回复并输出纯文本字符串:

curl -sS localhost:9200 | jq -r .version.number

通用脚本语言可以完成相同的任务,但通常更笨重:

curl -sS localhost:9200 | python -c 'import json, sys; print(json.loads(sys.stdin.read())["version"]["number"])'

另一种不需要任何外部依赖的方法是使用 response filteringfilter_path 查询字符串参数(自 ES 1.6 起可用)和 awk 命令。

curl -s -XGET 'localhost:9200?filter_path=version.number&pretty=false' | awk -F'"' {'print '}

那个returns:

2.1.1