Skip to content

Instantly share code, notes, and snippets.

@svsh227
Created December 27, 2019 05:15
Show Gist options
  • Save svsh227/6dec57c314a8c538a6716f628fc2d095 to your computer and use it in GitHub Desktop.
Save svsh227/6dec57c314a8c538a6716f628fc2d095 to your computer and use it in GitHub Desktop.
Integrate Elasticsearch With Node.js
//searchData.js
const elasticsearch = require('elasticsearch');
const indexName = 'demo_elastic_index';
const query = 'Lewisham';
const searchData = async () => {
const client = new elasticsearch.Client({
host: 'localhost:9200',
// log: 'trace',
});
await client.ping({
requestTimeout: 3000
}, function (error) {
if (error) {
console.trace('elasticsearch cluster is down!');
} else {
console.log('Elastic search is running.');
}
});
try {
const resp = await client.search({
index: indexName,
type: 'place',
body: {
sort: [
{
place_rank_num: { order: 'desc' },
},
{
importance_num: { order: 'desc' },
},
],
query: {
bool: {
should: [{
match: {
lat: '51.4624325',
}
},{
match: {
alternative_names: query,
},
}]
},
},
},
});
const { hits } = resp.hits;
console.log(hits);
} catch (e) {
// console.log("Error in deleteing index",e);
if (e.status === 404) {
console.log('Index Not Found');
} else {
throw e;
}
}
}
searchData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment