in supporting-blog-content/automatically-updating-your-index-nodejs-azure/azure_index.js [18:42]
async function getLastUpdateDate() {
try {
const response = await client.search({
index: 'nasa-node-js',
body: {
size: 1,
sort: [{ close_approach_date: { order: 'desc' } }],
_source: ['close_approach_date']
}
});
if (response.body && response.body.hits && response.body.hits.hits.length > 0) {
return response.body.hits.hits[0]._source.close_approach_date;
} else {
// Default to one day ago if no records found
const today = new Date();
const lastWeek = new Date(today);
lastWeek.setDate(today.getDate() - 1);
return lastWeek.toISOString().split('T')[0];
}
} catch (error) {
console.error('Error fetching last update date from Elasticsearch:', error);
throw error;
}
}