索引管理
# 创建索引
# elasticsearch.yml
# action.auto_create_index: false 禁止自动创建索引
PUT /my_index
{
"settings": {},
"mappings": {
"type_one":{
"properties":{
"name":{
"type":"text",
"analyzer":"standard"
}
}
}
}
}
# 删除索引
# DELETE /index_one,index_two
# DELETE /index_*
# DELETE /_all
# DELETE /*
# elasticsearch.yml
# action.destructive_requires_name: true 强制使用精确索引名删除索引
DELETE /my_index
# 索引设置
PUT /index_temp_set
{
"settings": {
"number_of_shards": 5, // 主分片数量,索引创建后不可修改
"number_of_replicas": 1 // 副本数量,索引创建后可以更改
}
}
# 修改副本数
PUT /index_temp_set/_settings
{
"number_of_replicas": 2
}
# 配置分析器
PUT /index_temp_set
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer":{
"type":"standard",
"stopwords":"_spanish_"
}
}
}
}
}
GET /index_temp_set/_analyze
{
"analyzer": "my_analyzer",
"text": ["El veloz zorro marrón"]
}
# 自定义分析器
# 字符过滤器:分词前进行字符过滤
# 分词器:进行分词
# 词单元过滤器:分词后对词进行过滤
PUT /my_index
{
"settings": {
"analysis": {
"char_filter": {
"&_to_and":{
"type":"mapping",
"mappings":["&=>and"]
}
},
"filter": {
"my_stopwords":{
"type":"stop",
"stopwords":["the", "a"]
}
},
"analyzer": {
"my_analyzer":{
"type":"custom",
"char_filter":["html_strip", "&_to_and"],
"tokenizer":"standard",
"filter":["lowercase","my_stopwords"]
}
}
}
}
}
GET /my_index/_analyze
{
"analyzer": "my_analyzer",
"text": ["<p>The quick & brown fox</p>"]
}
# 将自定义的分析器使用在文档属性上
# 属性三个重要的设置 type、analyzer、index:true/false
PUT /my_index/_mapping/my_type
{
"properties": {
"title":{
"type":"text",
"analyzer": "my_analyzer"
}
}
}
# 元数据
# _source字段,存储元数据
# _all,其值为大字符串的特殊值
# 搜索值在title中出现比在content中出现更重要,但是使用_all字段搜索,体现不出重要性
# 可以通过如下方式禁用_all字段
PUT /my_index/_mapping/my_type
{
"my_type": {
"_all": { "enabled": true }
}
}
# 也可以手动定义定义哪些字段需要包含在_all字段中
PUT /my_index/_mapping/my_type
{
"properties":{
"tags":{
"type":"text",
"copy_to":"_all"
}
}
}
# 自定义动态映射
# 关闭日期检测
PUT /my_index/_mapping
{
"date_detection":false
}
# 索引别名和零停机
PUT /my_index_v1
{
"mappings": {
"my_type": {
"properties": {
"tags":{
"type":"keyword"
}
}
}
},
"aliases": {
"my_index": {}
}
}
PUT /my_index_v2
{
"mappings": {
"my_type": {
"properties": {
"tags":{
"type":"keyword"
}
}
}
},
"aliases": {
"my_index": {}
}
}
# 查看哪些索引指向别名my_index
GET /*/_alias/my_index
# 查询my_index_v1索引指向哪个别名
GET /my_index_v1/_alias/*
# 操作别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "test1",
"alias": "alias1"
},
"remove": {
"index":"test2",
"alias":"alias2"
}
}
]
}