美文网首页
Elasticsearch 查询模版、reindex、index

Elasticsearch 查询模版、reindex、index

作者: dylan丶QAQ | 来源:发表于2020-10-22 19:39 被阅读0次

起因:在项目开发过程中,要使用到搜索 引擎来对一些关键字实现逆向查询,如果仅用模糊搜索,那么搜索的时间会根据数据量的增大而增大,对比之下就学了elasticsearch,也记录一下,常常回顾。


1. search template使用

什么是查询模版?

对于ES来说,你可以把它理解为一个NoSQL的容器,应用来访问和调用的过程,对于搜索引擎来讲,你的所有业务搜索场景是不是都是相对明确的?

能不能做到ES的业务处理,由ES来做,前端不关心ES的json的语法格式来做到搜索引擎和前端解耦

# 模版结构可以应用到所有的索引上
# 所以创建的时候不加索引,tpnick是模版名,可以自定义
POST /_scripts/tpnick
{
 "script": {
 "lang": "mustache",
 "source": {
 "query": {
 "match": {
 "nickname": {
 "query": "{{nick_value}}",
 "analyzer": "{{nick_analyzer}}"
 }
 }
 }
 }
 }
}
# 获得模版
GET /_scripts/tpnick
# 删除模版
DELETE /_scripts/tpnick
# 调用模版进行查询
GET /index_customer/_search/template
{
 "id": "tpnick",
 "params": {
 "nick_value": "太阳",
 "nick_analyzer": "ik_max_word"
 }
}

2. field增加分词器update by query

# 场景1: 一个field在使用了一段时间后,发现需要增加新的分词器,比如原来是中文,后面要加pinyin
POST /index_customer/_mapping
{
 "properties": {
 "nickname": {
 "type": "text",
 "analyzer": "ik_max_word",
 "fields": {
 "pinyin": {
 "type": "text",
 "analyzer": "pinyin"
 }
 }
 }
 }
}
# 添加分词器后就进行搜索,是搜不到数据的
# 分词器在数据加入后增加的,对前面的数据无法进行分词搜索
GET /index_customer/_search
{
 "query": {
 "match": {
 "nickname.pinyin": "taiyang"
 }
 }
}
# 就要对现有index进行自更新
POST /index_customer/_update_by_query
# 场景2:中文分词器里面,会在数据已经导入后加入新的分词,这个时候这个分词就搜不到以前的数据
# 不是ik分词器的问题,是因为你没有更新索引
​
# 这里需要注意的点
# ES索引中的数据,是在导入是就进行了分词,而不是在查询的时候才进行分词

3. reindex&index alias功能分析使用

3.1. reindex

场景描述**

  • 我们一个index在创建好mapping后不能对type进行修改,原来是keyword我想改成text?

  • 比如我们的存储不够了,加入了新的机器进入集群,主分片我想增加怎么办?

# 这个时候就需要使用reindex
# 相当于索引复制的概念
# 如果你的新索引没有手工创建mapping,那么ES会根据数据来自动生成mapping
POST /_reindex
{
 "source": {
 "index": "index_customer"
 },
 "dest": {
 "index": "index_test"
 }
}
# 如果新的index里本来就有数据,希望设定为没有才写,有的话版本冲突
POST /_reindex
{
 "source": {
 "index": "index_customer"
 },
 "dest": {
 "index": "index_test",
 "op_type": "create"
 }
}
# 如果跨集群进行索引复制
# 将索引从集群A复制到集群B,这个要在B集群上执行
# 要给soure设置白名单,在B集群的elasticsearch.yml文件里
# reindex.remote.whitelist: "192.168.0.100:9200,192.168.0.101:9200"
POST /_reindex
{
 "source": {
 "remote": {
 "host": "http://192.168.0.100:9200"
 },
 "index": "index_customer",
 "query": {
 "match": {
 "desc": "艾编程"
 }
 },
 "size": 100
 },
 "dest": {
 "index": "index_test1",
 "op_type": "create"
 }
}
​
# reindex还可以支持异步操作
POST /_reindex?wait_for_completion=false
# 会返回一个taskId

3.2. index alias

场景描述

  • 如果在ES中会横向以时间为维度创建很多索引,比如index_0201,index_0202

  • 这个时候前端应用不能因为ES名字改了就修改索引名,这个时候就可以通过别名来统一

  • 一个别名节点可以关联多个索引

  • 别名也不能和现有索引重复,如果别名重复,数据就合并到一起了

# 添加别名
POST /_aliases
{
 "actions": [
 {
 "add": {
 "index": "index_customer",
 "alias": "index_user",
 "filter": {
 "range": {
 "consume": {
 "gte": 2000
 }
 }
 }
 }
 }
 ]
}
# 同时创建多个别名,相当于做个了一个逻辑合并,可以支持分词和相关搜索
{
 "actions": [
 {
 "add": {
 "index": "index_test",
 "alias": "index_user"
 }
 },
 {
 "add": {
 "index": "index_test1",
 "alias": "index_user"
 }
 }
 ]
}
# 删除别名
{
 "actions": [
 {
 "remove": {
 "index": "index_test",
 "alias": "index_user"
 }
 },
 {
 "remove": {
 "index": "index_test1",
 "alias": "index_user"
 }
 }
 ]
}

不要以为每天把功能完成了就行了,这种思想是要不得的,互勉~!

相关文章

网友评论

      本文标题:Elasticsearch 查询模版、reindex、index

      本文链接:https://www.haomeiwen.com/subject/kqvwpktx.html