stability-2
Some checks failed
Deploy to core / deploy (push) Failing after 1m33s

This commit is contained in:
Untone 2024-01-29 06:03:37 +03:00
parent cf23d343d1
commit 4b9382c47d

View File

@ -11,21 +11,17 @@ from services.rediscache import redis
logger = logging.getLogger('\t[services.search]\t') logger = logging.getLogger('\t[services.search]\t')
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
ELASTIC_HOST = ( ELASTIC_HOST = os.environ.get('ELASTIC_HOST', '').replace('https://', '').replace('http://', '')
os.environ.get('ELASTIC_HOST', '').replace('https://', '').replace('http://', '')
)
ELASTIC_USER = os.environ.get('ELASTIC_USER', '') ELASTIC_USER = os.environ.get('ELASTIC_USER', '')
ELASTIC_PASSWORD = os.environ.get('ELASTIC_PASSWORD', '') ELASTIC_PASSWORD = os.environ.get('ELASTIC_PASSWORD', '')
ELASTIC_PORT = os.environ.get('ELASTIC_PORT', 9200) ELASTIC_PORT = os.environ.get('ELASTIC_PORT', 9200)
ELASTIC_AUTH = f'{ELASTIC_USER}:{ELASTIC_PASSWORD}' if ELASTIC_USER else '' ELASTIC_AUTH = f'{ELASTIC_USER}:{ELASTIC_PASSWORD}' if ELASTIC_USER else ''
ELASTIC_URL = os.environ.get( ELASTIC_URL = os.environ.get('ELASTIC_URL', f'https://{ELASTIC_AUTH}@{ELASTIC_HOST}:{ELASTIC_PORT}')
'ELASTIC_URL', f'https://{ELASTIC_AUTH}@{ELASTIC_HOST}:{ELASTIC_PORT}'
)
REDIS_TTL = 86400 # 1 day in seconds REDIS_TTL = 86400 # 1 day in seconds
class SearchService: class SearchService:
def __init__(self, index_name='posts'): def __init__(self, index_name='search_index'):
self.index_name = index_name self.index_name = index_name
self.disabled = False self.disabled = False
self.manager = Manager() self.manager = Manager()
@ -59,23 +55,14 @@ class SearchService:
self.disabled = True self.disabled = True
def info(self): def info(self):
try: if self.client:
if self.client: logger.info(f'{self.client}')
logger.info(f'{self.client}') else:
indices = self.client.indices.get_alias('*') logger.info(' * Задайте переменные среды для подключения к серверу поиска')
logger.debug('List of indices:')
for index in indices:
logger.debug(f'- {index}')
else:
logger.info(
' * Задайте переменные среды для подключения к серверу поиска'
)
except Exception as e:
logger.error(f'Error while listing indices: {e}')
def delete_index(self): def delete_index(self):
if not self.disabled: if not self.disabled and self.client:
self.client.indices.delete(index=self.index_name, ignore_unavailable=True) self.client.indices.delete(index=self.index_name, params={'ignore_unavailable': True})
def create_index(self): def create_index(self):
index_settings = { index_settings = {
@ -112,10 +99,11 @@ class SearchService:
}, },
} }
try: try:
with self.lock: if self.client:
self.client.indices.create(index=self.index_name, body=index_settings) with self.lock:
self.client.indices.close(index=self.index_name) self.client.indices.create(index=self.index_name, body=index_settings)
self.client.indices.open(index=self.index_name) self.client.indices.close(index=self.index_name)
self.client.indices.open(index=self.index_name)
except Exception as error: except Exception as error:
logger.warn(error) logger.warn(error)
self.disabled = True self.disabled = True
@ -128,29 +116,28 @@ class SearchService:
'author': {'type': 'text'}, 'author': {'type': 'text'},
} }
} }
if self.client:
self.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.client.indices.exists(index=self.index_name) and not self.disabled: if self.client:
logger.debug(f'Creating {self.index_name} index') if not self.client.indices.exists(index=self.index_name) and not self.disabled:
self.create_index() logger.debug(f'Creating {self.index_name} index')
self.put_mapping() self.create_index()
else: self.put_mapping()
# Check if the mapping is correct, and recreate the index if needed else:
mapping = self.client.indices.get_mapping(index=self.index_name) # Check if the mapping is correct, and recreate the index if needed
expected_mapping = { mapping = self.client.indices.get_mapping(index=self.index_name)
'properties': { expected_mapping = {
'body': {'type': 'text', 'analyzer': 'ru'}, 'properties': {
'text': {'type': 'text'}, 'body': {'type': 'text', 'analyzer': 'ru'},
'author': {'type': 'text'}, 'text': {'type': 'text'},
'author': {'type': 'text'},
}
} }
} if mapping != expected_mapping:
if mapping != expected_mapping: logger.debug(f'Recreating {self.index_name} index due to incorrect mapping')
logger.debug( self.recreate_index()
f'Recreating {self.index_name} index due to incorrect mapping'
)
self.recreate_index()
def recreate_index(self): def recreate_index(self):
with self.lock: with self.lock:
@ -158,7 +145,7 @@ class SearchService:
self.check_index() self.check_index()
def index_post(self, shout): def index_post(self, shout):
if not not self.disabled: if not not self.disabled and self.client:
id_ = str(shout.id) id_ = str(shout.id)
logger.debug(f'Indexing post id {id_}') logger.debug(f'Indexing post id {id_}')
self.client.index(index=self.index_name, id=id_, body=shout) self.client.index(index=self.index_name, id=id_, body=shout)
@ -168,19 +155,18 @@ class SearchService:
search_body = { search_body = {
'query': {'match': {'_all': query}}, 'query': {'match': {'_all': query}},
} }
if self.client:
search_response = self.client.search(index=self.index_name, body=search_body, size=limit, from_=offset)
hits = search_response['hits']['hits']
search_response = self.client.search( return [
index=self.index_name, body=search_body, size=limit, from_=offset {
) **hit['_source'],
hits = search_response['hits']['hits'] 'score': hit['_score'],
}
return [ for hit in hits
{ ]
**hit['_source'], return []
'score': hit['_score'],
}
for hit in hits
]
search = SearchService() search = SearchService()