Elastic Search 的高級應用


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 團隊提供了很多輔助工具幫助開發人員進行重建索引。

步驟

  1. nba 取一個別名 nba_latest, nba_latest 作為對外使用
  2. 新增一個索引 nba_20220101,結構復制於 nba 索引,根據業務要求修改字段
  3. 將 nba 數據同步到 nba_20220101
  4. 給 nba_20220101 添加別名 nba_latest,刪除 nba 別名 nba_latest
  5. 刪除 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"
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM