一起来玩Elasticsearch,加我微信:wx1250134974
Elasticsearch认证复习准备
https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html
##重新索引的意义
尽管可以增加新的类型到索引中,或者增加新的字段到类型中,但是不能添加新的分析器或者对现有的字段做改动。如果你那么做的话,结果就是那些已经被索引的数据就不正确, 搜索也不能正常工作。事实上ES也不会允许你这么更改
对现有数据的这类改变最简单的办法就是重新索引:用新的设置创建新的索引并把文档从旧的索引复制到新的索引。
重新索引的两种方式:
- 从Elasticsearch v2.3.0开始, Reindex API 被引入。它能够对文档重建索引而不需要任何插件或外部工具。
#最简单的方式
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
注:将twitter的索引数据 导入到new_twitter索引中。目标中如果有相同type、id的数据将会直接覆盖。
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"version_type": "external"
}
}
注:"version_type": "external" 可以在目标索引中保留最新版本数据,而不会直接覆盖,没有的数据直接创建。这种方式更好些
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"op_type": "create"
}
}
注: "op_type": "create"只是在目标中创建,如果目标中有相同数据,直接退出,某些需求可能需要
POST _reindex
{
"conflicts": "proceed",
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"op_type": "create"
}
}
注: 如果目标中有相同数据( "op_type": "create"
会导致直接退出),加上这个"conflicts": "proceed"参数,将不在退出,对于冲突的数据会计个数,让你知道有多少是有冲突的。
POST _reindex
{
"source": {
"index": "twitter",
"type": "tweet",
"query": {
"term": {
"user": "kimchy"
}
}
},
"dest": {
"index": "new_twitter"
}
}
注:在源端加些条件,导入一部分符合自己需求的数据到目标端。类似mysql中insert into ...select ...
POST _reindex
{
"source": {
"index": ["twitter", "blog"],
"type": ["tweet", "post"]
},
"dest": {
"index": "all_together"
}
}
注:源端可以指定多个索引,多个类型。然后索引到目标索引中。这里有个注意的点,源端如果有type、 id相同的数据,只会保留一份,另一份就丢了。
POST _reindex
{
"size": 1,
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
注: "size": 1考备一条数据到目标索引中
POST _reindex
{
"size": 10000,
"source": {
"index": "twitter",
"sort": { "date": "desc" }
},
"dest": {
"index": "new_twitter"
}
}
注:"size": 10000 拷贝10000条数据,"sort": { "date": "desc" }按照date降序排好序后的10000条。 这种方式会降低reindex的速度。
POST _reindex
{
"source": {
"index": "twitter",
"_source": ["user", "tweet"]
},
"dest": {
"index": "new_twitter"
}
}
注: "_source": ["user", "tweet"] 只考部分字段进去
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"version_type": "external"
},
"script": {
"source": "if (ctx._source.foo == 'bar') {ctx._version++; ctx._source.remove('foo')}",
"lang": "painless"
}
}
注:可以用脚本改字段的名字,这样目标索引中的字段名字就用你更改后的。还可以通过定义ctx.op的值(ctx.op = "noop"表示不索引这个文档、ctx.op = "delete"表示在目标索引中删除这个文档),用脚本可以改元数据,因此这里注意,如果你八version设置成NULL或者去掉,目标索引中的数据将直接被覆盖。还有一个有意思的功能,就是你可以定义routing属性来达到在目标索引中路由到指定分片的功能。
POST _reindex
{
"source": {
"index": "source",
"query": {
"match": {
"company": "cat"
}
}
},
"dest": {
"index": "dest",
"routing": "=cat"
}
}
注:定义routing可以路由到目标索引中的某个分片中
POST _reindex
{
"source": {
"remote": {
"host": "http://otherhost:9200",
"username": "user",
"password": "pass"
},
"index": "source",
"query": {
"match": {
"test": "data"
}
}
},
"dest": {
"index": "dest"
}
}
注:可以从远程集群直接导数据到当前集群中的索引里边(直接在目标端执行这个语句就可以),注意这里username、password是可选的,而且用的时候最好用https的协议,防止被别人窃取密码。
注:远程的机器必须明确地在elasticsearch.yaml中的reindex.remote.whitelist属性上定义好,格式为(otherhost:9200, another:9200, 127.0.10.*:9200, localhost:*),并且从远程重新索引这个操作,不支持slices(多任务)复制。
POST _reindex
{
"source": {
"remote": {
"host": "http://otherhost:9200",
"socket_timeout": "1m",
"connect_timeout": "10s"
},
"index": "source",
"query": {
"match": {
"test": "data"
}
}
},
"dest": {
"index": "dest"
}
}
注: "socket_timeout": "1m", "connect_timeout": "10s" 读取超时时间和连接超时时间,这个有时候会有用
POST _reindex?slices=10&refresh&requests_per_second=100
{
"source": {
"index": "commands"
},
"dest": {
"index": "new_commands",
"version_type": "external"
}
}
注:requests_per_second=100(表示每秒限制发起的批量的操作的次数),这个可以限速,防止负载过高。限速的实现原理就是在多个批量操作之间sleep一段时间,计算方法就是(batch size(默认1000 ) 除以 requests_per_second 减去 spent writing(默认0.5 ).),这个时间也是scroll的等待超时时间。
其它的参数像wait_for_completion=false(后台执行,不用等程序结束)、wait_for_active_shards(对于数据安全、一致性要求高的需要)都是支持的。
GET _tasks?detailed=true&actions=*reindex
GET /_tasks/taskId:12204154
注:查看reindex任务情况
POST _tasks/task_id:12204154/_cancel
注:取消任务
POST _reindex/task_id:1/_rethrottle?requests_per_second=-1
注:重新限速,可以取消也可以更新限速,而且是动态更改生效,不过降速的设置会在下个批次生效。
POST _reindex
{
"source": {
"index": "test"
},
"dest": {
"index": "test2"
},
"script": {
"source": "ctx._source.tag = ctx._source.remove(\"flag\")"
}
}
注: "source": "ctx._source.tag = ctx._source.remove(\"flag\")"直接将字段名字由flag改成tag。
POST _reindex?slices=5&refresh
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
注:使用slices方式,多任务并发的将源端数据拷贝到目标端。速度快的很,但是slices别太多,量力而行。一般机器好的话,设置成主分片个数的倍数最好,安全点设置成主分片数就可以了。
POST new_twitter/_search?size=0&filter_path=hits.total
注:验看目标索引中的数据量,查看重新索引的进度
POST _reindex
{
"size": 10,
"source": {
"index": "twitter",
"query": {
"function_score" : {
"query" : { "match_all": {} },
"random_score" : {}
}
},
"sort": "_score"
},
"dest": {
"index": "random_twitter"
}
}
注:随机的复制源端数据到目标里边。数量10条。一般测试使用
其它的参看下方官方链接,上方的也都是翻译过来的
- 用 scroll 从旧的索引检索批量文档 , 然后用 bulk API 把文档推送到新的索引中。
#不怎么常用了,毕竟有第一种封装好的,如果实在用不了上边的接口,自己动手写吧,参看下方链接
https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-request-scroll.html#sliced-scroll
一起来玩Elasticsearch,加我微信:wx1250134974