-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopensearch_client_opensource.py
More file actions
70 lines (59 loc) · 1.88 KB
/
Copy pathopensearch_client_opensource.py
File metadata and controls
70 lines (59 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from opensearchpy import OpenSearch
import opensearchpy
from exceptions import CreateIndexError, SystemSetupError
from ingestion_prepper import write_to_string
import logging
logger = logging.getLogger(__name__)
host = '' # OpenSource Endpoint
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
port = 443
# Create the client with SSL/TLS enabled, but hostname verification disabled.
client = OpenSearch(
hosts = [{'host': host, 'port': port}],
http_compress = True, # enables gzip compression for request bodies
http_auth = auth,
use_ssl = True,
verify_certs = False,
ssl_assert_hostname = False,
ssl_show_warn = False,
)
# Create an index with non-default settings.
index_name = 'python-test-index-test'
index_body = {
'settings': {
'index': {
'number_of_shards': 5
}
}
}
def create_index(index_name:str, index_body:dict):
try:
response = client.indices.create(index_name, body=index_body)
except opensearchpy.exceptions.RequestError:
msg = f"Could not create index {index_name}!"
raise CreateIndexError(msg)
# https://opensearch-project.github.io/opensearch-py/_modules/opensearchpy/exceptions.html
def validating_mapping_and_create_monthly_index(index_name_with_date:str, index_mappings:str):
try:
response = client.indices.create(index_name_with_date, body=index_mappings)
print('\nCreating index:')
print(response)
except opensearchpy.exceptions.RequestError:
msg = f"Could not create index {index_name_with_date}"
raise CreateIndexError(msg)
# validating_mapping_and_create_monthly_index("index-3-24", index_body)
# create_index("grocery-products", index_body)
# products = write_to_string()
# print(products)
# client.bulk(products)
query = {
"size": 5,
"query": {
"match_all": {}
}
}
response = client.search(
body=query,
index='grocery-products'
)
print(response)