TypeScript के लिए Elasticsearch क्लाइंट: @elastic/elasticsearch

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें

TypeScript के लिए Elasticsearch क्लाइंट (@elastic/elasticsearch)

आधुनिक वेब डेवलपमेंट में, फुल-टेक्स्ट सर्च और डेटा एनालिसिस कई एप्लिकेशन का अहम हिस्सा बन गए हैं। Elasticsearch Apache Lucene पर आधारित एक शक्तिशाली डिस्ट्रीब्यूटेड सर्च इंजन है। TypeScript एप्लिकेशन (Node.js सर्वर और ब्राउज़र दोनों) में इसका उपयोग करने के लिए आधिकारिक क्लाइंट @elastic/elasticsearch का उपयोग किया जाता है। इस लेख में हम इसकी इंस्टॉलेशन, कॉन्फ़िगरेशन और मुख्य सुविधाओं को विस्तार से समझेंगे।

@elastic/elasticsearch क्या है?

@elastic/elasticsearch Elasticsearch के लिए एक आधिकारिक, हाई-परफॉर्मेंस क्लाइंट है जो TypeScript में लिखा गया है। यह Elasticsearch क्लस्टर के साथ इंटरैक्ट करने के लिए पूरा API प्रदान करता है, जिसमें शामिल हैं:

  • डॉक्यूमेंट इंडेक्स करना और डिलीट करना
  • जटिल क्वेरी के साथ फुल-टेक्स्ट सर्च
  • डेटा एनालिसिस के लिए एग्रीगेशन (ग्रुपिंग, स्टैटिस्टिक्स)
  • इंडेक्स और मैपिंग मैनेजमेंट
  • कनेक्शन पूल, रीट्राई और लोड बैलेंसिंग सपोर्ट

TypeScript के पूर्ण सपोर्ट के कारण, आपको IDE में ऑटो-कम्प्लीशन, टाइप चेकिंग और डॉक्यूमेंटेशन मिलता है।

इंस्टॉलेशन

शुरू करने के लिए, npm या yarn के माध्यम से पैकेज इंस्टॉल करें:

npm install @elastic/elasticsearch
# या
yarn add @elastic/elasticsearch

अगर आप TypeScript का उपयोग कर रहे हैं, तो अलग से टाइप्स इंस्टॉल करने की ज़रूरत नहीं है - वे पैकेज में ही शामिल हैं।

क्लाइंट को कनेक्ट और कॉन्फ़िगर करना

अपने Elasticsearch क्लस्टर का URL देकर क्लाइंट का इंस्टेंस बनाएं:

import { Client } from '@elastic/elasticsearch';

const client = new Client({
  node: 'http://localhost:9200', // या क्लाउड endpoint
  auth: {
    username: 'elastic',
    password: 'changeme'
  },
  // प्रोडक्शन के लिए TLS का उपयोग करें
  tls: {
    rejectUnauthorized: false // केवल डेवलपमेंट के लिए!
  }
});

क्लाउड डिप्लॉयमेंट (Elastic Cloud) के लिए cloud.id और auth.apiKey का उपयोग करें:

const client = new Client({
  cloud: {
    id: 'my-cluster:dXMtZWFzdD...'
  },
  auth: {
    apiKey: 'base64EncodedApiKey'
  }
});

मुख्य ऑपरेशन

1. डॉक्यूमेंट इंडेक्स करना

इंडेक्स में नया डॉक्यूमेंट जोड़ना:

interface Product {
  id: number;
  name: string;
  description: string;
  price: number;
  tags: string[];
}

async function indexProduct(product: Product) {
  const response = await client.index({
    index: 'products',
    id: String(product.id),
    body: product,
    refresh: 'wait_for' // इंडेक्स अपडेट होने तक प्रतीक्षा करें
  });
  console.log('Document indexed:', response.result);
}

indexProduct({
  id: 1,
  name: 'Lenovo ThinkPad Laptop',
  description: 'डेवलपमेंट के लिए शक्तिशाली लैपटॉप',
  price: 1200,
  tags: ['इलेक्ट्रॉनिक्स', 'लैपटॉप']
});

2. डॉक्यूमेंट सर्च करना

description फील्ड में सरल फुल-टेक्स्ट सर्च:

async function searchProducts(query: string) {
  const response = await client.search({
    index: 'products',
    body: {
      query: {
        match: {
          description: query
        }
      },
      // पेजिनेशन
      from: 0,
      size: 10
    }
  });

  const hits = response.hits.hits.map(hit => hit._source);
  console.log('Found products:', hits);
  return hits;
}

searchProducts('लैपटॉप डेवलपमेंट');

3. एग्रीगेशन

टैग के अनुसार उत्पादों को ग्रुप करना:

async function aggregateByTags() {
  const response = await client.search({
    index: 'products',
    body: {
      size: 0, // डॉक्यूमेंट वापस न करें
      aggs: {
        by_tag: {
          terms: {
            field: 'tags.keyword',
            size: 20
          }
        }
      }
    }
  });

  const buckets = response.aggregations?.by_tag?.buckets || [];
  console.log('Tags distribution:', buckets);
}

अनुशंसाएँ