Skip to content

OpenSearch

Install

1
npm install @testcontainers/opensearch --save-dev

Examples

These examples use the following libraries:

Choose an image from the container registry and substitute IMAGE.

Create an index

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
await using container = await new OpenSearchContainer(image).start();

const client = new Client({
  node: container.getHttpUrl(),
  auth: {
    username: container.getUsername(),
    password: container.getPassword(),
  },
  ssl: {
    rejectUnauthorized: false,
  },
});

await client.indices.create({ index: "people" });

const { body } = await client.indices.exists({ index: "people" });
expect(body).toBe(true);

Index a document

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
await using container = await new OpenSearchContainer(IMAGE).start();

const client = new Client({
  node: container.getHttpUrl(),
  auth: {
    username: container.getUsername(),
    password: container.getPassword(),
  },
  ssl: {
    rejectUnauthorized: false,
  },
});

const document = { id: "1", name: "John Doe" };

await client.index({
  index: "people",
  id: document.id,
  body: document,
});

const { body } = await client.get({ index: "people", id: document.id });
expect(body._source).toEqual(document);

With password

1
await using container = await new OpenSearchContainer(IMAGE).withPassword("Str0ng!Passw0rd2025").start();