How to use the Client function from elasticsearch

Find comprehensive JavaScript elasticsearch.Client code examples handpicked from public code repositorys.

elasticsearch.Client is a constructor function that creates a new Elasticsearch client object, allowing you to connect to and interact with an Elasticsearch cluster using JavaScript.

800
801
802
803
804
805
806
807
808
809
  return esClient
}
const esHost = config.get('ES.HOST')
// AWS ES configuration is different from other providers
if (/.*amazonaws.*/.test(esHost)) {
  esClient = elasticsearch.Client({
    apiVersion: config.get('ES.API_VERSION'),
    hosts: esHost,
    connectionClass: require('http-aws-es'), // eslint-disable-line global-require
    amazonES: {
fork icon44
star icon17
watch icon25

+ 9 other calls in file

26
27
28
29
30
31
32
33
34
35
36
37
38


app.use(bodyParser.json());


const ES_API_VERSION = process.env.ES_API_VERSION || '7.6';


const client = new elasticsearch.Client({
  host: process.env.ELASTICSEARCH,
  log: 'warning',
  apiVersion: ES_API_VERSION
});
fork icon33
star icon66
watch icon39

How does elasticsearch.Client work?

elasticsearch.Client is a constructor function provided by the Elasticsearch JavaScript client, which allows you to create a new Elasticsearch client object.

When elasticsearch.Client is called with an object containing the configuration options for your Elasticsearch cluster, it creates a new client object that can be used to interact with the cluster.

The client object provides a variety of methods that can be used to perform operations on the Elasticsearch cluster, such as indexing, searching, updating, and deleting documents.

The elasticsearch.Client constructor is often used in Node.js applications to connect to and interact with Elasticsearch clusters.

430
431
432
433
434
435
436
437
438
439
  return esClient
}
const esHost = config.get('ES.HOST')
// // AWS ES configuration is different from other providers
// if (/.*amazonaws.*/.test(esHost)) {
//   esClient = elasticsearch.Client({
//     apiVersion: config.get('ES.API_VERSION'),
//     hosts: esHost,
//     connectionClass: require('http-aws-es'), // eslint-disable-line global-require
//     amazonES: {
fork icon11
star icon4
watch icon18

+ 29 other calls in file

118
119
120
121
122
123
124
125
126
127
128
var client = undefined;
setTimeout(startElastic, elasticTimer);


function startElastic() {
    try {
        client = new elasticsearch.Client({
            hosts: hosts,
            maxRetries: 10,
            requestTimeout: 100000,
            // requestTimeout: Infinity,
fork icon1
star icon4
watch icon5

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const { Client } = require("@elastic/elasticsearch");

const client = new Client({
  node: "http://localhost:9200",
});

async function indexDocument() {
  try {
    const { body } = await client.index({
      index: "myindex",
      body: {
        title: "My Document",
        content: "This is a sample document.",
      },
    });
    console.log(body);
  } catch (error) {
    console.error(error);
  }
}

indexDocument();

In this example, we're using elasticsearch.Client to create a new Elasticsearch client object and connect to an Elasticsearch cluster running on http://localhost:9200. We're then defining an indexDocument function that uses the client object to index a new document with the title "My Document" and the content "This is a sample document". We're using the client.index method to perform the indexing operation, and using console.log to output the response body. Finally, we're calling the indexDocument function to execute the indexing operation. When we run this code, Elasticsearch will index the new document and return a response indicating that the operation was successful. As you can see, elasticsearch.Client has made it easy to connect to and interact with an Elasticsearch cluster using JavaScript.

4
5
6
7
8
9
10
11
12
13
14
var elasticsearch = require('elasticsearch');
var elas = function (config) {
};
elas.connect = function (config) {
	//  this.logger.error(config);
	this.elasClient = new elasticsearch.Client(config);
	this.logger = FLLogger.getLogger("elasticsearch.log");
}


/**
fork icon0
star icon0
watch icon1

+ 8 other calls in file

16
17
18
19
20
21
22
23
24
25
};
let nodeIndex = 1;
// elasticsearch
const ESINDEX = "ningmoindex";
const ESTYPE = "_doc";
const ESClient = new elasticsearch.Client({
  host: "127.0.0.1:9200",
  log: "error",
});

fork icon0
star icon0
watch icon1

80
81
82
83
84
85
86
87
88
const clientSource = new elasticsearch.Client({
    host: sourceHost,
    requestTimeout: 30000
});

const clientTarget = new elasticsearch.Client({
    host: targetHost,
    requestTimeout: 30000
});
fork icon0
star icon0
watch icon1

+ 2 other calls in file

43
44
45
46
47
48
49
50
51
52
53
54
//    });
//  });


//Elastic Client


const elasticClient = new elasticsearch.Client({
   host: '209.94.59.43:9200',
   //log: 'trace'
});

fork icon0
star icon0
watch icon1

+ 3 other calls in file