新聞中心
es之索引別名的使用
- 別名有什么?
在開發(fā)中,隨著業(yè)務需求的迭代,較?的業(yè)務邏輯就要?臨更新甚?是重構,?對于es來說,為了適應新的業(yè)務邏輯,可能就要對原有的索引做?些修改,?如對某些字段做調(diào)整,甚?是重建索引。?做這些操作的時候,可能會對業(yè)務造成影響,甚?是停機調(diào)整等問題。由此,es提供了索引別名來解決這些問題。 索引別名就像?個快捷?式或是軟連接,可以指向?個或多個索引,也可以給任意?個需要索引名的API來使?。別名的應?為程序提供了極?地靈活性。

- 查詢別名
GET /nba/_alias
GET /_alias
- 新增別名
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_v1.0"
}
}
]
}
PUT /nba/_alias/nba_v1.1
- 刪除別名
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"
}
es之如何重建索引
背景
Elasticsearch是?個實時的分布式搜索引擎,為?戶提供搜索服務,當我們決定存儲某種數(shù)據(jù)時,在創(chuàng)建索引的時候需要將數(shù)據(jù)結構完整確定下來,于此同時索引的設定和很多固定配置將?不能改變。當需要改變數(shù)據(jù)結構時,就需要重新建?索引,為此,Elastic團隊提供了很多輔助?具幫助開發(fā)?員進?重建索引。
步驟
- nba取?個別名nba_latest, nba_latest作為對外使?。
- 新增?個索引nba_20220101,結構復制于nba索引,根據(jù)業(yè)務要求修改字段。
- 將nba數(shù)據(jù)同步到nba_20220101。
- 給nba_20220101添加別名nba_latest,刪除nba別名nba_latest。
- 刪除nba索引。
我們對外提供訪問nba索引時使?的是nba_latest別名
1.新增?個索引(?如修改字段類型,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"
}
}
}
}
2.將舊索引數(shù)據(jù)copy到新索引
同步等待,接?將會在 reindex 結束后返回。
POST /_reindex
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20220101"
}
}
異步執(zhí)?,如果 reindex 時間過?,建議加上 wait_for_completion=false 的參數(shù)條件,這樣 reindex 將直接返回 taskId
POST /_reindex?wait_for_completion=false
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20220101"
}
}
3.替換別名
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba_20220101",
"alias": "nba_latest"
}
},
{
"remove": {
"index": "nba",
"alias": "nba_latest"
}
}
]
}
4.刪除舊索引
DELETE /nba
5.通過別名訪問新索引
POST /nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
}
}
es之refresh操作
理想的搜索:
新的數(shù)據(jù)?添加到索引中??就能搜索到,但是真實情況不是這樣的。我們使?鏈式命令請求,先添加?個?檔,再?刻搜索。
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 /star/_settings
{
"index": {
"refresh_interval": "5s"
}
}
將refresh關閉
PUT /star/_settings
{
"index": {
"refresh_interval": "-1"
}
}
es之高亮查詢
前?
如果返回的結果集中很多符合條件的結果,那怎么能?眼就能看到我們想要的那個結果呢??如下??站所示的那樣,我們搜索 ?d課堂 ,在結果集中,將所有 ?d課堂 ?亮顯示?
高亮查詢
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": [
"
]
}
}
}
}
es之查詢建議
查詢建議是什么
- 查詢建議,是為了給?戶提供更好的搜索體驗。包括:詞條檢查,?動補全。
- 詞條檢查
- ?動補全
Suggester
- Term suggester
- Phrase suggester
- Completion suggester
字段
Term suggester
term 詞條建議器,對給輸?的?本進?分詞,為每個分詞提供詞項建議。
POST /nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "jamse hardne",
"term": {
"suggest_mode": "missing",
"field": "displayNameEn"
}
}
}
}
Phrase suggester
phrase 短語建議,在term的基礎上,會考量多個term之間的關系,?如是否同時出現(xiàn)在索引的原??,相鄰程度,以及詞頻等。
POST /nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "jamse harden",
"phrase": {
"field": "displayNameEn"
}
}
}
}
Completion suggester
Completion 完成建議。
POST /nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "Miam",
"completion": {
"field": "teamCityEn"
}
}
}
}
分享名稱:搜索引擎之ElasticSearch的高級使用
網(wǎng)站地址:http://www.dlmjj.cn/article/dpssooc.html


咨詢
建站咨詢
