This commit is contained in:
parent
6c398fc593
commit
69eb41fc8d
|
@ -19,7 +19,7 @@ aiohttp = "^3.9.1"
|
||||||
pre-commit = "^3.6.0"
|
pre-commit = "^3.6.0"
|
||||||
granian = "^1.0.1"
|
granian = "^1.0.1"
|
||||||
google-analytics-data = "^0.18.3"
|
google-analytics-data = "^0.18.3"
|
||||||
elasticsearch = "^8.12.0"
|
opensearch-py = "^2.4.2"
|
||||||
|
|
||||||
[tool.poetry.group.dev.dependencies]
|
[tool.poetry.group.dev.dependencies]
|
||||||
setuptools = "^69.0.2"
|
setuptools = "^69.0.2"
|
||||||
|
|
|
@ -2,7 +2,7 @@ import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from elasticsearch import Elasticsearch
|
from opensearchpy import OpenSearch
|
||||||
|
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
|
|
||||||
|
@ -26,7 +26,17 @@ class SearchService:
|
||||||
self.index_name = index_name
|
self.index_name = index_name
|
||||||
self.disabled = False
|
self.disabled = False
|
||||||
try:
|
try:
|
||||||
self.elasticsearch_client = Elasticsearch(f'{ELASTIC_URL}', verify_certs=False)
|
self.client = OpenSearch(
|
||||||
|
hosts = [{'host': ELASTIC_HOST, 'port': ELASTIC_PORT}],
|
||||||
|
http_compress = True,
|
||||||
|
http_auth = (ELASTIC_USER, ELASTIC_PASSWORD),
|
||||||
|
use_ssl = True,
|
||||||
|
verify_certs = False,
|
||||||
|
ssl_assert_hostname = False,
|
||||||
|
ssl_show_warn = False,
|
||||||
|
# ca_certs = ca_certs_path
|
||||||
|
)
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.error(exc)
|
logger.error(exc)
|
||||||
self.disabled = True
|
self.disabled = True
|
||||||
|
@ -36,10 +46,10 @@ class SearchService:
|
||||||
self.recreate_index()
|
self.recreate_index()
|
||||||
|
|
||||||
def info(self):
|
def info(self):
|
||||||
logging.info(f'{self.elasticsearch_client}')
|
logging.info(f'{self.client}')
|
||||||
|
|
||||||
def delete_index(self):
|
def delete_index(self):
|
||||||
self.elasticsearch_client.indices.delete(index=self.index_name, ignore_unavailable=True)
|
self.client.indices.delete(index=self.index_name, ignore_unavailable=True)
|
||||||
|
|
||||||
def create_index(self):
|
def create_index(self):
|
||||||
index_settings = {
|
index_settings = {
|
||||||
|
@ -76,9 +86,9 @@ class SearchService:
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
self.elasticsearch_client.indices.create(index=self.index_name, body=index_settings)
|
self.client.indices.create(index=self.index_name, body=index_settings)
|
||||||
self.elasticsearch_client.indices.close(index=self.index_name)
|
self.client.indices.close(index=self.index_name)
|
||||||
self.elasticsearch_client.indices.open(index=self.index_name)
|
self.client.indices.open(index=self.index_name)
|
||||||
|
|
||||||
def put_mapping(self):
|
def put_mapping(self):
|
||||||
mapping = {
|
mapping = {
|
||||||
|
@ -89,10 +99,10 @@ class SearchService:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.elasticsearch_client.indices.put_mapping(index=self.index_name, body=mapping)
|
self.client.indices.put_mapping(index=self.index_name, body=mapping)
|
||||||
|
|
||||||
def check_index(self):
|
def check_index(self):
|
||||||
if not self.elasticsearch_client.indices.exists(index=self.index_name):
|
if not self.client.indices.exists(index=self.index_name):
|
||||||
logger.debug(f'Creating {self.index_name} index')
|
logger.debug(f'Creating {self.index_name} index')
|
||||||
self.create_index()
|
self.create_index()
|
||||||
self.put_mapping()
|
self.put_mapping()
|
||||||
|
@ -104,7 +114,7 @@ class SearchService:
|
||||||
def index_post(self, shout):
|
def index_post(self, shout):
|
||||||
id_ = str(shout.id)
|
id_ = str(shout.id)
|
||||||
logger.debug(f'Indexing post id {id_}')
|
logger.debug(f'Indexing post id {id_}')
|
||||||
self.elasticsearch_client.index(index=self.index_name, id=id_, body=shout)
|
self.client.index(index=self.index_name, id=id_, body=shout)
|
||||||
|
|
||||||
def search_post(self, query, limit, offset):
|
def search_post(self, query, limit, offset):
|
||||||
logger.debug(f'query: {query}')
|
logger.debug(f'query: {query}')
|
||||||
|
@ -112,7 +122,7 @@ class SearchService:
|
||||||
'query': {'match': {'_all': query}},
|
'query': {'match': {'_all': query}},
|
||||||
}
|
}
|
||||||
|
|
||||||
search_response = self.elasticsearch_client.search(
|
search_response = self.client.search(
|
||||||
index=self.index_name, body=search_body, size=limit, from_=offset
|
index=self.index_name, body=search_body, size=limit, from_=offset
|
||||||
)
|
)
|
||||||
hits = search_response['hits']['hits']
|
hits = search_response['hits']['hits']
|
||||||
|
|
Loading…
Reference in New Issue
Block a user