我正在尝试创建一个带有rest的node.js应用程序,用于查询elastic search应用程序云上的数据。我有以下elasticsearch连接的代码
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host:'localhost:9200'
});
上面的连接连接正常,如果我添加任何数据,它也会被添加。但是我不希望数据被添加到localhost中。我希望我的实际集群拥有数据..我尝试了下面的代码
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在互联网上我只找到本地主机的例子..请问nodejs如何直接接入弹性搜索云集群??
发布于 2021-05-16 12:11:29
我也有同样的问题。因此,连接到您的企业搜索主机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>"
您还可以通过以主机urls数组的形式提供urls,一次连接到多个主机。
发布于 2021-04-15 15:10:05
请确保使用弹性云UI提供的云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
https://stackoverflow.com/questions/67105876
复制相似问题