前言
本文只讲语法,下载安装请查看专栏其他文章,语法由简入繁,有错误的地方请海涵指正,本文采用的是es7.x并配有ik分词器,所以没有type概念。
演示数据
kibana执行添加
PUT /test_index/_bulk
{"index":{"_id":"1"}}
{"name":"张三丰","age":30,"address":"河南省郑州市高新区"}
{"index":{"_id":"2"}}
{"name":"李四喜","age":40,"address":"北京市海淀区"}
{"index":{"_id":"3"}}
{"name":"王五毛","age":15,"address":"湖南省长沙市"}
{"index":{"_id":"4"}}
{"name":"赵六才","age":27,"address":"河北省沧州市"}
{"index":{"_id":"5"}}
{"name":"钱七福","age":22,"address":"河南省郑州市二七区"}
{"index":{"_id":"6"}}
{"name":"孙老八","age":60,"address":"河南省郑州市管城区"}
{"index":{"_id":"7"}}
{"name":"吴老九","age":25,"address":"河南省郑州市高新区"}
{"index":{"_id":"8"}}
{"name":"郑在来","age":33,"address":"河南省郑州市高新区"}
增删改查
添加
- 添加索引index(类似数据库)
PUT /test_index
查看添加的索引信息
GET _cat/indices/test_index
- 添加文档(相当于表中的每行数据)
//1是文档的id
PUT /test_index/_doc/1
{
"name":"张三",
"age":30
}
//再添加一行
PUT /test_index/_doc/2
{
"name":"李四",
"age":35
}
- 批量添加-必须保证数据写在一行
PUT /test_index/_bulk
{"index":{"_id":"5"}}
{"name":"王五","age":32}
{"index":{"_id":"6"}}
{"name":"赵六","age":33}
删除
- 删除索引
DELETE /test_index
- 删除文档
DELETE /test_index/_doc/1
修改
- PUT 更新,相当于覆盖重新添加
PUT /test_index/_doc/4
{
"name":"张三2",
"age":30
}
- POST更新,针对某个字段更新
POST /test_index/_update/1
{
"doc":{
"name":"张三2"
}
}
查询
查询索引
- 查询全部索引
GET _cat/indices
- 查询指定索引
GET _cat/indices/test_index
查询索引中的数据
- 查询全部
GET /test_index/_search
//或者
GET /test_index/_search
{
"query": {"match_all": {}}
}
- 简单条件查询
GET /test_index/_search
{
"query": {
//根据name模糊查询
"match": {
"name": "张三"
}
},
// 排序
"sort": [
{
"age": {
"order": "asc"
}
}
],
//分页
"from": 0,
"size": 3,
//过滤,查询年龄大于16小于18的
"post_filter": {
"range": {
"age": {
"gte": 16,
"lte": 20
}
}
}
//过滤,查询结果只显示哪些字段
"_source": "name"
},
//高亮显示
"highlight": {"fields": {"name": {}}}
- 精确查询
GET /test_index/_search
{
"query": {
"match": {
"name.keyword": "张三"
}
}
}
- match_phrase-短语匹配
区别于match,短语匹配不会分词
GET /test_index/_search
{
"query": {
"match_phrase": {
"name": "张三"
}
}
}
- multi_math-多字段匹配
检索 name 或 address 匹配包含 张三 的数据,会对查询条件分词
GET /test_index/_search
{
"query": {
"multi_match": {
"query": "张三",
"fields": ["name","address"]
}
}
}
bool-复合查询
● must:必须达到must所列举的所有条件
● must_not,必须不匹配must_not所列举的所有条件。
● should,应该满足should所列举的条件
GET /test_index/_search
{
"query": {
"bool": {
"must": [
{"match": {
"age": "33"
}},
{
"match": {
"name": "张三"
}
}
]
}
}
}
term-精确检索
Elasticsearch 官方对于非文本字段,推荐使用 term来精确检索,文本字段用match
GET /test_index/_search
{
"query": {
"term": {
"age": {
"value": "33"
}
}
}
}
Aggregation-执行聚合
按年龄聚合,并且计算平均年龄
GET /test_index/_search
{
"query": {
"match": {
"address": "河南"
}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 10
}
},
"ageAvg":{
"avg": {
"field": "age"
}
}
}
}
# "ageAgg": { --- 聚合名为 ageAgg
# "terms": { --- 聚合类型为 term
# "field": "age", --- 聚合字段为 age
# "size": 10 --- 取聚合后前十个数据
# }
# },
# ------------------------
# "ageAvg": { --- 聚合名为 ageAvg
# "avg": { --- 聚合类型为 avg 求平均值
# "field": "age" --- 聚合字段为 age
# }
# },
# ------------------------
# "balanceAvg": { --- 聚合名为 balanceAvg
# "avg": { --- 聚合类型为 avg 求平均值
# "field": "balance" --- 聚合字段为 balance
# }
# }
# ------------------------
# "size": 0 --- 不显示命中结果,只看聚合信息
Mapping-映射
Maping是用来定义一个文档(document),以及它所包含的属性(field)是如何存储和索引的。
创建索引映射
创建索引并指定属性的映射规则(相当于新建表并指定字段和字段类型)
PUT /my_index
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"email": {
"type": "keyword"
},
"name": {
"type": "text"
}
}
}
}
- 给已有映射增加字段
PUT /my_index/_mapping
{
"properties": {
"id": {
"type": "keyword",
"index": false
}
}
}
# 这里的 "index": false,表明新增的字段不能被检索。默认是true
- 更新映射
对于已经存在的字段映射,我们不能更新。更新必须创建新的索引,进行数据迁移
1、创建新索引 my_new_index并修改字段类型 age
PUT /my_new_index
{
"mappings": {
"properties": {
"age": {
"type": "text"
},
"email": {
"type": "keyword"
},
"name": {
"type": "text"
}
}
}
}
2、迁移数据
POST _reindex
{
"source": {"index": "my_index"},
"dest": {"index": "my_new_index"}
}
分词
- 自带分词
测试
GET _analyze
{
"analyzer": "standard",
"text":"今天是个好天气"
}
- ik分词器
POST _analyze
{
"analyzer": "ik_max_word",
"text":"今天是个好天气"
}