文章目录
一、elasticsearch基本操作
1._cat
GET _cat/nodes
GET _cat/health
GET _cat/master
GET _cat/indices
2.索引一个文档(换言之保存一条记录)
//POST可以不带id,不带自动生成id;如果携带id就和PUT操作一样
POST /department/_doc
{
"depart_name": "胸痛科"
}
//PUT必须带id,id存在更新,不存在新增
PUT /department/_doc/1
{
"depart_name": "胸痛科"
}
3.查询文档
// 发送请求
GET /department/_doc/1
// 返回结果
{
"_index" : "department",
"_type" : "_doc",
"_id" : "1",
"_version" : 7,
"_seq_no" : 9, // 并发控制字段,每次更新+1,用来做乐观锁
"_primary_term" : 2,// 同上,主分片重新分配,如重启,就会发生变化
"found" : true,
"_source" : {
"depart_name" : "胸痛科"
}
}
// 更新携带?if_seq_no=9&if_primary_term=2
PUT /department/_doc/1?if_seq_no=9&if_primary_term=2
{
"depart_name": "胸痛科"
}
4.更新文档
// 第一种方式 会进行对比操作,存在就不进行更新操作 noop(no operation 没有操作)
// 最新版本
POST /department/_update/2
{
"doc": {
"depart_name": "骨科"
}
}
// 返回结果
{
"_index" : "department",
"_type" : "_doc",
"_id" : "2",
"_version" : 3,
"result" : "noop",// no operation 没有操作
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
},
"_seq_no" : 17,
"_primary_term" : 2
}
// 或者
POST /department/_doc/2
{
"depart_name": "骨科"
}
// 第三种方式
PUT /department/_doc/2
{
"depart_name": "骨科"
}
// 同时支持添加新的字段 put和post也可以 区别在于_update更新是需要doc属性
POST /department/_update/2
{
"doc": {
"depart_name": "骨科",
"remark":"外伤科室"
}
}
总结:
5.删除文档和索引
DELETE /department/_doc/1
DELETE /index_users_2
6.bulk批量API
// 索引一个文档 id=1 name=zhangsan
// 索引一个文档 id=2 name=lisi
POST /users/_bulk
{"index":{"_id":"1"}}
{"name":"zhangsan"}
{"index":{"_id":"2"}}
{"name":"lisi"}
POST /_bulk
{"delete":{"_index":"website","_type":"_doc","_id":"123"}}
{"create":{"_index":"website","_type":"_doc","_id":"123"}}
{"title":"my blog first save"}
{"index":{"_index":"website"}}
{"title":"I second index a document and not confirm _id"}
{"update":{"_index":"website","_type":"_doc","_id":"123","retry_on_conflict":3}}
{"doc":{"title":"my blog updated"}}
7.es测试数据导入
POST /bank/_bluk
...具体的批量操作数据
https://gitee.com/xmc-space/common_content.git
二、elasticsearch进阶检索
1、SearchApi
es支持两种基本检索
一个是通过使用 REST request URI 发送搜索参数(uri+检索参数)
另一个是通过使用 REST request body 来发送他们(uri+请求体)
// 第一种方式 uri+请求参数
GET /bank/_search?q=*&sort=account_number:asc
// 第二种方式 uri+请求体
GET /bank/_search
{
"query": { "match": { "address": "mill lane" } }
}
2、Query DSL
也就是上面所说的请求体
match检索
//match-全文检索最终按照评分进行排序 会对检索条件进行分词匹配
// 含有Mill和Road的都会查找出来
GET /bank/_search
{
"query": {"match": {
"address": "Mill Road"
}}
}
//match_phrase-短语匹配 不会进行分词
GET /bank/_search
{
"query": {"match_phrase": {
"address": "Mill Road"
}}
}
//multi_match-多字段匹配
GET /bank/_search
{
"query": {"multi_match": {
"query": "mill",
"fields": ["address","employer"]
}}
}
bool复合查询
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{
"match": {
"age": "28"
}
}
],
"should": [
{
"match": {
"lastname": "w"
}
}
],
"filter": [ //最终结果过滤 不会贡献相关性得分
{
"range": {
"age": {
"gte": 20,
"lte": 38
}
}
}
]
}
}
}
filter查询
GET /bank/_search
{
"query": {
"bool": {
"filter": [
{"range": {
"age": {
"gte": 10,
"lte": 30
}
}}
]
}
}
}
term查询-精确查询 非text字段的检索
GET /bank/_search
{
"query": {
"term": {
"age": {
"value": "28"
}
}
}
}
// .keyword也可以精确检索
GET /bank/_search
{
"query": {
"match": {
"address.keyword": "149 Surf Avenue"
}
}
}
// match_phrase只是短语匹配
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "149 Surf"
}
}
}
聚合检索aggregations
##搜索address中包含mill的所有人的年龄分布情况和平均年龄
GET /bank/_search
{
"query": {
"match": {
"address": "mill"
}
},
"aggs": { //聚合
"ageAgg": { //聚合名称 自定义
"terms": {// 聚合类型 按照api进行指定
"field": "age",//聚合字段
"size": 10 //聚合结果假如有20种年龄分布 则只取其中10种
}
},
"ageAvg":{
"avg": {
"field": "age"
}
},
"balanceAvg":{
"avg": {
"field": "balance"
}
}
},
"size": 0 //只看聚合结果 不看hits
}
##按照年龄聚合,并且求出年段包含人数的平均薪资
GET /bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
}
}
}
##查出所有年龄分布,进步求出各年龄段不同性别的人的平均薪资以及总体平均薪资
GET /bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"genderAgg": {
"terms": {
"field": "gender.keyword",
"size": 10
},
"aggs": {
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
},
"balanceAva":{
"avg": {
"field": "balance"
}
}
}
}
},
"size": 0
}
3、映射-mapping
## 创建映射
PUT /my-index-000001
{
"mappings": {
"properties": {
"age": { "type": "integer" },
"email": { "type": "keyword" },
"name": { "type": "text" }
}
}
}
## 添加映射 index、doc_values做冗余的时候添加,节约空间
PUT /my-index-000001/_mapping
{
"properties": {
"employee-id": {
"type": "keyword",
"index": false //表明不是用于检索的字段
"doc_values": false // 表明不能用于聚合操作
}
}
}
##对于已经存在的映射字段,不能进行更新,更新必须创建新的索引进行数据迁移
##数据迁移
POST _reindex
{
"source": {
"index": "bank",
"type": "_doc" // 主要针对7以前的有type类型的版本数据的迁移
},
"dest": {
"index": "newbank"
}
}
4、分词
安装ik分词器(正对中文语法单词进行分词,英文是按照一个一个字进行分词)
https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.14.0