一、创建Index同时设置mapping\settings

PUT 'http://localhost:9200/index1'

{
  "settings": {
    "number_of_shards": 6,
    "number_of_replicas": 1,
    "refresh_interval": "10s",
    "translog":{
      "flush_threshold_size":"1gb",
      "sync_interval":"30s",
      "durability":"async"
    }
  },
  "mappings": {
    "properties":{
       "name":{
         "type":"keyword"
       },
       "age":{
         "type": "long"
       },
       "address":{
         "type":"text"
       },
       "birthday":{
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" 
       }
     }
   }
}
# 查看settings
GET `localhost:9200/index1/_settings?pretty`

#  查看mapping
GET `localhost:9200/index1/_mapping`

二、删除Index

DELETE 'http://localhost:9200/index1'

三、查询Index

# 可以使用_all或*来获取所有索引的信息
# 可以通过向该索引发送获取请求来确定索引的存在。
# 如果HTTP响应为200,则存在; 如果是404,它不存在。
GET 'http://localhost:9200/index1'

# 查询列出所有以创建的索引
GET 'http://localhost:9200/_cat/indices?v'

四、打开/关闭Index

# 通过在post中添加_close或_open来请求索引,可以很容易地关闭或打开一个或多个索引。 
# 关闭索引
POST 'http://localhost:9200/index1/_close'

# 打开索引-
POST 'http://localhost:9200/index1/_open'

五、Index别名

# _aliases关键字向任何索引提供别名。 
# 单个别名可以映射到多个别名,且别名不能与索引具有相同的名称。
POST `http://localhost:9200/_aliases`
# 请求正文
{
   "actions" : [
      { "add" : { "index" : "schools", "alias" : "schools_pri" } }
   ]
}
# 响应
{"acknowledged":true}

# 使用别名
GET http://localhost:9200/schools_pri
# 响应
{"schools":{"aliases":{"schools_pri":{}},"}}

六、_analyze

# _analyze是Elasticsearch一个非常有用的API,
# 它可以帮助你分析每一个field或者某个analyzer/tokenizer
# 是如何分析和索引一段文字。
# 返回结果字段含义:
# token是一个实际被存储在索引中的词
# position指明词在原文本中是第几个出现的
# start_offset和end_offset表示词在原文本中占据的位置。
# 默认analyzer:standard,此外还有whitespace、ik_max_word、pinyin
GET `http://localhost:9200/_analyze`
# 请求体
{"analyzer" : "standard",  "text" : "床前明月光"}
# 响应
{
  "tokens": [
    {
      "token": "床",
      "start_offset": 0,
      "end_offset": 1,
      "type": "<IDEOGRAPHIC>",
      "position": 0
    },
    {
      "token": "前",
      "start_offset": 1,
      "end_offset": 2,
      "type": "<IDEOGRAPHIC>",
      "position": 1
    },
    {
      "token": "明",
      "start_offset": 2,
      "end_offset": 3,
      "type": "<IDEOGRAPHIC>",
      "position": 2
    },
    {
      "token": "月",
      "start_offset": 3,
      "end_offset": 4,
      "type": "<IDEOGRAPHIC>",
      "position": 3
    },
    {
      "token": "光",
      "start_offset": 4,
      "end_offset": 5,
      "type": "<IDEOGRAPHIC>",
      "position": 4
    }
  ]
}

七、Index模板

# 以创建具有映射的索引模板,这可以应用于新的索引
# 以“tu”开头的任何索引都将具有与模板相同的设置。

POST `http://localhost:9200/_template/template_a`
# 响应
{
   "template" : "tu*", 
      "settings" : {
         "number_of_shards" : 3
   },

   "mappings" : {
      "chapter" : {
         "_source" : { "enabled" : false }
      }
   }
}

八、Index其他操作

# Index统计
# 可用于提取有关特定索引的统计信息。只需要发送一个带有索引URL和_stats关键字的get请求。
GET `http://localhost:9200/index1/_stats`

# 刷新清除数据
# 用于从索引内存中清除数据,并将其迁移到索引存储,并清除内部事务日志。
GET `http://localhost:9200/index1/_flush`

# 刷新索引
# 默认情况下,刷新在Elasticsearch中一般按计划来执行,
# 但可以使用_refresh显式刷新一个或多个索引。 
GET `http://localhost:9200/index1/_refresh`

# 逗号分隔符号
POST `http://localhost:9200/index1,index2,index3/_search`

# 可以使用_all表示所有索引
POST `http://localhost:9200/_all/_search`

# 通配符 (*,+[包含], -[排除] )
# 当使用通配符查询时,当有索引不存在的时候是否返回查询失败。值为true和false,默认为true.
# allow_no_indices 跟 ignore_unavailable 都是用来防止没有索引的错误的,它们的区别是:
# ignore_unavailable控制的是任何索引包括带通配符和不带通配符的,
# allow_no_indices 控制的是带通配符的索引
POST `http://localhost:9200/school*/_search?ignore_unavailable=true`

# 其他参数
# expand_wildcards
# 设置是否扩展通配符到closed的index中,值为
# open:表示只支持open类型的索引
# close:表示只支持关闭状态的索引
# none:表示不可用
# all:表示同时支持open和close索引

# format:表示返回数据的格式, 可选值为yaml和json两种。

# pretty:表示在已json格式返回数据时是否以可视化的格式返回, 
# false或未在设置表示不格式化, 否则格式化

# filter_path查询结果过滤,
# 比如说我们只需要 took, hits.total, hits.hits._id, hits._source

# 日期筛选(假设现在时间为:2021-03-22)
# 日期筛选的语法为:<static_name{date_math_expr{date_format |time_zone}}>
# 语法解释:
#  -static_ name:索引的名称;
#  -date_ math_ expr: 动态日期计算表达式;
#  -date_ format:日期格式;
#  -time_ zone: 时区,默认为UTC。
# GET 'http://localhost:9200/<*moives-{now/d}>/_search'
# 表达式说明:
#  <logstash-{now/d}>	结果: logstash-2024.03.22
#  <logstash-{now-2d}>	结果: logstash-2024.03.20
#  <logstash-{now/M}>	结果: logstash-2024.03.01
#  <logstash-{now/M{YYYY.MM}}>	结果: logstash-2024.03
#  <logstash-{now/M-1M{YYYY.MM}}>	结果: logstash-2024.02
#  <logstash-{now/d{YYYY.MM.dd|+12:00}}>	结果: logstash-2024.03.23