I am trying to create a node.js application with rest apis to query data present on elastic search app cloud. I have following the code for elasticsearch connection
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host:'localhost:9200'
});
The above connection connects properly and if i add any data it gets added too.. But i want the data to not been added to localhost.. I want my actual cluster to have the data .. I tried below code
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
})
})
The above code still doesn't add the data or retrive it from my cloud cluster... Evrrywhere on internet I found only localhost examples.. Please can anyone tell me how can i connect nodejs directly to the elastic search cloud cluster??
I had same issue. So connecting to host url of your enterprise search will work. Like this
If that doesn't work then try
node:"https://<username>:<password>@<ip1><port>"
You can also connect to multiple hosts at a time by providing the urls as array of host urls.
Refer This