ES
一、简介
1、Elastic Stack
ES技术栈:
- ElasticSearch:存数据+搜索;QL;
- Kibana:Web可视化平台,分析。
- LogStash:日志收集,Log4j:产生日志;log.info(xxx)
- 。。。。
使用场景:
- metrics:指标监控
- …
2、基本概念
- Index(索引)
- 动词:保存(插入)
- 名词:类似MySQL数据库,给数据
- Type(类型)
- 已废弃,以前类似MySQL的表
- 现在用索引对数据分类
- Document(文档)
- 真正要保存的一个JSON数据
{ name:"tcx" }
二、入门实战
{ "name" : "DESKTOP-1TSVGKG", "cluster_name" : "elasticsearch", "cluster_uuid" : "PFpgHGhaTZiajHD4xvRPkg", "version" : { "number" : "7.14.0", "build_flavor" : "default", "build_type" : "zip", "build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1", "build_date" : "2021-07-29T20:49:32.864135063Z", "build_snapshot" : false, "lucene_version" : "8.9.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
1、索引CRUD
1.创建索引
put http://localhost:9200/person { "acknowledged": true, #响应结果 "shards_acknowledged": true, #分片 "index": "person" #索引名称 }
2.删除索引
delete http://localhost:9200/person { "acknowledged": true }
3.修改索引
# 在修改时,需要先删掉索引 put http://localhost:9200/person { "settings": { "index": { "number_of_shards":"3" } } }
4.查询索引
get http://localhost:9200/person { "person": { "aliases": {}, "mappings": {}, "settings": { "index": { "routing": { "allocation": { "include": { "_tier_preference": "data_content" } } }, "number_of_shards": "3", "provided_name": "person", "creation_date": "1664357188860", "number_of_replicas": "1", "uuid": "GOyDifOJTQi8yBzKHk5zNQ", "version": { "created": "7140099" } } } } }
2、文档CRUD
1、保存-POST(非幂等性)
POST /person/_doc { "id":"1", "name":"田", "age":"19" } # 结果 { "_index" : "person", //保存到的索引 "_type" : "_doc", // "_id" : "BcKKg4MBcNCrjSmlWbAK", //刚才那条数据的唯一id "_version" : 1, //版本号 "result" : "created", //创建了数据 "_shards" : { //分片工作 "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, //乐观锁机制 "_primary_term" : 1 //乐观锁机制 }
以上是:非幂等操作
- 幂等:同一个操作执行无数遍的结果都是一样的
- select
- insert: 幂等
- 非幂等:
- insert: 自增主键就是非幂等
2、保存-PUT(幂等性)
保存更新 二合一
PUT /person/_doc/1 { "id":"1", "name":"田", "age":"19" } PUT /person/_doc/1 { "id":"1", "name":"田", "age":"19", "note":"做个小笔记" } # 也可以任意扩展/缩减字段;(会覆盖)
3、POST-增量字段更新
POST person/_doc/1/_update { "doc":{ "name":"张三" } }
4、查询
# 查看所有 GET /person/_search # 根据ID查询 GET /person/_doc/ID号
5、乐观锁
CAS
三、进阶检索
# DSL 查询条件以Json POST person/_search { "query": { "match_all": {} } }
1、导入基本数据
POST /student/_doc/1001 { "name":"李白", "age":"18" } POST /student/_doc/1002 { "name":"诗仙", "age":"19" } POST /student/_doc/1003 { "name":"杜甫", "age":"20" } POST /student/_doc/1004 { "name":"诗圣", "age":"21" } POST /student/_doc/1005 { "name":"诗圣 厉害", "age":"21" }
2、查询所有文档
POST /student/_search { "query": { "match_all": {} } } POST /student/_search { "query": { "match_all": {} } } { "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "student", "_type" : "_doc", "_id" : "1001", "_score" : 1.0, "_source" : { "name" : "李白", "age" : "18" } }, { "_index" : "student", "_type" : "_doc", "_id" : "1002", "_score" : 1.0, "_source" : { "name" : "诗仙", "age" : "19" } }, { "_index" : "student", "_type" : "_doc", "_id" : "1003", "_score" : 1.0, "_source" : { "name" : "杜甫", "age" : "20" } }, { "_index" : "student", "_type" : "_doc", "_id" : "1004", "_score" : 1.0, "_source" : { "name" : "诗圣", "age" : "21" } } ] } }
3、match-全文索引精确查询
POST /student/_search { "query": { "match": { "name.keyword": "诗圣" } } }
multi_match-多个字段查询
POST /student/_search { "query": { "multi_match": { "query": "诗圣", "fields": ["name"] } } }
4、term-精确查询
POST /student/_search { "query": { "term": { "name": { "value": "shixian" } } } }
5、terms-查询
# 查询age是20,18的 POST /student/_search { "query": { "terms": { "age": [ "20", "18" ] } } }
6、多个字段查询
7、查询部分字段
POST /student/_search { "query": { "match": { "name": "诗圣" } }, "_source": ["name"] }
8、区间查询
POST /student/_search { "query": { "range": { "age": { "gte": 18, "lte": 20 } } } }
9、复合查询
# must POST /student/_search { "query": { "bool": { "must": [ { "range": { "age": { "gte": 18, "lte": 20 } } },{ "match": { "name": "诗圣" } } ] } } }
must和filter执行的结果是一样的,但是must会有分数,filter没有分数