Elastic search and nodejs connection

3.3k views Asked by At

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??

2

There are 2 answers

1
AudioBubble On BEST ANSWER

I had same issue. So connecting to host url of your enterprise search will work. Like this


var client = new elasticsearch.Client({
// do not forget to add the username and password in the url
  host:"https://<username>:<password>[complete_host_url]" 
}); 

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

2
Ricardo Ferreira On

Make sure to use the Cloud ID provided by the Elastic Cloud UI:

enter image description here

And then use the credentials created when you created the deployment. Alternatively, you can also create API keys to authenticate:

const { Client } = require('@elastic/elasticsearch')

const client = new Client({
  cloud: {
    id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA==',
  },
  auth: {
    apiKey: {
      id: 'foo',
      api_key: 'bar'
    }
  }
})

More information here: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-connecting.html