User-Profile-Image
hankin
  • 5
  • centos7
  • docker
  • mysql
  • PostgreSQL
  • git/gitlab
  • ELK
  • python
    • python-Tornado
    • python-django
  • redis
  • nginx
  • kvm
  • proxmox
  • mongo
  • kubernetes
  • prometheus
  • GlusterFs
  • nfs
  • freeswitch
  • httpd
  • shell脚本
  • linux
  • fastdfs
  • nextcloud
  • openssl
  • openvpn
  • rabbitmq
  • sqlite
  • svn
  • java
  • ubuntu
  • vue2
  • wordpress
  • php
  • IOT物联网
  • 项目
  • 故障处理
  • 树莓派
  • 博客存档
  • 未分类
  • 杂项
  • #1742(无标题)
  • 新视野
  • 分类
    • 项目
    • 树莓派
    • 杂项
    • 未分类
    • 新视野
    • 故障处理
    • 博客存档
    • 交换机
    • wordpress
    • vue2
    • ubuntu
    • svn
    • sqlite
    • shell脚本
    • redis
    • rabbitmq
    • python-django
    • python
    • proxmox
    • prometheus
    • PostgreSQL
    • php
    • openvpn
    • openssl
    • nginx
    • nfs
    • nextcloud
    • mysql
    • mongo
    • linux
    • kvm
    • kubernetes
    • java
    • IOT物联网
    • httpd
    • GlusterFs
    • git/gitlab
    • freeswitch
    • fastdfs
    • ELK
    • docker
    • centos7
  • 页面
    • #1742(无标题)
  • 友链
      请到[后台->主题设置->友情链接]中设置。
Help?

Please contact us on our email for need any support

Support
    首页   ›   ELK   ›   正文
ELK

elasticsearch使用方法-基础

2023-05-14 09:54:00
976  0 0

先尝试在kibana手动导入数据,熟悉界面点击查看操作,然后在开发工具中测试使用

使用风格:
es支持两种风格
风格一: REST request URI 风格
    curl -XGET 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty'REST 
风格二: request body 风格
    curl -XGET 'localhost:9200/bank/_search?pretty' -d'
    {
      "query": { "match_all": {} },
      "sort": [
        { "account_number": "asc" }
      ]
    }'
健康状态
http://192.168.1.x:9200/_cat/health?v&pretty
  Green 表示一切正常(集群功能齐全), 
  yellow 表示所有数据可用,但是有些副本尚未分配(集群功能齐全),
  red 意味着由于某些原因有些数据不可用。注意,它仍然具有部分功能(例如,它将继续从可用的分片中服务搜索请求)
查询响应数据
took - Elasticsearch 执行搜索的时间(毫秒)
time_out - 告诉我们搜索是否超时
_shards - 告诉我们多少个分片被搜索了,以及统计了成功/失败的搜索分片
hits - 搜索结果
hits.total - 搜索结果
hits.hits - 实际的搜索结果数组(默认为前 10 的文档),不指定 size,默认为 10
sort - 结果的排序 key(键)(没有则按 score 排序),分数越高,文档的相关度更高
score 和max_score -现在暂时忽略这些字段
获取节点列表
curl -XGET 'localhost:9200/_cat/nodes?v&pretty'

索引

查看所有索引
curl -XGET 'localhost:9200/_cat/indices?v&pretty'
查看获取指定索引数据
GET /test1/_search

GET /test3/_search
{
  "query": {
    "match_all": {}
  }
}
查看索引结构?
GET /ip
创建索引
PUT /test1

或curl -XPUT 'localhost:9200/tangnginx?pretty&pretty'

或PUT /test2
{
  "mappings":{
    "properties": {
       "name":{
      "type":"text"
      },
       "age":{
      "type":"long"
      },
      "sex":{
      "type":"text"
      }
    }
  }
}
创建索引-设置分片、副本数
put test2
{
  "settings":{
    "number_of_shards":2,
    "number_of_replicas":3
  }
}
创建索引常用参数配置
pass
修改索引
PUT test1/_settings
{
  "index":{
    "number_of_replicas":5
  }
}
删除索引
DELETE test1
或
curl -XDELETE 'localhost:9200/customer?pretty&pretty'

