Elasticsearch
Elasticsearch is the world's leading free and open search and analytics solution.
Official Websitehttps://www.elastic.co/
TagsDatabaseSQL
- JavaScript
- Python
NodeJS packagehttps://www.npmjs.com/package/@elastic/elasticsearch
Version8.7.0
Source Codehttps://github.com/elastic/elasticsearch-js
Pypi packagehttps://pypi.org/project/elasticsearch/
Version8.12.0
Source Codehttps://github.com/elastic/elasticsearch-py
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.
- JavaScript
- Python
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))
Integration
New integration from credential
elasticsearch = yepcode.integration.elasticsearch('credential-slug')
New integration from plain authentication data
from elasticsearch import Elasticsearch
# Use the params you need for your use case
elasticsearch = Elasticsearch(
cloud_id="your-cloud-id",
# hosts="https://localhost:9200",
# basic_auth=("username", "password"),
api_key="base64EncodedKey",
# bearer_auth="bearerToken",
# Other extra options
)
Add Data to an Index
Add data to an index
doc = {
"character": "Ned Stark",
"quote": "Winter is coming."
}
elasticsearch.index(index="index-name", document=doc)
Search in Index
Search in index
query = {"match": {"someKey": "someValue"}}
result = elasticsearch.search(index="index-name", query=query)
for hit in result['hits']['hits']:
print(hit)
Execute SQL Query
Execute SQL query
result = elasticsearch.sql.query(query= "SELECT * FROM \"search-api-index\" WHERE key='value'")
for column in result['columns']:
print(column)