snippets_js
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))