Skip to main content

Elasticsearch

Elasticsearch is the world's leading free and open search and analytics solution.

Official Websitehttps://www.elastic.co/
TagsDatabaseSQL
Network Connection needs

This integration needs network access to the server where the service is running.

See the Network access page for details about how to achieve that.

Credential configuration

Fill in the appropiate values for your connection, depending on whether you are using https://cloud.elastic.co/ or your own installation.

In the extra options field, you can provide any of the parameters found here.

Here is an example of a filled credential configuration form in YepCode:

Elasticsearch snippets available in editor

note

The title is the triggering text for YepCode to autocomplete the script.

Integration

New integration from credential
const elasticsearch = yepcode.integration.elasticsearch('credential-slug')
New integration from plain authentication data
const { Client } = require('@elastic/elasticsearch')

const elasticsearch = new Client({
cloud: { id: '<cloud-id>' },
auth: { apiKey: 'base64EncodedKey' }
})

Add Data to an Index

Add data to an index
cosnt document = {
character: 'Ned Stark',
quote: 'Winter is coming.'
}

await elasticsearch.index({
index: 'game-of-thrones',
body: document
});

Execute Query

Execute query
  const result = await elasticsearch.sql.query({
query: "SELECT * FROM \"game-of-thrones\" WHERE house='stark'"
})

console.log(result)

const data = result.rows.map(row => {
const obj = {}
for (let i = 0; i < row.length; i++) {
obj[result.columns[i].name] = row[i]
}
return obj
})

console.log(data)
Execute get
  const document = await elasticsearch.get({
index: 'game-of-thrones',
id: '1'
})

console.log(document)
Execute query (stream)
  const result = await elasticsearch.search({
index: 'game-of-thrones',
query: {
match: {
quote: 'winter'
}
}
}, {
asStream: true
})

// stream async iteration, available in Node.js ≥ 10
let payload = ''
body.setEncoding('utf8')
for await (const chunk of result) {
payload += chunk
}
console.log(JSON.parse(payload))