Skip to content

OpenSearch Module

OpenSearch is a community-driven, open source search and analytics suite derived from Elasticsearch. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents.

Install

npm install @testcontainers/opensearch --save-dev

Examples

it.each(images)("should create an index with %s", async (image) => {
  const container = await new OpenSearchContainer(image).start();
  const client = new Client({
    node: container.getHttpUrl(),
    auth: {
      username: container.getUsername(),
      password: container.getPassword(),
    },
    ssl: {
      // trust the self-signed cert
      rejectUnauthorized: false,
    },
  });

  await client.indices.create({ index: "people" });
  const existsResponse = await client.indices.exists({ index: "people" });
  expect(existsResponse.body).toBe(true);
  await container.stop();
});
it("should index a document", async () => {
  const 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 getResponse = await client.get({ index: "people", id: document.id });
  expect(getResponse.body._source).toStrictEqual(document);
  await container.stop();
});
it("should set custom password", async () => {
  const container = await new OpenSearchContainer(IMAGE).withPassword("Str0ng!Passw0rd2025").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 existsResponse = await client.indices.exists({ index: "people" });
  expect(existsResponse.body).toBe(true);
  await container.stop();
});