数据操作-基本增删改查(CRUD )

基本

新增
#不指定会自动创建_id值
post /test1/_doc/
{
  "name":"n213"
}

#不存在则会新增,已存在则会报错,保障_id唯一
post /test1/_create/2
{
  "name":"n23"
}
批量新增
post _bulk
{"create":{"_index":"test1","_type":"_doc","_id":3}}
{"name":"n2131"}
{"create":{"_index":"test1","_type":"_doc","_id":4}}
{"age":"n2111"}
全量修改
#会删除原文档,新建。比如原有age字段,更改后就没有了
PUT /test1/_doc/2
{
  "name":"n21"
}

#会删除原文档,新建。比如原有age字段,更改后就没有了
post /test1/_doc/2
{
  "name":"n21"
}
数据更新,不会清空原文档,只会新增或修改字段
post /test1/_update/1
{
  "doc":{
    "name1":"n212"
  }
}
批量更新
#也支持同时更改不同索引
post _bulk
{"update":{"_index":"test1","_type":"_doc","_id":3}}
{"doc":{"name":"n1"}}
{"update":{"_index":"test1","_type":"_doc","_id":4}}
{"doc":{"name":"n1"}}
删除数据
#批量删除
post _bulk
{"delete":{"_index":"test1","_type":"_doc","_id":3}}
{"delete":{"_index":"test1","_type":"_doc","_id":4}}
组合
#可同时将create update等组合在一起执行
post _bulk
{"delete":{"_index":"test1","_type":"_doc","_id":3}}
{"update":{"_index":"test1","_type":"_doc","_id":4}}
{"doc":{"name":"n1"}}
筛选符合条件的更新
#将"name"为 "n213"的所有doc的name > 'mmm'
POST /test1/_update_by_query
{
  "query": {
    "match": {
      "name": "n213"
    }
  },
  "script": {
    "source": "ctx._source.name = 'mmm'"
  }
}
批量读取
#_mget需要知道id,

#读取多个index数据
GET _mget
{
  "docs":[{"_index":"test1","_id":3},{"_index":"ip","_id" : "retn9ocBF6xrz6VsU1U5"}]
}

#读取单个index的多个数据
GET /ip/_mget
{
  "docs":[{"_id" : "retn9ocBF6xrz6VsU1U5"},{"_id" : "rutn9ocBF6xrz6VsU1U5"}]
}
GET /ip/_mget
{
  "ids":["retn9ocBF6xrz6VsU1U5","rutn9ocBF6xrz6VsU1U5"]
}

#msearch
GET /_msearch 或 get /ip/_msearch
  ...

#...
GET /test1/_search
GET /ip/_search

高级

查询-macha_ll
#使用macha_ll查询查询所有,默认返回前10记录(size默认=10) 

GET /ip/_search

GET /ip/_search
{
  "query": {
  "match_all": {}
}}
查询-指定返回条数size
#默认size<10000,可改大窗口配置参数,但不推荐修改配置
PUT /ip/_settings
{
  "index.max_result_window":"100000"
}

#指定0-100
GET /ip/_search
{
  "query": {
  "match_all": {}
  },
  "size":100,
  "from": 0
}
查询-scroll
#避免内存消耗,大量数据分页使用多次scroll查询
#scroll采用游标查询(_scoll_id),若果数据更新了,可能新数据查不到,主要用于减少消耗性能
#scroll=1m 1分钟
GET /ip/_search?scroll=1m
{
  "query": {"match_all": {}},
  "size": 20
}

#第二次拉取,使用上次返回的_scoll_id,(拉取同上20条数据)
GET /_search/scroll
{
  "scroll":"1m",
  "scroll_id":"FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjFVZDNDWW5vUmV5VW1jNWpkOTcwRWcAAAAAAAAz4BZELWNic01ueVJpR0JCNGJjM3p1VWRR"
}
查询-排序
#降序desc 升序...
#(指定排序后"_score"值会为 null)
GET /ip/_search
{
  "query": {"match_all": {}},
  "sort": [
    {
      "column2": {
        "order": "desc"
      }
    }
  ],
  "from":10,
  "size":2
}
查询-条件查询 match
#match在匹配时,对所有查找的关键词进行分词,然后按匹配查找
#支持参数 
#query 指定匹配的值
#operator 匹配条件类型
#and 条件分词后都要匹配
#or 条件分词后有一个就行
#minmum_should_match 最低匹配度

