File size: 1,265 Bytes
f6c4594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require('dotenv').config();
const { Client } = require('@elastic/elasticsearch');

// Configuration for the Elasticsearch client using environment variables
const client = new Client({
  node: process.env.ELASTICSEARCH_URL,
  auth: {
    username: process.env.ELASTICSEARCH_USERNAME,
    password: process.env.ELASTICSEARCH_PASSWORD
  }
});

async function run() {
  try {
    // Check if the client is connected
    const { body } = await client.ping();
    console.log('Elasticsearch is running:', body);

    // Define an index and a document to be indexed
    const indexName = 'test-index';
    const doc = {
      name: 'John Doe',
      age: 30,
      email: 'john.doe@example.com'
    };

    // Index the document
    const indexResponse = await client.index({
      index: indexName,
      body: doc,
      refresh: true // Refresh to make the indexed document immediately searchable
    });
    console.log('Document indexed with id:', indexResponse.body._id);

    // Search for documents in the index
    const searchResponse = await client.search({
      index: indexName,
      q: 'name:John Doe'
    });
    console.log('Search results:', searchResponse.body.hits.hits);
  } catch (error) {
    console.error('Error:', error.message);
  }
}

run();