Elasticsearch
Install
| npm install @testcontainers/elasticsearch --save-dev
|
Examples
These examples use the following libraries:
Choose an image from the container registry and substitute IMAGE
.
Create an index
| await using container = await new ElasticsearchContainer(image).start();
const client = new Client({
node: container.getHttpUrl(),
auth: { username: container.getUsername(), password: container.getPassword() },
});
await client.indices.create({ index: "people" });
expect(await client.indices.exists({ index: "people" })).toBe(true);
|
Index a document
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | await using container = await new ElasticsearchContainer(IMAGE).start();
const client = new Client({
node: container.getHttpUrl(),
auth: { username: container.getUsername(), password: container.getPassword() },
});
const document = {
id: "1",
name: "John Doe",
};
await client.index({
index: "people",
id: document.id,
document,
});
expect((await client.get({ index: "people", id: document.id }))._source).toStrictEqual(document);
|
With password
| await using container = await new ElasticsearchContainer(IMAGE).withPassword("testPassword").start();
const client = new Client({
node: container.getHttpUrl(),
auth: { username: container.getUsername(), password: container.getPassword() },
});
|