在这里,我们介绍了一些使用curl的最常见的ElasticSearch命令。ElasticSearch有时很复杂。因此,在这里我们将其简化。
删除索引
删除名为samples的索引。
curl -X DELETE 'http://localhost:9200/samples'
列出所有索引
curl -X GET 'http://localhost:9200/_cat/indices?v'
列出索引中的所有文档
curl -X GET 'http://localhost:9200/sample/_search'
使用URL参数查询
在这里,我们使用Lucene查询格式来写q = school:Harvard。
curl -X GET http://localhost:9200/samples/_search?q=school:Harvard
使用JSON查询(又名Elasticsearch查询DSL)
您可以使用URL上的参数进行查询。但是您也可以使用JSON,如下面的示例所示。当您进行复杂的查询时,与一个庞大的URL参数字符串相比,JSON更易于阅读和调试。
复制
curl -XGET --header 'Content-Type: application/json' http://localhost:9200/samples/_search -d '{
"query" : {
"match" : { "school": "Harvard" }
}
}'
列出索引映射
所有Elasticsearch字段都是索引。因此,这会在索引中列出所有字段及其类型。
curl -X GET http://localhost:9200/samples
新增数据
curl -XPUT --header 'Content-Type: application/json' http://localhost:9200/samples/_doc/1 -d '{
"school" : "Harvard"
}'
更新文件
这是向现有文档添加字段的方法。首先,我们创建一个新的。然后我们更新它。
curl -XPUT --header 'Content-Type: application/json' http://localhost:9200/samples/_doc/2 -d '
{
"school": "Clemson"
}'
curl -XPOST --header 'Content-Type: application/json' http://localhost:9200/samples/_doc/2/_update -d '{
"doc" : {
"students": 50000}
}'
备份索引
curl -XPOST --header 'Content-Type: application/json' http://localhost:9200/_reindex -d '{
"source": {
"index": "samples"
},
"dest": {
"index": "samples_backup"
}
}'
以JSON格式批量加载数据
export pwd="elastic:"
curl --user $pwd -H 'Content-Type: application/x-ndjson' -XPOST 'https://58571402f5464923883e7be42a037917.eu-central-1.aws.cloud.es.io:9243/0/_bulk?pretty' --data-binary @<file>
显示集群健康
curl --user $pwd -H 'Content-Type: application/json' -XGET https://58571402f5464923883e7be42a037917.eu-central-1.aws.cloud.es.io:9243/_cluster/health?pretty
聚合和存储桶聚合
对于Nginx Web服务器,这将按用户城市生成Web命中计数:
curl -XGET --user $pwd --header 'Content-Type: application/json' https://58571402f5464923883e7be42a037917.eu-central-1.aws.cloud.es.io:9243/logstash/_search?pretty -d '{
"aggs": {
"cityName": {
"terms": {
"field": "geoip.city_name.keyword",
"size": 50
}
}
}
}
'
这会将其扩展到Nginx Web服务器日志中按城市划分的产品响应代码计数
curl -XGET --user $pwd --header 'Content-Type: application/json' https://58571402f5464923883e7be42a037917.eu-central-1.aws.cloud.es.io:9243/logstash/_search?pretty -d '{
"aggs": {
"city": {
"terms": {
"field": "geoip.city_name.keyword"
},
"aggs": {
"responses": {
"terms": {
"field": "response"
}
}
}
},
"responses": {
"terms": {
"field": "response"
}
}
}
}'
结合使用ElasticSearch和基本身份验证
如果您已通过ElasticSearch启用了安全性,则需要向每个curl命令提供如下所示的用户名和密码:
curl -X GET 'http://localhost:9200/_cat/indices?v' -u elastic:(password)
漂亮的输出
将?pretty = true添加到任何搜索中以漂亮地打印JSON。像这样:
curl -X GET 'http://localhost:9200/(index)/_search'?pretty=true
仅查询和返回某些字段
要仅返回某些字段,请将它们放入_source数组中:
GET filebeat-7.6.2-2020.05.05-000001/_search
{
"_source": ["suricata.eve.timestamp","source.geo.region_name","event.created"],
"query": {
"match" : { "source.geo.country_iso_code": "GR" }
}
}
按日期查询
当字段的类型为date时,您可以使用日期数学,如下所示:
GET filebeat-7.6.2-2020.05.05-000001/_search
{
"query": {
"range" : {
"event.created": {
"gte" : "now-7d/d"
}
}
}
}