Use OpenSearch Container Image For Local Development Version#

Keywords: AWS, Amazon, OpenSearch, OS, OSS, Docker, Unittest, Unit Test.

OpenSearch Container Image for Local Development#

If you just want to try out OpenSearch, learn app development, Query language syntax, or test your app, you can use container image to run a test server locally.

First, ensure that you have docker daemon running on your machine. On Windows or MacOS, you use Docker desktop.

Pull image

docker pull opensearchproject/opensearch:latest

Run it locally. The default username password is admin/admin. It has a built-in SSL server. I added --rm, so the container will be removed after you stop it. Note that all index and data will be gone after you stop the container.

docker run --rm -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" --name opensearch-node -d opensearchproject/opensearch:latest

If you want to retain the data after you stop the container, you can mount a local folder to the container. The following command will mount the local folder:

docker run --rm -v /path/to/your/data/folder:/usr/share/opensearch/data -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" --name opensearch-node -d opensearchproject/opensearch:latest

This command will stop container and remove it:

docker stop opensearch-node

connect_to_local_oss.py tests the connection to a local OpenSearch container.

 1# -*- coding: utf-8 -*-
 2
 3"""
 4This script tests the connection to a local OpenSearch container.
 5"""
 6
 7from datetime import datetime, timezone
 8
 9from opensearchpy import OpenSearch
10from rich import print as rprint
11
12
13def create_oss_for_local_container(test_conn: bool = True) -> OpenSearch:
14    """
15    If you are using the OpenSearch Docker image for local developing following
16    this https://hub.docker.com/r/opensearchproject/opensearch,
17    and you are using this command to start the container:
18    ``docker run -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" --name opensearch-node -d opensearchproject/opensearch:latest``
19    then this function should give you the right OSS object.
20    """
21    oss = OpenSearch(
22        hosts=[{"host": "localhost", "port": 9200}],
23        http_auth=("admin", "admin"),
24        http_compress=True,
25        use_ssl=True,
26        verify_certs=False,
27        ssl_assert_hostname=False,
28        ssl_show_warn=False,
29        timeout=300,
30    )
31    if test_conn:
32        res = oss.cat.indices(format="json")
33        print(res)
34    return oss
35
36
37def delete_index():
38    res = oss.indices.delete(index=index_name, ignore=[400, 404])
39    # print(res)
40
41
42def create_index():
43    res = oss.indices.create(
44        index=index_name,
45        body={
46            "mappings": {
47                "properties": {
48                    "time": {"type": "date"},
49                    "log": {"type": "text"},
50                }
51            }
52        },
53        ignore=400,
54    )
55    # print(res)
56
57
58def get_utc_now() -> datetime:
59    return datetime.utcnow().replace(tzinfo=timezone.utc)
60
61
62def insert_document():
63    oss.index(
64        index=index_name,
65        id="id-1",
66        body={
67            "time": get_utc_now(),
68            "log": "login failed, username not found",
69        },
70    )
71
72
73def search_all():
74    res = oss.search(
75        index=index_name,
76        body={"query": {"match_all": {}}},
77    )
78    rprint(res)
79
80
81def search_by_fts():
82    res = oss.search(
83        index=index_name,
84        body={"query": {"match": {"log": "failed"}}},
85    )
86    rprint(res)
87
88
89if __name__ == "__main__":
90    oss = create_oss_for_local_container(test_conn=False)
91    index_name = "app_log"
92
93    # delete_index()
94    create_index()
95    # insert_document()
96    # search_all()
97    # search_by_fts()