1.es索引的别名使用
GET /nba/_alias
GET /_alias
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_v1.0"
}
}
]
}
POST /_aliases
{
"actions": [
{
"remove": {
"index": "nba",
"alias": "nba_v1.0"
}
}
]
}
DELETE /nba/_alias/nba_v1.1
POST /_aliases
{
"actions": [
{
"remove": {
"index": "nba",
"alias": "nba_v1.0"
}
},
{
"add": {
"index": "nba",
"alias": "nba_v2.0"
}
}
]
}
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "national_player"
}
},
{
"add": {
"index": "wnba",
"alias": "national_player"
}
}
]
}
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_v2.1"
}
},
{
"add": {
"index": "nba",
"alias": "nba_v2.2"
}
}
]
}
当别名指定了⼀个索引,则查出⼀个索引
GET /nba_v2.1
当别名指定了多个索引,则查出多个索引
GET /national_player
当别名指定了⼀个索引,则可以做写的操作
POST /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 /_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"
}
2.es之如何重建索引
1.我们对外提供访问nba索引时使⽤的是nba_latest别名
2.新增⼀个索引(⽐如修改字段类型,jerseyNo改成keyword类型)
PUT /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"
}
}
}
}
3.将旧索引数据copy到新索引
3.1 同步等待,接⼝将会在 reindex 结束后返回
POST /_reindex
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20220101"
}
}
3.2异步执⾏,如果 reindex 时间过⻓,建议加上 wait_for_completion=false 的参数条件,
这样 reindex 将直接返回 taskId
POST /_reindex?wait_for_completion=false
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20220101"
}
}
4.替换别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba_20220101",
"alias": "nba_latest"
}
},
{
"remove": {
"index": "nba",
"alias": "nba_latest"
}
}
]
}
5.删除旧索引
DELETE /nba
6.通过别名访问新索引
POST /nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
}
}
3.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
PUT /star/_settings
{
"index": {
"refresh_interval": "5s"
}
}
PUT /star/_settings
{
"index": {
"refresh_interval": "-1"
}
}
4.es之⾼亮查询
POST /nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
},
"highlight": {
"fields": {
"displayNameEn": { }
}
}
}
POST /nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
},
"highlight": {
"fields": {
"displayNameEn": {
"pre_tags": [
""
],
"post_tags": [
"
"
]
}
}
}
}
5.es查询建议
POST /nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "jamse hardne",
"term": {
"suggest_mode": "missing",
"field": "displayNameEn"
}
}
}
}
POST /nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "jamse harden",
"phrase": {
"field": "displayNameEn"
}
}
}
}
POST /nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "Miam",
"completion": {
"field": "teamCityEn"
}
}
}
}