ElasticSearch8.x操作记录

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状态码 200404
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.中文分词

#分词操作
### Elasticsearch 8.x 版本学习笔记 #### 关键特性概述 Elasticsearch 是一款分布式搜索引擎,支持实时搜索、多租户等功能。其核心优势在于能够快速处理大量数据并提供高效的全文检索能力[^1]。 #### 安装与启动 对于初次使用者来说,可以从官方网站下载适合的操作系统版本进行安装。完成安装后通过命令行工具执行 `bin/elasticsearch` 启动服务实例。为了确保集群稳定运行,建议按照官方文档中的最佳实践来调整JVM参数和其他环境变量设置[^2]。 #### 创建索引及Mapping定义 创建一个新的索引时可以通过API指定名称以及配置文件路径: ```json PUT /my-index-000001 { "settings": { "index.number_of_shards": 3, "index.number_of_replicas": 2 }, "mappings": { "_source": { "enabled": true }, "properties": { "name": {"type": "keyword"}, "description": {"type": "text"} } } } ``` 此段代码展示了如何设定shard数量和副本数,并为特定字段指定了相应的类型——例如这里设置了`name`作为关键字型(`keyword`)而`description`则被设成文本型(`text`)[^3]。 #### 动态模板应用 当面对未知结构的数据源时,动态模板允许自动识别新加入字段的性质从而为其分配最恰当的数据表示形式。这极大地方便了灵活应对各种业务场景下的需求变化。 ```json PUT _template/my_template_1 { "index_patterns": ["te*", "bar*"], "priority": 1, "version": 5, "settings": {}, "aliases": {}, "mappings": { "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "mapping": { "type": "keyword" } } } ] } } ``` 这段JSON片段说明了一个简单的动态模板案例,其中任何匹配模式下新增加字符串类型的字段都将默认转换成关键词类型存储。 #### 正排索引 vs 倒排索引 正排索引指的是基于唯一标识符查找对应记录的方式;相反地,倒排索引则是依据某些特征值反向定位到拥有这些特性的所有条目集合之中。这种机制使得即使是在海量数据库里也能迅速实现精准查询操作[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值