在es中有很多配置参数,有些配置是可以在建好索引后重新配置的,比如索引的副本数量、索引的分词等
1.更新索引配置
请求:PUT http://127.0.0.1:9200/secisland/_settings
参数:
{
"index":{"number_of_replicas":4}
}
更新分词器。添加索引分析器之前必须先关闭索引,添加之后再打开索引
POST http://127.0.0.1:9200/secisland/_close
PUT http://127.0.0.1:9200/secisland/_settings
参数:
{
"analysis":{
"analyzer":{
"content":{"type":"custom","tokenizer":"whitespace"}
}
}
}
POST http://127.0.0.1:9200/secisland/_open
2.获取配置
GET http://127.0.0.1:9200/secisland/_settings
过滤配置参数的返回结果:
GET http://127.0.0.1:9200/secisland/_settings/name=index.number_*
name=index.number_*设置将只返回number_of_replicas,number_of_shards两个参数详情
3.索引分析
首先,把一个文本块分析成一个个单独的词,为后面的倒排索引做准备
一个分析器是由以下三个功能组合而成:
- 字符过滤器(character filter):字符串经过字符过滤器处理,他们的工作是标记化之前处理字符串。字符过滤器能够去除HTML标记,或者转换"&“为"and”。
- 分词器(tokenizer):分词器被标记化成独立的词。一个简单的分词器可以根据空格或逗号将单词分开
- 标记过滤器(token filters):每个词都通过所有标记过滤处理,它可以转小写,去掉词,增加词
自定义分析器:
请求POST 127.0.0.1:9200/_analyze
{
"tokenizer":"keyword",
"token_filters":["lowercase"],
"char_filters":["html_strip"],
"text":"this is a <b>test</b>"
}
如果想获取分析器分析的更多细节,设置explain属性为true(默认是false),将输出的分词器的分词详情
请求POST http://127.0.0.1:9200/secisland_analyze
参数:
{
"tokenizer":"standard",
"token_filters":["snowball"],
"text":"detailed output",
"explain":true,
"attributes":["keyword"]
}
4.索引模板
- 创建索引模板
索引模板就是创建好一个索引参数设置(settings)和映射(mapping)的模板,再创建新索引的时候指定模板名称就可以使用
请求:PUT http://127.0.0.1:9200/_template/template_1
{
"template":"te*",
"settings":{"number_of_shards":1},
"mappings":{"type1":{"_source":{"enabled":false}}}
}
可以用te*来适配,分片数量为1,默认文档类型是type1,_source的enabled为false
也可以再模板中定义别名等其他属性
2.删除索引模板
DELETE http://127.0.0.1:9200/_template/template_1
- 获取索引模板
GET http://127.0.0.1:9200/_template/template_1
使用通配符或者逗号分隔符可以获取多个索引模板
获取所有索引模板:
GET http://127.0.0.1:9200/_template/
判断索引模板是否存在:
HEAD http://127.0.0.1:9200/_template/template_1
- 多个模板匹配存在的问题
template1、template2两个模板,使用te*会匹配2个模板,最后合并两个模板的配置,如果配置重复,这时应该设置order属性,order是从0开始的数字,先匹配小的,再匹配大的,如果属性相同,后匹配的会覆盖之前的配置 - 复制配置
复制配置可以选择索引数据保存在磁盘上的位置,也可以指如何在副本分片上进行操作
充分利用index.data_path和index.shadow_replicas设置,可以使相同数据目录为多个实例服务,需在elasticsearch.yml中设置node.add_id_to_custom_path为false
elasticsearch.yml中的path.shared_data权限管理标明自定义索引的位置
path.shared_data:/opt/data
这意味着es可以读写path.shared_data设置中所有子目录中的文档
可以在自定义数据路径中创建索引,每个节点用这个路径来存储数据:
PUT http://127.0.0.1:9200/secisland
{
"index":{"number_of_shards":1,"number_of_replicas":4,
"data_path':"/opt/data/secisland","shadow_replicas":true
}
}
index.shadow_replicas设置为"true"不会在任何副本上重复文档操作,反而会不停地刷新
index.data_path—字符型索引数据使用的路径。es默认附加节点序号到路径中,确保相同机器上的多重实例不会共享数据目录。
index.shadow_replicas—布尔值,指示索引是否应该使用副本。默认false
index.shared_filesystem—布尔值,指示索引使用共享文件系统.如果index.shadow_replicas设置为true,默认为true,其他情况下默认为false
- 重建索引
重建索引的最基本功能是拷贝一个文件从一个索引到另一个索引,例如:
请求:POST http://127.0.0.1:9200/_reindex
{
"source":{"index":"secisland"},
"dest":{"index":"new_secisland"}
}
返回值如下:
{
"took":639,
"updated":112,
"batches":130,
"version_conflicts":0,
"failures":[],
"created":12344
}
took:从开始到结束整个操作的毫秒数
updated:已成功更新的文档数
created:成功创建的文档数
batches:从创建索引拉回滚动响应的数量
version_conflicts:重建索引中版本冲突的数量
failures:所有索引失败的数组。如果这是非空的,则请求将被中止
由于_reindex是源索引的快照,而且目标索引是不同的索引,所以基本不可能产生冲突。在接口参数中可以增加dest来进行乐观并发控制。如果version_type设置为internal会导致es盲目转储文件到目标索引,任何具有相同类型和ID的文档将被重写。例如
请求:POST http://127.0.0.1:9200/_reindex
{
"source":{"index":"secisland"},
"dest":{"index":"new_secisland","version_type":"internal"}
}
如果设置version_type为external将会导致elasticsearch 保护源索引的版本,如果在目标索引中有一个比源索引就的版本,则会更新文档。源文件中丢失的文档也会在目标中创建
请求:
POST http://127.0.0.1:9200/_reindex
{
"source":{"index":"secisland"},
"dest":{"index":"new_secisland","version_type":"external"}
}
设置op_type为create将导致_reindex在目标索引中仅创建丢失的文件,所有现有的文件将导致版本冲突。
请求:
POST http://127.0.0.1:9200/_reindex
{
"source":{"index":"secisland"},
"dest":{"index":"new_secisland","op_type":"create"}
}
设置"conflicts":"proceed"可使_reindex发生冲突也可执行下去
请求:
POST http://127.0.0.1:9200/_reindex
{
"conflicts":"proceed"
"source":{"index":"secisland"},
"dest":{"index":"new_secisland","version_type":"external"}
}
在源添加一个类型或者一个查询可以限制文档的数量,
例如只复制用户名为kimchy的文档:
请求:
请求:
POST http://127.0.0.1:9200/_reindex
{
"source":{
"index":"secisland",
"type":"secilog",
"query":{"term":{"user":"kimchy"}
}
},
"dest":{"index":"new_secisland"}
}
在请求接口中可列出多个源索引和类型
请求:
请求:
POST http://127.0.0.1:9200/_reindex
{
"size":1 //可通过设置数量来限制处理文档的数量
"source":{"index":["secisland","blog"],"type":["secilog","post"],"sort":{"date":"desc"} //排序},
"dest":{"index":"all_together"}
}
_reindex可以使用脚本来修改文档,可修改的字段包括_id,_type,_index,_version,_routing,_parent,_timestamp,_ttl
请求:
POST http://127.0.0.1:9200/_reindex
{
"source":{"index":"secisland"},
"dest":{"index":"new_secisland","version_type":"external"}
"script":{
"internal":"if(ctx._source.foo == 'bar') {ctx.version++;ctx._source.remove('foo')}"
}
}
_reindex可以设置文档路由来进行路由保存,可以设置dest参数来进行路由设置:
keep:设置在匹配的每一个路由上发送请求的路由。这个是默认设置
discard:为每一个匹配发送的请求设置为空
=设置为每一个匹配文本请求发送路由上的路由
URL后面可跟参数 _pretty之外,还支持_refresh,wait_for_completion,consistency,timeout
consistency 控制每次写请求必须多少份分片被响应
timeout控制每个写请求等待可用分片的时间
任务查看:
请求:
GET http://127.0.0.1:9200/_tasks/?pretty&detailed=true&actions=*reindex
可以通过_reindex来修改列的名称,例如:
请求:
POST http://127.0.0.1:9200/_reindex?pretty
{
"source":{"index":"secisland"},
"dest":{"index":"secisland2"},
"script":{"inline":"ctx._source.tag = ctx._source.remove(\"flag\")"}
}
flag字段名称被修改成了tag