ElasticSearch8.x操作记录
文档内容来自于尚硅谷海波老师的ElasticSearch教程课,在Kibana中的一些操作演示
以下为在文档中的相关操作记录
1.索引操作
#创建索引
#PUT 索引名称
PUT test_index
#PUT 索引
#增加配置:JSON格式的主题内容
PUT test_index_1
{
"aliases": {
"test1": {
}
}
}
#删除索引
#delete 索引名称
DELETE test_index_1
#修改索引配置
#ES不允许修改索引信息
POST test_index_1
{
"aliases": {
"test1": {
}
}
}
#HEAD索引 (判读索引是否存在)HTTP状态码 200, 404
HEAD test_index
#查询索引
GET test_index
GET test_index_1
GET test1
#查询所有索引
GET _cat/indices
#创建文档(索引数据)--增加唯一性标识(手动:PUT,后面需要自己添加/自动;POST自动生成,不需要再后面添加)
#首先需要先创建索引
PUT index_doc
PUT index_doc/_doc/1001
{
"id": 1001,
"name": "zhangsan",
"age": 30
}
POST index_doc/_doc
{
"id": 1002,
"name": "lisi",
"age": 14
}
2.文档操作
#查询文档
GET index_doc/_doc/1001
#查询当前索引中所有的文档数据
GET index_doc/_search
#修改文档数据
PUT index_doc/_doc/1001
{
"id": 100111,
"name": "zhangsan",
"age": 30,
"tel": "15123392594"
}
#POST修改数据
POST index_doc/_doc/okBdhIQB7PHEeADHmDqa
{
"id": 1003,
"name": "wangwu",
"age": 22
}
#删除数据
DELETE index_doc/_doc/okBdhIQB7PHEeADHmDqa
#以下操作是不被允许的
DELETE index_doc/_doc
3.文档搜索
#增加索引
PUT test_query
DELETE test_query
#添加数据
PUT test_query/_bulk
{
"index":{
"_index": "test_query", "_id":"1001"}}
{
"id":"1001", "name": "zhang san", "age": 30}
{
"index":{
"_index": "test_query", "_id":"1002"}}
{
"id":"1002", "name": "li si", "age": 40}
{
"index":{
"_index": "test_query", "_id":"1003"}}
{
"id":"1003", "name": "wang wu", "age": 50}
{
"index":{
"_index": "test_query", "_id":"1004"}}
{
"id":"1004", "name": "zhangsan", "age": 30}
{
"index":{
"_index": "test_query", "_id":"1005"}}
{
"id":"1005", "name": "lisi", "age": 40}
{
"index":{
"_index": "test_query", "_id":"1006"}}
{
"id":"1006", "name": "wangwu", "age": 50}
#Match是分词查询,ES会将数据分词(关键词)保存
#zhang san
GET test_query/_search
{
"query": {
"match": {
"name": "zhang san"
}
}
}
GET test_query/_search
{
"query": {
"term": {
"name": {
"value": "zhang san"
}
}
}
}
#对查询结果字段进行限制
GET test_query/_search
{
"_source": ["name", "age"],
"query": {
"match": {
"name": "zhang san"
}
}
}
#组合多个条件 or
GET test_query/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "zhang"
}
},
{
"match": {
"age": "40"
}
}
]
}
}
}
# 排序后查询
GET test_query/_search
{
"query": {
"match": {
"name": "zhang li"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
#分页查询
GET test_query/_search
{
"query": {
"match_all": {
}
},
"from": 4,
"size": 2
}
4.聚合搜索
# 分组查询
GET test_query/_search
{
"aggs": {
"ageGroup": {
"terms": {
"field": "age"
}
}
},
"size": 0
}
# 分组后聚合(求和)
GET test_query/_search
{
"aggs": {
"ageGroup": {
"terms": {
"field": "age"
},
"aggs": {
"ageSum": {
"sum": {
"field": "age"
}
}
}
}
},
"size": 0
}
# 求年龄平均值
GET test_query/_search
{
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
},
"size": 0
}
# 获取前几名操作
GET test_query/_search
{
"aggs": {
"top3": {
"top_hits": {
"sort": [
{
"age": {
"order": "desc"
}
}
],
"size": 3
}
}
},
"size": 0
}
5.索引模板
PUT test_temp
GET test_temp
PUT test_temp_1
{
"settings": {
"number_of_shards": 2
}
}
GET test_temp_1
#创建模板
PUT _template/mytemplate
{
"index_patterns": [
"my*"
],
"settings": {
"index": {
"number_of_shards" : "2"
}
},
"mappings": {
"properties": {
"now": {
"type": "date",
"format": "yyyy/MM/dd"
}
}
}
}
#查看模板
GET _template/mytemplate
PUT test_temp_2
GET test_temp_2
# 匹配模板规则,以my开头
PUT my_test_temp
GET my_test_temp
#删除模板
DELETE _template/mytemplate
6.中文分词
#分词操作
GET _analyze
{