Technical note
Elasticsearch学习笔记
GUI
elasticvue
- chrome插件
- 查询 + 管理
elasticsearch-head
https://github.com/mobz/elasticsearch-head
- chrome插件
- 查询 + 管理
Kaizen
https://www.elastic-kaizen.com/
- java桌面版
- 纯查询
elasticsearch-hq
https://github.com/ElasticHQ/elasticsearch-HQ
docker run -p 5000:5000 elastichq/elasticsearch-hq open http://localhost:5000/
- docker run
- 监控 + 简单查询
djavu
https://github.com/appbaseio/dejavu/
docker run -p 1358:1358 -d appbaseio/dejavu
open http://localhost:1358/
参考文档
https://www.ruanyifeng.com/blog/2017/08/elasticsearch.html https://cloud.tencent.com/developer/article/1547867 https://juejin.cn/post/6844903919013855240 https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-dsl-intro.html https://n3xtchen.github.io/n3xtchen/elasticsearch/2017/07/05/elasticsearch-23-useful-query-example https://www.jianshu.com/p/eb30eee13923 https://zhuanlan.zhihu.com/p/34240906 https://feifeiyum.github.io/2020/02/27/es-mapping/ https://cloud.tencent.com/developer/article/1189279
Kibana教程
https://www.ruanyifeng.com/blog/2017/08/elasticsearch.html https://www.jianshu.com/p/7ca9e5b02ef6 https://www.elastic.co/guide/cn/kibana/current/setup.html
KibanaDevTool
查询
# 查看
GET /_cat
# 查看Index
GET /_cat/indices?v
# 查看健康
GET /_cat/health?v
# 查看全部mapping
GET /_mapping?pretty=true
# 创建索引
PUT /customer?pretty
# 创建文档
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}
# 根据ID查询文档
GET /customer/_doc/1?pretty
# 查询所有文档
GET /customer/_search?q=*&sort=name:asc&pretty
# 查看索引的mapping
GET /customer/_mapping?pretty=true
# 查看索引的settings
GET /customer/_settings?pretty=true
GET /bookdb_index/book/_search?q=guide
{
"query": {
"multi_match" : {
"query" : "guide",
"fields" : ["_all"]
}
}
}
查询2
# 查询
GET /crawl_log/_search/
{
"query": {
"match_all": {}
}
}
# 基本匹配
GET /bookdb_index/book/_search?q=guide
GET /bookdb_index/book/_search
{
"query": {
"multi_match" : {
"query" : "guide",
"fields" : ["_all"]
}
}
}
# 词条查询
POST /bookdb_index/book/_search
{
"query": {
"term" : {
"publisher": "manning"
}
},
"_source" : ["title","publish_date","publisher"]
}
# 多词条查询
POST /bookdb_index/book/_search
{
"query": {
"terms" : {
"publisher": ["oreilly", "packt"]
}
}
}
# 排序
POST /bookdb_index/book/_search
{
"query": {
"term" : {
"publisher": "manning"
}
},
"_source" : ["title","publish_date","publisher"],
"sort": [
{ "publish_date": {"order":"desc"}}
]
}
# 范围查询
POST /bookdb_index/book/_search
{
"query": {
"range" : {
"publish_date": {
"gte": "2015-01-01",
"lte": "2015-12-31"
}
}
},
"_source" : ["title","publish_date","publisher"]
}
查询3
# match_all
POST /crawl_log/_search
{
"query": {
"match_all": {}
}
}
# term
POST /crawl_log/_search
{
"query":{
"term":{
"task_id":1
}
}
}
# from size
POST /crawl_log/_search
{
"query":{
"term":{
"task_id":1
},
},
"from": 10,
"size": 10
}
# match
POST /crawl_log/_search
{
"query": {
"match": {
"message": "中埃建交五十周年"
}
}
}
# match_phase
POST /crawl_log/_search
{
"query": {
"match_phase": {
"message": "中埃建交五十周年"
}
}
}
日志查询
GET /crawl_log
GET /crawl_log/_search
POST /crawl_log/_search
{
"query": {
"match_all": {}
}
}
POST /crawl_log/_search
{
"query": {
"bool": {
"must": [{
"term": {
"task_id": "1"
}
},
{
"match": {
"level_name": "INFO"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}
管理
# 创建一个索引
PUT twitte
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
# 创建mapping映射
PUT twitte
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"type1" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
}
# 查看索引的定义信息
GET /twitte
# 删除索引
DELETE /twitte
别名
# 查看别名
GET /_cat/aliases
# 增加一个别名
POST /_aliases
{"actions": [{"add": {"index": "twitte", "alias": "alias1"}}]}
# 删除别名
POST /_aliases
{"actions": [{"remove": {"index": "twitte", "alias": "alias1"}}]}
# 一个别名对应多个索引
POST /_aliases
{"actions": {"add": {"indices": ["twitte", "twitte2"], "alias": "alias1"}}}
# 一个别名对应多个索引(通配符)
POST /_aliases
{"actions": [{"add": {"index": "twitte*", "alias": "alias1"}}]}
索引复制
POST /_reindex?slices=9&refresh&wait_for_completion=false
POST _reindex?wait_for_completion=false
{
"source": {
"index": "crawl_log"
},
"dest": {
"index": "crawl_log_bak"
}
}
mapping
- 字符串: text和keyword
- 整数 : byte, short, integer, long
- 浮点数: float, double
- 布尔型: boolean
- 日期: date
# 创建索引
PUT /my-index
{
"mappings" {
"properties": {
"age": {"type": "integer"},
"email": {"type": "keyword"},
"name": {"type": "text"}
}
}
}
# 添加字段
PUT /my-index/_mapping
{
"properties": {
"content": {"type": "text"}
}
}
清空数据
# 清空索引
POST /policies_hydrabot/_delete_by_query?refresh&slices=5&pretty
{
"query": {
"match_all": {}
}
}
Curl请求示例
curl -X GET 'http://localhost:9200/_cat/indices?v'
curl -X GET 'http://localhost:9200/_mapping?pretty=true'
curl -X GET 'http://localhost:9200/crawl_log/_mapping?pretty=true'
curl -X PUT 'localhost:9200/weather'
curl -X DELETE 'localhost:9200/weather'
curl -X PUT 'http://localhost:9200/accounts/person/1' \
-H 'Content-Type: application/json' \
-d '{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'
curl -X POST 'http://localhost:9200/accounts/person' \
-H 'Content-Type: application/json' \
-d '{
"user": "李四",
"title": "工程师",
"desc": "系统管理"
}'
curl 'localhost:9200/accounts/person/1?pretty=true'
curl 'localhost:9200/weather/beijing/abc?pretty=true'
curl -X DELETE 'localhost:9200/accounts/person/1'
curl 'localhost:9200/accounts/person/_search'
curl 'localhost:9200/accounts/person/_search' \
-H 'Content-Type: application/json' \
-d '
{
"query" : { "match" : { "desc" : "软件" }}
}'