0

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

  • The connection params look OK. What code do you use to **ingest** your data? – Joe - GMapsBook.com Apr 15 '21 at 11:18
  • 2
    I have edited the code... u can check once and tell me if I am going wrong somewhere? –  Apr 17 '21 at 10:06
  • 2
    ok so i tried the above codes now.. it adds data and also i am able to retrive it.. but idk why it's not being stored on my cloud cluster still... the documents on elastic search cluster are sill the same... can you please let me know why it's not been added to the cloud cluster? –  Apr 17 '21 at 11:09
  • 2
    I have found this link https://github.com/elastic/app-search-node... but still i get error saying unauthorised... and asks me to enter my credentials.. Idk where I have to enter my credentials.. I gave correct Api_key too –  Apr 19 '21 at 17:54

2 Answers2

0

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

Ricardo Ferreira
  • 1,236
  • 3
  • 6
  • 2
    Yes i checked twice... I am using correct cloud id... I have edited the code... u can check once and tell me if I am going wrong somewhere? –  Apr 17 '21 at 10:07
  • 2
    ok.. so I have created cluster on enterprise search... so how do I connect to that cluster.. instead of Kibana? –  Apr 17 '21 at 11:21
0

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
    Tq ... This worked just fine. I tried various ways, but forgot to add username password all the time. Now it worked tq. –  May 16 '21 at 12:12