2022-07-21 12:47:02 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from opensearchpy import OpenSearch
|
|
|
|
|
|
|
|
|
|
|
|
def initialise_opensearch():
|
|
|
|
auth = (settings.OPENSEARCH_USERNAME, settings.OPENSEARCH_PASSWORD)
|
|
|
|
client = OpenSearch(
|
2022-07-21 12:47:10 +00:00
|
|
|
# fmt: off
|
2022-07-21 12:47:02 +00:00
|
|
|
hosts=[{"host": settings.OPENSEARCH_URL,
|
2022-07-21 12:47:10 +00:00
|
|
|
"port": settings.OPENSEARCH_PORT}],
|
2022-07-21 12:47:02 +00:00
|
|
|
http_compress=False, # enables gzip compression for request bodies
|
|
|
|
http_auth=auth,
|
|
|
|
# client_cert = client_cert_path,
|
|
|
|
# client_key = client_key_path,
|
|
|
|
use_ssl=settings.OPENSEARCH_TLS,
|
|
|
|
verify_certs=False,
|
|
|
|
ssl_assert_hostname=False,
|
|
|
|
ssl_show_warn=False,
|
|
|
|
# a_certs=ca_certs_path,
|
|
|
|
)
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
2022-07-21 12:49:11 +00:00
|
|
|
def construct_query(query, fields, results):
|
2022-07-21 12:49:01 +00:00
|
|
|
if not fields:
|
|
|
|
fields = settings.OPENSEARCH_MAIN_SEARCH_FIELDS
|
2022-07-21 12:49:11 +00:00
|
|
|
if not results:
|
|
|
|
results = 5
|
2022-07-21 12:47:02 +00:00
|
|
|
query = {
|
2022-07-21 12:49:11 +00:00
|
|
|
"size": results,
|
2022-07-21 12:47:02 +00:00
|
|
|
"query": {
|
|
|
|
"multi_match": {
|
|
|
|
"query": query,
|
2022-07-21 12:49:01 +00:00
|
|
|
"fields": fields,
|
2022-07-21 12:47:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
|
|
|
|
|
2022-07-21 12:49:11 +00:00
|
|
|
def run_main_query(client, query, fields=None, results=None):
|
|
|
|
search_query = construct_query(query, fields, results)
|
2022-07-21 12:47:10 +00:00
|
|
|
# fmt: off
|
|
|
|
response = client.search(body=search_query,
|
|
|
|
index=settings.OPENSEARCH_INDEX_MAIN)
|
2022-07-21 12:47:02 +00:00
|
|
|
return response
|