get /myik/_search

GET /myik/_search
{
  "query": {
    "match": {
      "arch": {
      "query": "草履毒",
      "operator": "or"
      }
      }
  }
}
#有问题,只返回一条? 是不是分词器的问题,待测试默认分词器
查询-短语查询match_phrase
#要求分词必须在被检索字段都包含,并且顺序必须相同,
#slop:2参数允许不连续可隔2个
GET /myik/_search
{
  "query": {
    "match_phrase": {
      "arch": "草履虫"
    }
  }
}
查询-multi_match
#可根据字段类型,决定是否使用分词查询,得分高的在前面
GET /myik/_search
{
  "query": {
    "multi_match": {
      "query": "草履毒n2",
      "fields": [
        "arch",
        "name"
      ]
    }
  }
}
查询-query_string
#允许在单个查询字符串中指定 and or not ,也支持多字段搜索

#不指定字段查询
GET /myik/_search
{
  "query": {
    "query_string": {
      "query": "细菌 or 毒"
    }
  }
}

#指定单字段查询
GET /myik/_search
{
  "query": {
    "query_string": {
      "default_field": "arch",
      "query": "细菌 or 毒"
    }
  }
}

#指定多字段查询
GET /myik/_search
{
  "query": {
    "query_string": {
      "fields": ["name","arch"], 
      "query": "细菌 or n"
    }
  }
}
查询-simpli_query_string
pass
查询-关键词查询term
#中文拆成单个字
#?
#小写!!!
#filter 不计算得分

GET /myik/_search
get /myik/_mapping
GET /myik/_search
{
  "query": {
    "term": {
      "arch": {
        "value": "草"
      }
    }
    }
  }
}

GET /myik/_search
{
  "query": {
    "term": {
      "arch.keyword": {
        "value": "草履虫都能听懂"
      }
    }
    }
}

}

查询-wildcard通配符查询

只能用于keywords类型字段

分词后要注意,有没有该词条

GET /ip
GET /ip/_search

GET /ip/_search
{
"query": {
"wildcard": {
"column2": {
"value": "23:04"
}
}
}
}

查询-范围查询
# range 范围关键字
# gte >=
# lte <=
# gt  >
# lt  <
# now 当前时间

POST /ip/_search
{
  "query":{
    "range": {
      "column4": {
        "gte": 400,
        "lte": 500
      }
    }
  }
}

#组合一下,搜索400-500的状态,只显示ip和状态,并按照状态降序排序
POST /ip/_search
{
  "query":{
    "range": {
      "column4": {
        "gte": 400,
        "lte": 500
      }
    }
  },
  "_source": ["column1","column4"] ,
  "sort": [
    {
      "column4": "desc"
    }
  ]
}

#now用法
#查询一年内的数据
POST /ip/_search
{
  "query":{
    "range": {
      "column4": {
        "gte": now-1y
      }
    }
  }
}
多id查询
#ids关键字:值为数组类型,用来根据一组id获取多个对应文档
GET /ip/_search
{
  "query": {
    "ids": {
      "values": [1,2]
    }
  }
}
查询-fuzzy模糊查询
#重要参数 fuzziness prefix_length
fuzziness表示输入的关键字通过几次操作可以转变为es库里对应的field字段(新增、删除或更改一个字符都算一次),默认为0不开启
prefix_length
评论 (0)

点击这里取消回复。

欢迎您 游客  

Copyright © 2025 网站备案号 : 蜀ICP备2022017747号
smarty_hankin 主题. Designed by hankin
主页
页面
  • #1742(无标题)
博主
tang.show
tang.show 管理员
linux、centos、docker 、k8s、mysql等技术相关的总结文档
213 文章 2 评论 201906 浏览
测试
测试