Replace OpenSearch with Elasticsearch

This commit is contained in:
Mark Veidemanis 2022-11-21 07:20:29 +00:00
parent 7702e04286
commit 61f93390d9
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
8 changed files with 28 additions and 28 deletions

View File

@ -1,9 +1,9 @@
# OpenSearch settings # Elasticsearch settings
OPENSEARCH_URL = "10.1.0.1" ELASTICSEARCH_URL = "10.1.0.1"
OPENSEARCH_PORT = 9200 ELASTICSEARCH_PORT = 9200
OPENSEARCH_TLS = True ELASTICSEARCH_TLS = True
OPENSEARCH_USERNAME = "admin" ELASTICSEARCH_USERNAME = "admin"
OPENSEARCH_PASSWORD = "secret" ELASTICSEARCH_PASSWORD = "secret"
# Manticore settings # Manticore settings
MANTICORE_URL = "http://example-db-1:9308" MANTICORE_URL = "http://example-db-1:9308"
@ -58,7 +58,7 @@ DRILLDOWN_DEFAULT_PARAMS = {
# # Delay results by this many days # # Delay results by this many days
# DELAY_DURATION = 10 # DELAY_DURATION = 10
OPENSEARCH_BLACKLISTED = { ELASTICSEARCH_BLACKLISTED = {
} }

View File

@ -217,7 +217,7 @@ class StorageBackend(object):
# For every hit from ES # For every hit from ES
for index, item in enumerate(list(response["hits"]["hits"])): for index, item in enumerate(list(response["hits"]["hits"])):
# For every blacklisted type # For every blacklisted type
for blacklisted_type in settings.OPENSEARCH_BLACKLISTED.keys(): for blacklisted_type in settings.ELASTICSEARCH_BLACKLISTED.keys():
# Check this field we are matching exists # Check this field we are matching exists
if "_source" in item.keys(): if "_source" in item.keys():
data_index = "_source" data_index = "_source"
@ -228,7 +228,7 @@ class StorageBackend(object):
if blacklisted_type in item[data_index].keys(): if blacklisted_type in item[data_index].keys():
content = item[data_index][blacklisted_type] content = item[data_index][blacklisted_type]
# For every item in the blacklisted array for the type # For every item in the blacklisted array for the type
for blacklisted_item in settings.OPENSEARCH_BLACKLISTED[ for blacklisted_item in settings.ELASTICSEARCH_BLACKLISTED[
blacklisted_type blacklisted_type
]: ]:
if blacklisted_item == str(content): if blacklisted_item == str(content):

View File

@ -2,8 +2,8 @@
# from datetime import datetime, timedelta # from datetime import datetime, timedelta
from django.conf import settings from django.conf import settings
from opensearchpy import OpenSearch from elasticsearch import Elasticsearch
from opensearchpy.exceptions import NotFoundError, RequestError from elasticsearch.exceptions import NotFoundError, RequestError
from core.db import StorageBackend from core.db import StorageBackend
@ -19,18 +19,18 @@ class ElasticsearchBackend(StorageBackend):
def initialise(self, **kwargs): def initialise(self, **kwargs):
""" """
Inititialise the OpenSearch API endpoint. Inititialise the Elastuicsearch API endpoint.
""" """
auth = (settings.OPENSEARCH_USERNAME, settings.OPENSEARCH_PASSWORD) auth = (settings.ELASTICSEARCH_USERNAME, settings.ELASTICSEARCH_PASSWORD)
client = OpenSearch( client = Elasticsearch(
# fmt: off # fmt: off
hosts=[{"host": settings.OPENSEARCH_URL, hosts=[{"host": settings.ELASTICSEARCH_URL,
"port": settings.OPENSEARCH_PORT}], "port": settings.ELASTICSEARCH_PORT}],
http_compress=False, # enables gzip compression for request bodies http_compress=False, # enables gzip compression for request bodies
http_auth=auth, http_auth=auth,
# client_cert = client_cert_path, # client_cert = client_cert_path,
# client_key = client_key_path, # client_key = client_key_path,
use_ssl=settings.OPENSEARCH_TLS, use_ssl=settings.ELASTICSEARCH_TLS,
verify_certs=False, verify_certs=False,
ssl_assert_hostname=False, ssl_assert_hostname=False,
ssl_show_warn=False, ssl_show_warn=False,
@ -40,7 +40,7 @@ class ElasticsearchBackend(StorageBackend):
def construct_query(self, query, size, use_query_string=True, tokens=False): def construct_query(self, query, size, use_query_string=True, tokens=False):
""" """
Accept some query parameters and construct an OpenSearch query. Accept some query parameters and construct an Elasticsearch query.
""" """
if not size: if not size:
size = 5 size = 5
@ -116,10 +116,10 @@ class ElasticsearchBackend(StorageBackend):
try: try:
response = client.search(body=search_query, index=index) response = client.search(body=search_query, index=index)
except RequestError as err: except RequestError as err:
print("OpenSearch error", err) print("Elasticsearch error", err)
return err return err
except NotFoundError as err: except NotFoundError as err:
print("OpenSearch error", err) print("Elasticsearch error", err)
return err return err
return response return response
@ -137,7 +137,7 @@ class ElasticsearchBackend(StorageBackend):
tags=None, tags=None,
): ):
""" """
API helper to alter the OpenSearch return format into something API helper to alter the Elasticsearch return format into something
a bit better to parse. a bit better to parse.
Accept a HTTP request object. Run the query, and annotate the Accept a HTTP request object. Run the query, and annotate the
results with the other data we have. results with the other data we have.

View File

@ -7,9 +7,9 @@ def get_db():
return DruidBackend() return DruidBackend()
elif settings.DB_BACKEND == "ELASTICSEARCH": elif settings.DB_BACKEND == "ELASTICSEARCH":
from core.db.elastic import ElasticSearchBackend from core.db.elastic import ElasticsearchBackend
return OpensearchBackend() return ElasticsearchBackend()
elif settings.DB_BACKEND == "MANTICORE": elif settings.DB_BACKEND == "MANTICORE":
from core.db.manticore import ManticoreBackend from core.db.manticore import ManticoreBackend

View File

@ -3,7 +3,7 @@ from math import ceil
from django.conf import settings from django.conf import settings
from numpy import array_split from numpy import array_split
from core.db.opensearch import client, run_main_query from core.db.elastic import client, run_main_query
def construct_query(net, nicks): def construct_query(net, nicks):
@ -48,7 +48,7 @@ def get_meta(request, net, nicks, iter=True):
request.user, request.user,
query, query,
custom_query=True, custom_query=True,
index=settings.OPENSEARCH_INDEX_META, index=settings.ELASTICSEARCH_INDEX_META,
) )
if "hits" in results.keys(): if "hits" in results.keys():
if "hits" in results["hits"]: if "hits" in results["hits"]:

View File

@ -2,7 +2,7 @@ wheel
django django
django-crispy-forms django-crispy-forms
crispy-bulma crispy-bulma
#opensearch-py elasticsearch
stripe stripe
django-rest-framework django-rest-framework
numpy numpy

View File

@ -2,7 +2,7 @@ wheel
django django
django-crispy-forms django-crispy-forms
crispy-bulma crispy-bulma
#opensearch-py elasticsearch
stripe stripe
django-rest-framework django-rest-framework
numpy numpy

View File

@ -3,7 +3,7 @@ django
pre-commit pre-commit
django-crispy-forms django-crispy-forms
crispy-bulma crispy-bulma
#opensearch-py elasticsearch
stripe stripe
django-rest-framework django-rest-framework
numpy numpy