設定 & 連線到 Elasticsearch
連線到 Elasticsearch 最簡單的方式是使用 Elastic Cloud。您可以註冊免費試用帳戶並立即開始使用。
使用 Docker
如果您使用 Docker,可以使用下列指令執行 Elasticsearch
提取 Elasticsearch Docker 映像
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.6.2
為 Elastic 建立 Docker 網路
docker network create elastic
啟動 Elasticsearch
docker run --name elasticsearch --net elastic -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "xpack.security.enabled=false" -e http.cors.enabled=true -e "http.cors.allow-origin='*'" -e http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization -e http.cors.allow-credentials=true -e network.publish_host=localhost -e xpack.security.enabled=false docker.elastic.co/elasticsearch/elasticsearch:8.6.2
我們在此範例中停用了安全性,但您可以在官方文件中閱讀更多關於保護 Elasticsearch 安全 (在新分頁中開啟)的資訊。
您將能夠在主機 http://localhost:9200
存取 Elasticsearch。
透過 Docker 使用 Opensearch
提取 Opensearch Docker 映像
docker pull opensearchproject/opensearch:1.2.4
啟動 Opensearch
docker run --name opensearch --rm -d -p 9200:9200 -e http.port=9200 -e discovery.type=single-node -e http.max_content_length=10MB -e http.cors.enabled=true -e "http.cors.allow-origin='*'" -e http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization -e http.cors.allow-credentials=true opensearchproject/opensearch:1.2.4
您將能夠在主機 http://localhost:9200
存取 Opensearch。
使用 Elastic Cloud
若要連線到 Elastic Cloud,您需要建立帳戶並取得您的憑證。您可以按照Elastic Cloud 網站 (在新分頁中開啟)上的指示執行此操作。
設定部署後,請執行以下操作
- 前往Elastic Cloud 主控台 (在新分頁中開啟)。
- 按一下您要連線的部署。
- 找到雲端 ID 或 Elasticsearch URL 並複製它。
- 將 URL 貼到 Node 應用程式中組態物件內的
connection.cloud_id
或connection.host
欄位中。
使用使用者名稱:密碼連線
當您建立部署時,系統會提供您使用者名稱 & 密碼。您可以設定組態物件中的 connection.auth.username
和 connection.auth.password
欄位。
const client = Client({
connection: {
cloud_id: "ELASTICSEARCH_CLOUD_ID",
// or optionally the elasticsearch host
// host: "ELASTICSEARCH_URL",
auth: {
username: "elastic",
password: "changeme"
}
}
})
使用 API 金鑰連線
建立一個具有索引 read
權限的 API 金鑰。
您需要傳遞 Authorization
標頭,才能建立 API 金鑰。
curl --location --request POST 'http://localhost:9200/_security/api_key' \
--header 'Content-Type: application/json' \
-u 'elastic:changeme' \
--data-raw '{
"name": "Elastic Search API Key",
"role_descriptors": {
"role-a": {
"cluster": ["all"],
"index": [
{
"names": ["<index-name>"],
"privileges": ["read"]
}
]
}
}
}'
將 API 金鑰以 encoded
複製,並貼到 connection.apiKey
欄位。
const client = Client({
connection: {
host: "ELASTICSEARCH_URL",
apiKey: "API_KEY"
}
})
您也可以在 Kibana 中執行此操作,方法是前往 Stack Management > API 金鑰
並建立新的 API 金鑰。
如果 Searchkit 無法連線到 Elasticsearch,它會擲回錯誤。您應該可以在執行 Node API 的終端機中看到錯誤。
您也可以在 Node 應用程式中組態物件內開啟 debug
旗標,以查看更詳細的記錄。
使用自訂標頭連線到 Elasticsearch
如果您使用帶有 Proxy 的 Elasticsearch,您可能需要將自訂標頭新增至請求。您可以藉由在 Node 應用程式中組態物件內設定 connection.headers
欄位來執行此操作。
const client = Client({
connection: {
host: "http://localhost:9200",
headers: {
"X-My-Header": "My-Value"
}
}
})
自訂傳輸
如果以上方法都失敗,您可以建立自己的自訂傳輸方式。您可以透過實作 Transporter
介面來完成。
import { ESTransporter } from 'searchkit'
import type { SearchRequest } from "searchkit"
class MyTransporter extends ESTransporter {
async performNetworkRequest(requests: SearchRequest[]) {
// you can use any http client here
return fetch(`https://localhost:9200/_msearch`, {
headers: {
// Add custom headers here
},
body: this.createElasticsearchQueryFromRequest(requests),
method: 'POST'
})
}
}
// then pass the custom transporter to the client
const client = Client({
connection: new MyTransporter()
});