Why, When and How to start with Elasticsearch

npm install -save elasticsearch
const elasticsearch = require("elasticsearch")
const esClient = new elasticsearch.Client({
host: '127.0.0.1:9200',
log: 'error'
})

esClient.ping({
requestTimeout: 30000, // 30 seconds
}, (err) => {
if (err)
console.error(`Error connecting to the es client: ${err}`)
else
console.log(`Success! ElasticSearch cluster is up!`)
})
esClient.indices.create({
index: "indexName"
}).then((resp) => {
console.log(`Index Created with response: ${resp}`)
}, err => {
console.log(`Error creating Index: ${err}`)
})
esClient.indices.exists({
index: "indexName"
}).then((resp) => {
console.log(resp)
}, err => {
console.log(`Index does not exist: ${err}`)
})
esClient.indices.putMapping({
index: "indexName",
type: "typeName",
body: {
"properties": {
"property1": {
"type": "text",
},
"property2": {
"type": "date"
},
"property3": {
"type": "keyword",
},
}
}
}).then((resp) => {
console.log(resp)
}, err => {
console.log(`Error mapping existing index: ${err}`)
})
esClient.index({
index: "indexName",
type: "typeName",
id: "_id",
body: {
"property1": "value1",
"property2": "value2",
"property3": "value3",
}
}).then((resp) => {
console.log(resp)
}, err => {
console.log(`Error indexing document: ${err}`)
})
esClient.update({
index: "indexName",
type: "typeName",
id: "_id",
body: {
"property2": "newValue2",
"property3": "newValue3",
}
}).then((resp) => {
console.log(resp)
}, err => {
console.log(`Error updating document: ${err}`)
})
esClient.search({
index: "indexName",
type: "typeName",
from: 0,
size: 100,
body: {
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"term": {
"property1": "value1"
}
},
{
"terms": {
"property2": [
"possibleValue1",
"possibleValue2",
"possibleValue3"
]
}
},
],
"should": [
{
"term": {
"property1": "falseValue"
}
},
{
"term": {
"property3": "value3"
}
},
]
}
}
}
},
"sort": [
{ "property2": { "order": "desc" } }
],
}
}).then((resp) => {
console.log(resp)
}, (err) => {
console.log(`Error getting query results: ${err}`)
})
esClient.delete({
index: "indexName",
type: "typeName",
id: "_id",
}).then((resp) => {
console.log(resp)
}, err => {
console.log(`Error deleting document: ${err}`)
})

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store