elasticsearch索引总结

从了解ELK到搭建再到业务新建到现在也有6个月了,一直没有总结过es的查询语法,每次查询都要在网上找一波,上周接到一个任务,统计nginx日中所有域名的pv统计值,刚开始还准备用python调用elasticsearch模块去遍历某个索引下的所有日志,然后筛选相关内容进行整合(使用了scroll函数),结果倒是出来了,但是慢的不是一星半点,最后使用了es自己的api通过聚合查询很快就在几千万条日志里面把相关 的值统计了出来。

es查询整理:

一、_cat  API 使用

1. curl "ip:9200/_cat/health?v"

green表示一切正常

2.curl 'ip:9200/_cat/nodes?v'

可以查看所有的节点信息

3.curl 'ip:9200/_cat/indices?v'

可以列出所有的索引 以及每个索引的相关信息

扫描二维码关注公众号,回复: 3671626 查看本文章

二、索引的增删改查

语法格式: 

curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>

  <REST Verb>:REST风格的语法谓词

  <Node>:节点ip

  <port>:节点端口号,默认9200

  <Index>:索引名

  <Type>:索引类型

  <ID>:操作对象的ID号

1.新建索引:

curl -XPUT 'ip:9200/新建的索引名?pretty'

2.插入数据

curl -XPUT 'ip:9200/索引名/type类型/ID号?pretty' -d ‘

{

  “name” : "tom"

}'

3.获取数据:

curl -XGET 'ip:9200/索引名/ID号?pretty'

pretty使得返回数据美观

4.curl -XDELETE 'ip:9200/索引名?pretty'

删除索引

5。更新数据以及新增字段

curl -XPOXT 'ip:9200/索引名/类型名/id/_update?pretty' -d '

{

  "doc":{"name":"jack"}

}'

curl -XPOST 'ip:9200/索引名/类型名/id/_update?pretty' -d '

{

  "doc":{"name":"jack","age":"20"}

}'

三、批处理

下面语句将在一个批量操作中执行创建索引:

  curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
  {"index":{"_id":"1"}}
  {"name": "John Doe" }
  {"index":{"_id":"2"}}
  {"name": "Jane Doe" }
  '

  下面语句批处理执行更新id为1的数据然后执行删除id为2的数据

  curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
  {"update":{"_id":"1"}}
  {"doc": { "name": "John Doe becomes Jane Doe" } }
  {"delete":{"_id":"2"}}
  '
四、查询
curl -XPOST 'localhost:9200/索引名/_search?pretty' -d '
  {
    "query": { "match_all": {} }
   "size": 100 
 }'
返回索引下的匹配到的所有数据 默认返回十条
加上size指定返回条数
curl -XPOST 'localhost:9200/索引名/_search?pretty' -d '
  {
    "query": { "match_all": {} }
"_source" ["hostnanme","path"]
   "size": 100 
 }'
指定返回字段
curl -XPOST 'localhost:9200/索引名/_search?pretty' -d '
{
  "query" :{"match":{"hostname":"admin.com"}}
}’

猜你喜欢

转载自www.cnblogs.com/gzcheng/p/9828736.html