Node.js के लिए Elasticsearch क्लाइंट (@elastic/elasticsearch)
लाइब्रेरी @elastic/elasticsearch Node.js वातावरण से Elasticsearch के साथ इंटरैक्ट करने के लिए एक आधिकारिक, उच्च-प्रदर्शन HTTP क्लाइंट है। यह सभी ऑपरेशनों को करने के लिए एक सुविधाजनक API प्रदान करती है: सरल दस्तावेज़ अनुक्रमण से लेकर जटिल खोज क्वेरी और एकत्रीकरण तक। लाइब्रेरी Elasticsearch 7.x और 8.x के सभी संस्करणों का पूर्ण समर्थन करती है, जिसमें क्लस्टर के साथ काम करना, प्रमाणीकरण और डेटा संपीड़न शामिल है।
स्थापना
स्थापना के लिए npm या yarn का उपयोग करें:
npm install @elastic/elasticsearch# याyarn add @elastic/elasticsearchबुनियादी कनेक्शन
Elasticsearch सर्वर का पता निर्दिष्ट करके क्लाइंट का एक इंस्टेंस बनाएं। संस्करण 8.x के लिए TLS और API-कुंजी या लॉगिन/पासवर्ड कॉन्फ़िगरेशन की आवश्यकता होती है।
const { Client } = require('@elastic/elasticsearch');
// स्थानीय सर्वर से कनेक्शन (Elasticsearch 7.x)const client = new Client({ node: 'http://localhost:9200' });
// Elastic Cloud क्लाउड सर्वर से कनेक्शन (8.x)const cloudClient = new Client({ cloud: { id: 'my-cloud-id' }, auth: { apiKey: 'base64EncodedApiKey' }});मुख्य संचालन
1. दस्तावेज़ अनुक्रमण (जोड़ना)
विधि index निर्दिष्ट इंडेक्स में एक दस्तावेज़ जोड़ती या बदलती है।
async function indexDocument() { const result = await client.index({ index: 'articles', id: '1', body: { title: 'Node.js के लिए Elasticsearch क्लाइंट', content: 'लाइब्रेरी का विस्तृत विवरण...', tags: ['elasticsearch', 'node.js'], published: true, views: 150 } }); console.log('दस्तावेज़ अनुक्रमित किया गया:', result.result);}indexDocument();2. दस्तावेज़ खोज
विधि search Query DSL का उपयोग करके पूर्ण-पाठ खोज करती है।
async function searchDocuments() { const { body } = await client.search({ index: 'articles', body: { query: { bool: { must: [ { match: { title: 'Elasticsearch' } }, { term: { published: true } } ], filter: [ { range: { views: { gte: 100 } } } ] } } } });
console.log('दस्तावेज़ मिले:', body.hits.total.value); body.hits.hits.forEach(hit => { console.log(`- ${hit._source.title} (प्रासंगिकता: ${hit._score})`); });}searchDocuments();3. ID द्वारा दस्तावेज़ प्राप्त करना
async function getDocument() { try { const { body } = await client.get({ index: 'articles', id: '1' }); console.log('दस्तावेज़:', body._source); } catch (error) { if (error.meta.statusCode === 404) { console.log('दस्तावेज़ नहीं मिला'); } else { console.error('त्रुटि:', error); } }}getDocument();4. दस्तावेज़ अद्यतन (आंशिक)
async function updateDocument() { const { body } = await client.update({ index: 'articles', id: '1', body: { doc: { views: 151, updated_at: new Date().toISOString() } } }); console.log('दस्तावेज़ अद्यतन किया गया, संस्करण:', body._version);}updateDocument();5. दस्तावेज़ हटाना
async function deleteDocument() { const { body } = await client.delete({ index: 'articles', id: '1' }); console.log('हटाने का परिणाम:', body.result);}deleteDocument();एकत्रीकरण के साथ काम करना
एकत्रीकरण खोज परिणामों से सांख्यिकीय डेटा प्राप्त करने की अनुमति देते हैं।
async function aggregateByTags() { const { body } = await client.search({ index: 'articles', body: { size: 0, // दस्तावेज़ स्वयं वापस न करें aggs: { popular_tags: { terms: { field: 'tags.keywor