Index 索引
ElasticSearch    2018-09-01 00:40:07    17    0    0
cqc   ElasticSearch
添加索引
PUT /idx 添加名称叫做idx的索引,默认生成5个primary主分片及一个replica副本

当不需要保存源数据时,可设置_source为disable
PUT /my_index
{
    "mappings": {
        "my_type": {
            "_source": {
                "enabled":  false
            }
        }
    }}
禁用_all字段
PUT /my_index/_mapping/my_type
{
    "my_type": {
        "_all": { "enabled": false }
    }}
部分使用_all字段
PUT /my_index/my_type/_mapping
{
    "my_type": {
        "include_in_all": false,
        "properties": {
            "title": {
                "type":           "string",
                "include_in_all": true
            },
            ...
        }
    }}
可设置_all解析器
PUT /my_index/my_type/_mapping
{
    "my_type": {
        "_all": { "analyzer": "whitespace" }
    }}
重建索引 reindex
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }}

索引别名
PUT /my_index_v1/_alias/my_index 设置别名。
GET /*/_alias/my_index 查看指定别名指向哪些索引
GET /my_index_v1/_alias/* 查看指定索引拥有哪些别名
无缝切换索引
POST /_aliases
{
    "actions": [
        { "remove": { "index": "my_index_v1", "alias": "my_index" }},
        { "add":    { "index": "my_index_v2", "alias": "my_index" }}
    ]}


可将不同的索引设置为相同的别名,这些导致这个别名拥有多个索引的引用,在查询该别名时,相当于同时查多个索引
用好别名,可以程序从一个索引无缝切换到另一个索引


删除索引
DELETE /my_index
DELETE /index_one,index_two
DELETE /index_*
DELETE /_all (DELETE /*)




settings 索引设置参数
参数名称参数作用
number_of_shards主分片的片数,在创建索引的时候有效
number_of_replicas副本的数量,可动态变更
auto_expand_replicas自动扩展副本。默认false,可设置副本的上下线,如0-5, 0-all

refresh_interval索引刷新间隔,即在数据改变后通过刷新使其可见。默认1s,-1表示禁用
max_result_windowfrom+size的最大值,默认为10000
analysis配置解析器部分
analyzer
char_filter
filter
tokenizer

详细配置:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html


配置解析器 configuring analyzers
在现有的解析器上更改默认设置
PUT /spanish_docs
{
    "settings": {
        "analysis": {
            "analyzer": {
                "es_std": {
                    "type":      "standard",
                    "stopwords": "_spanish_"
                }
            }
        }
    }}
创建自定义解析器
PUT /my_index
{
    "settings": {
        "analysis": {
            "char_filter": { ... custom character filters ... },
            "tokenizer":   { ...    custom tokenizers     ... },
            "filter":      { ...   custom token filters   ... },
            "analyzer":    { ...    custom analyzers      ... }
        }
    }}
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" ]
            }}}}}
应用解析器
PUT /my_index/_mapping/my_type
{
    "properties": {
        "title": {
            "type":      "string",
            "analyzer":  "my_analyzer"
        }
    }}

测试解析器
GET /spanish_docs/_analyze?analyzer=es_std
El veloz zorro marrón
注意自定义解析器并非是全局的,仅存在索引级别


elasticsearch.yml参数
action.auto_create_index: false 不允许自动创建索引
action.destructive_requires_name: true 禁止模糊删除索引 如 /_all, /index_*

注意
在同一个索引下,不同_type下同名field不能有不同的数据类型, es在创建该索引时将会报错拒绝.因为在lucene中是有没_type概念,下面的这段在es的索引设置
{
   "data": {
      "mappings": {
         "people": {
            "properties": {
               "name": {
                  "type": "string",
               },
               "address": {
                  "type": "string"
               }
            }
         },
         "transactions": {
            "properties": {
               "timestamp": {
                  "type": "date",
                  "format": "strict_date_optional_time"
               },
               "message": {
                  "type": "string"
               }
            }
         }
      }
   }}
在lucene中实际表现为
{
   "data": {
      "mappings": {
        "_type": {
          "type": "string",
          "index": "not_analyzed"
        },
        "name": {
          "type": "string"
        }
        "address": {
          "type": "string"
        }
        "timestamp": {
          "type": "long"
        }
        "message": {
          "type": "string"
        }
      }
   }}


添加索引
PUT /idx 添加名称叫做idx的索引,默认生成5个primary主分片及一个replica副本

当不需要保存源数据时,可设置_source为disable
PUT /my_index
{
"mappings": {
"my_type": {
"_source": {
"enabled": false
}
}
}}
禁用_all字段
PUT /my_index/_mapping/my_type
{
"my_type": {
"_all": { "enabled": false }
}}
部分使用_all字段
PUT /my_index/my_type/_mapping
{
"my_type": {
"include_in_all": false,
"properties": {
"title": {
"type": "string",
"include_in_all": true
},
...
}
}}
可设置_all解析器
PUT /my_index/my_type/_mapping
{
"my_type": {
"_all": { "analyzer": "whitespace" }
}}
重建索引 reindex
POST /_reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}}

索引别名
PUT /my_index_v1/_alias/my_index 设置别名。
GET /*/_alias/my_index 查看指定别名指向哪些索引
GET /my_index_v1/_alias/* 查看指定索引拥有哪些别名
无缝切换索引
POST /_aliases
{
"actions": [
{ "remove": { "index": "my_index_v1", "alias": "my_index" }},
{ "add": { "index": "my_index_v2", "alias": "my_index" }}
]}


可将不同的索引设置为相同的别名,这些导致这个别名拥有多个索引的引用,在查询该别名时,相当于同时查多个索引
用好别名,可以程序从一个索引无缝切换到另一个索引


删除索引
DELETE /my_index
DELETE /index_one,index_two
DELETE /index_*
DELETE /_all (DELETE /*)




settings 索引设置参数

参数名称参数作用
number_of_shards主分片的片数,在创建索引的时候有效
number_of_replicas副本的数量,可动态变更
auto_expand_replicas自动扩展副本。默认false,可设置副本的上下线,如0-5, 0-all
refresh_interval索引刷新间隔,即在数据改变后通过刷新使其可见。默认1s,-1表示禁用
max_result_windowfrom+size的最大值,默认为10000
analysis
配置解析器部分
analyzer
char_filter
filter
tokenizer

详细配置:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html


配置解析器 configuring analyzers
在现有的解析器上更改默认设置
PUT /spanish_docs
{
"settings": {
"analysis": {
"analyzer": {
"es_std": {
"type": "standard",
"stopwords": "_spanish_"
}
}
}
}}
创建自定义解析器
PUT /my_index
{
"settings": {
"analysis": {
"char_filter": { ... custom character filters ... },
"tokenizer": { ... custom tokenizers ... },
"filter": { ... custom token filters ... },
"analyzer": { ... custom analyzers ... }
}
}}
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" ]
}}}}}
应用解析器
PUT /my_index/_mapping/my_type
{
"properties": {
"title": {
"type": "string",
"analyzer": "my_analyzer"
}
}}

测试解析器
GET /spanish_docs/_analyze?analyzer=es_std
El veloz zorro marrón
注意自定义解析器并非是全局的,仅存在索引级别


elasticsearch.yml参数
action.auto_create_index: false 不允许自动创建索引
action.destructive_requires_name: true 禁止模糊删除索引 如 /_all, /index_*

注意
在同一个索引下,不同_type下同名field不能有不同的数据类型, es在创建该索引时将会报错拒绝.因为在lucene中是有没_type概念,下面的这段在es的索引设置
{
"data": {
"mappings": {
"people": {
"properties": {
"name": {
"type": "string",
},
"address": {
"type": "string"
}
}
},
"transactions": {
"properties": {
"timestamp": {
"type": "date",
"format": "strict_date_optional_time"
},
"message": {
"type": "string"
}
}
}
}
}}
在lucene中实际表现为
{
"data": {
"mappings": {
"_type": {
"type": "string",
"index": "not_analyzed"
},
"name": {
"type": "string"
}
"address": {
"type": "string"
}
"timestamp": {
"type": "long"
}
"message": {
"type": "string"
}
}
}}


上一篇: Snapshot 备份还原

下一篇: Document operations 文档操作

文档导航