Created
December 27, 2019 05:15
-
-
Save svsh227/6dec57c314a8c538a6716f628fc2d095 to your computer and use it in GitHub Desktop.
Integrate Elasticsearch With Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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