跳轉到

Expanding search options diversity using OpenSearch

前情提要

隨著查詢範圍的擴大和資料量的增長,以往使用完全相符以...開頭以...結尾 又或者包含...關鍵字等做法已經無法精確且有效率地的找出期望的結果。因此,建立一個現代化的搜尋解決方案勢在必行。

經過內部討論後,我們決定採用 OpenSearch 實作,主要原因如下:

  • 開源:自從 2021 年 Elasticsearch 變更軟體授權策略,亞馬遜雲端服務決定ElasticsearchKibana 最後一個 ALv2 版本建立和維護一個分支。
  • 支援多元的搜尋功能:如 K-Nearest Neighbor (KNN) 搜尋、SQL、異常偵測、機器學習共用、追蹤分析、全文搜尋等。

安裝 OpenSearch

解壓縮從官網下載下來的 .zip 檔後,打開 Powershell:

# 移動至目標資料夾
cd \path\to\opensearch

# OpenSearch 2.12 之後的版本需要指定預設的管理員密碼
set OPENSEARCH_INITIAL_ADMIN_PASSWORD=<custom-admin-password>
# 或使用
$Env:OPENSEARCH_INITIAL_ADMIN_PASSWORD="<custom-admin-password>"        

# 執行安裝檔
.\opensearch-windows-install.bat

# 測試有沒有裝成功
curl.exe -X GET https://localhost:9200 -u "admin:<custom-admin-password>" --insecure

設定環境並註冊服務

  1. 打開 opensearch\config 內的 opensearch.yml 並加上以下內容

    # Bind OpenSearch to the correct network interface. Use 0.0.0.0
    # to include all available interfaces or specify an IP address
    # assigned to a specific interface.
    network.host: 0.0.0.0
    
    # Unless you have already configured a cluster, you should set
    # discovery.type to single-node, or the bootstrap checks will
    # fail when you try to start the service.
    discovery.type: single-node
    
    # If you previously disabled the Security plugin in opensearch.yml,
    # be sure to re-enable it. Otherwise you can skip this setting.
    plugins.security.disabled: false
    

  2. 打開同樣在 opensearch\config 內的 jvm.options 根據電腦資訊調整(這邊以 16GB RAM 的配置為例)

    -Xms8g
    -Xmx8g
    

  3. 編輯系統環境變數,建立一個名稱為 JAVA_HOME,值為 \path\to\opensearch\jdk 的系統/使用者變數(如果系統原本有設定相容的 jdk 在 JAVA_HOME 變數可以沿用)

  4. 註冊 OpenSearch 為 Windows 服務

    .\bin\opensearch-service.bat install
    
    你會看到
    The service 'opensearch-service-x64' has been installed.
    

  5. 啟動服務

    .\bin\opensearch-service.bat start
    
    然後到服務設定啟動類型為自動

  6. 在瀏覽器輸入以下網址檢測是否安裝成功

    https://localhost:9200/_cat/indices?v