微信登录

索引库 - 索引库的操作 - 增删改查

索引库介绍

在Elasticsearch中,对索引库(即索引)进行操作的命令通常通过Elasticsearch的RESTful API来执行。以下是一些常用的索引操作命令:

功能 语法 例子 释义
索引统计信息 GET /index_name/_stats ``
索引健康状态 GET /_cluster/health/index_name ``
删除索引 DELETE /index_name DELETE /索引库名

创建索引:

基本步骤
定义索引名称:每个索引都有一个唯一的名称,例如my_index。
定义映射(Mappings):映射定义了索引中字段的类型和属性,例如日期格式、文本分析器等。
定义设置(Settings):设置可以包括分片(shards)数量、副本(replicas)数量、自定义分析器等。
创建索引:使用Elasticsearch的REST API发送创建索引的请求。

  1. PUT /my_index
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 1
  6. },
  7. "mappings": {
  8. "properties": {
  9. "title": {
  10. "type": "text",
  11. "analyzer": "ik_smart",
  12. "index": false
  13. },
  14. "date": {
  15. "type": "date"
  16. },
  17. "content": {
  18. "type": "text"
  19. },
  20. "name": {
  21. "type": "text",
  22. "properties":{ # name的子属性firstNamelastName
  23. "firstName":{
  24. "type": "text"
  25. },
  26. "lastName":{
  27. "type": "text"
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }

PUT /my_index:这个HTTP请求用来创建名为my_index的索引。如果索引已经存在,这个请求将会失败,除非使用?op_type=create参数来强制创建。
settings:定义了索引的设置。在这个例子中,我们设置了一个主分片和1个副本。
mappings:定义了索引的映射。这里我们定义了三个字段:title、date和content,它们的类型分别是text和date。
analyzer 指定分词器
index是否创建索引,true默认值,创建索引,false不创建

索引设置更新:

  1. PUT /index_name/_settings
  2. {
  3. "index": {
  4. "setting_name": "setting_value"
  5. }
  6. }