[英]Elastic search and nodejs connection
我正在嘗試使用 rest api 創建一個 node.js 應用程序,以查詢彈性搜索應用程序雲上存在的數據。 我遵循 elasticsearch 連接的代碼
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host:'localhost:9200'
});
上面的連接正確連接,如果我添加任何數據,它也會被添加..但我希望數據不被添加到本地主機..我希望我的實際集群擁有數據..我嘗試了下面的代碼
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
cloud: {
id: 'cloud_id',
},
auth: {
username: "username",
password: "pass",
},
});
//data I am trying to add
let data = {
"id": "park_somethign",
"title": "Something",
"description": "Split into the separate Rincon Mountain and Tucson Mountain districts, this park is evidence that the dry Sonoran Desert is still home to a great variety of life spanning six biotic communities. Beyond the namesake giant saguaro cacti, there are barrel cacti, chollas, and prickly pears, as well as lesser long-nosed bats, spotted owls, and javelinas.",
"nps_link": "https://www.nps.gov/sagu/index.htm",
"states": [
"Arizona"
],
"visitors": 838456,
"world_heritage_site": false,
"location": "32.20,-109.5",
"acres": 9215.72,
"square_km": 2271.2,
"date_established": "1997-10-14T05:00:00Z"
}
//this is what I am trying to do
client.index({
index: 'engines',
body: data
}).then(resp => {
return res.status(200).json({
message: "Added Data"
})
}).catch(e => {
console.log(e)
return res.status(500).json({
message: "Error",
e
})
})
上面的代碼仍然沒有添加數據或從我的雲集群中檢索數據... Evrrywhere on internet I found only localhost examples.. 請問誰能告訴我如何將nodejs直接連接到彈性搜索雲集群?
確保使用 Elastic Cloud UI 提供的 Cloud ID:
然后使用創建部署時創建的憑據。 或者,您也可以創建 API 密鑰進行身份驗證:
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
cloud: {
id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA==',
},
auth: {
apiKey: {
id: 'foo',
api_key: 'bar'
}
}
})
更多信息在這里: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-connecting.html
我有同樣的問題。 因此,連接到您的企業搜索的主機 url 將起作用。 像這樣
var client = new elasticsearch.Client({
// do not forget to add the username and password in the url
host:"https://<username>:<password>[complete_host_url]"
});
如果這不起作用,請嘗試node:"https://<username>:<password>@<ip1><port>"
您還可以通過將 url 提供為主機 url 數組來一次連接到多個主機。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.