ES 之索引别名的使用
在开发中,随着业务需求的迭代,较老的业务逻辑就要面临更新甚至是重构,而对于 ES 来说,为了适应新的业务逻辑,可能就要对原有的索引做一些修改,比如对某些字段做调整,甚至是重建索引。而做这些操作的时候,可能会对业务造成影响,甚至是停机调整等问题。由此,ES 提供了索引别名来解决这些问题。 索引别名就像一个快捷方式或是软连接,可以指向一个或多个索引,也可以给任意一个需要索引名的 API 来使用。别名的应用为程序提供了极大地灵活性。
查询别名
GET:localhost:9200/nba/_alias
GET:localhost:9200/_alias
新增别名
方式一:POST:localhost:9200/_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_v1.0"
}
}
]
}
方式二:PUT:localhost:9200/nba/_alias/nba_v1.1
删除别名
方式一:POST:localhost:9200/_aliases
{
"actions": [
{
"remove": {
"index": "nba",
"alias": "nba_v1.0"
}
}
]
}
方式二:DELETE:localhost:9200/nba/_alias/nba_v1.1
重命名
POST:localhost:9200/_aliases
{
"actions": [
{
"remove": {
"index": "nba",
"alias": "nba_v1.0"
}
},
{
"add": {
"index": "nba",
"alias": "nba_v2.0"
}
}
]
}
为多个索引指定一个别名
POST:localhost:9200/_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "national_player"
}
},
{
"add": {
"index": "wnba",
"alias": "national_player"
}
}
]
}
为同个索引指定多个别名
POST:localhost:9200/_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_v2.1"
}
},
{
"add": {
"index": "nba",
"alias": "nba_v2.2"
}
}
]
}
通过别名读索引
当别名指定了一个索引,则查出一个索引
GET:localhost:9200/nba_v2.1
当别名指定了多个索引,则查出多个索引
GET:localhost:9200/national_player
通过别名写索引
当别名指定了一个索引,则可以做写的操作
POST:localhost:9200/nba_v2.1/_doc/566
{
"countryEn": "Croatia",
"teamName": "快船",
"birthDay": 858661200000,
"country": "克罗地亚",
"teamCityEn": "LA",
"code": "ivica_zubac",
"displayAffiliation": "Croatia",
"displayName": "伊维察 祖巴茨哥哥",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "108.9 公斤",
"teamCity": "洛杉矶",
"playYear": 3,
"jerseyNo": "40",
"teamNameEn": "Clippers",
"draft": 2016,
"displayNameEn": "Ivica Zubac",
"heightValue": 2.16,
"birthDayStr": "1997-03-18",
"position": "中锋",
"age": 22,
"playerId": "1627826"
}
当别名指定了多个索引,可以指定写某个索引
POST:localhost:9200/_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "national_player",
"is_write_index": true
}
},
{
"add": {
"index": "wnba",
"alias": "national_player"
}
}
]
}
POST /national_player/_doc/566
{
"countryEn": "Croatia",
"teamName": "快船",
"birthDay": 858661200000,
"country": "克罗地亚",
"teamCityEn": "LA",
"code": "ivica_zubac",
"displayAffiliation": "Croatia",
"displayName": "伊维察 祖巴茨妹妹",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "108.9 公斤",
"teamCity": "洛洛杉矶",
"playYear": 3,
"jerseyNo": "40",
"teamNameEn": "Clippers",
"draft": 2016,
"displayNameEn": "Ivica Zubac",
"heightValue": 2.16,
"birthDayStr": "1997-03-18",
"position": "中锋",
"age": 22,
"playerId": "1627826"
}
ES 之如何重建索引
Elastic Search 是一个实时的分布式搜索引擎,为用户提供搜索服务,当我们决定存储某种数据时,在创建索引的时候需要将数据结构完整确定下来,与此同时索引的设定和很多固定配置将不能改变。当需要改变数据结构时,就需要重新建立索引,为此,Elastic 团队提供了很多辅助工具帮助开发人员进行重建索引。
步骤
- nba 取一个别名 nba_latest, nba_latest 作为对外使用
- 新增一个索引 nba_20220101,结构复制于 nba 索引,根据业务要求修改字段
- 将 nba 数据同步到 nba_20220101
- 给 nba_20220101 添加别名 nba_latest,删除 nba 别名 nba_latest
- 删除 nba 索引
我们对外提供访问 nba 索引时使用的是 nba_latest 别名
新增一个索引(比如修改字段类型,jerseyNo 改成 keyword 类型)
PUT:localhost:9200/nba_20220101
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"code": {
"type": "text"
},
"country": {
"type": "keyword"
},
"countryEn": {
"type": "keyword"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "keyword"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
PUT:localhost:9200/nba_20220101
将旧索引数据 copy 到新索引
同步等待,接口将会在 reindex 结束后返回
POST:localhost:9200/_reindex
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20220101"
}
}
异步执行,如果 reindex 时间过长,建议加上 wait_for_completion=false 的参数条件,这样 reindex 将直接返回 taskId
POST:localhost:9200/_reindex?wait_for_completion=false
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20220101"
}
}
替换别名
POST:localhost:9200/_aliases
{
"actions": [
{
"add": {
"index": "nba_20220101",
"alias": "nba_latest"
}
},
{
"remove": {
"index": "nba",
"alias": "nba_latest"
}
}
]
}
删除旧索引
DELETE:localhost:9200/nba
通过别名访问新索引
POST:localhost:9200/nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
}
}
ES 之 refresh 操作
新的数据一添加到索引中立马就能搜索到,但是真实情况不是这样的。
我们使用链式命令请求,先添加一个文档,再立刻搜索,会发现搜索不到
curl -X PUT localhost:9200/star/_doc/888 -H 'Content-Type:application/json' -d '{ "displayName": "蔡徐坤" }'
curl -X GET localhost:9200/star/_doc/_search?pretty
方法一:使用强制刷新
curl -X PUT localhost:9200/star/_doc/666?refresh -H 'Content-Type:application/json' -d '{ "displayName": "杨超越" }'
curl -X GET localhost:9200/star/_doc/_search?pretty
方法二:修改默认更更新时间(默认时间是1s)
PUT:localhost:9200/star/_settings
{
"index": {
"refresh_interval": "5s"
}
}
将 refresh 关闭
PUT:localhost:9200/star/_settings
{
"index": {
"refresh_interval": "-1"
}
}
ES 之高亮查询
如果返回的结果集中很多符合条件的结果,那怎么能一眼就能看到我们想要的那个结果呢?
高亮查询
POST:localhost:9200/nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
},
"highlight": {
"fields": {
"displayNameEn": {}
}
}
}
自定义高亮查询
POST:localhost:9200/nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
},
"highlight": {
"fields": {
"displayNameEn": {
"pre_tags": [
"<h1>"
],
"post_tags": [
"</h1>"
]
}
}
}
}
ES 之查询建议
查询建议,是为了给用户提供更好的搜索体验。包括:词条检查,自动补全。
Suggester
- Term suggester
- Phrase suggester
- Completion suggester
字段
Term suggester
term 词条建议器,对给输入的文本进行分词,为每个分词提供词项建议
POST:localhost:9200/nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "jamse hardne",
"term": {
"suggest_mode": "missing",
"field": "displayNameEn"
}
}
}
}
Phrase suggester
phrase 短语建议,在 term 的基础上,会考量多个 term 之间的关系,比如是否同时出现在索引的原文里,相邻程度,以及词频等
POST:localhost:9200/nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "jamse harden",
"phrase": {
"field": "displayNameEn"
}
}
}
}
Completion suggester
Completion 完成建议
POST:localhost:9200/nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "Miam",
"completion": {
"field": "teamCityEn"
}
}
}
}