This commit is contained in:
parent
cf23d343d1
commit
4b9382c47d
|
@ -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}')
|
||||||
indices = self.client.indices.get_alias('*')
|
|
||||||
logger.debug('List of indices:')
|
|
||||||
for index in indices:
|
|
||||||
logger.debug(f'- {index}')
|
|
||||||
else:
|
else:
|
||||||
logger.info(
|
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,6 +99,7 @@ class SearchService:
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
|
if self.client:
|
||||||
with self.lock:
|
with self.lock:
|
||||||
self.client.indices.create(index=self.index_name, body=index_settings)
|
self.client.indices.create(index=self.index_name, body=index_settings)
|
||||||
self.client.indices.close(index=self.index_name)
|
self.client.indices.close(index=self.index_name)
|
||||||
|
@ -128,10 +116,11 @@ 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 self.client:
|
||||||
if not self.client.indices.exists(index=self.index_name) and not self.disabled:
|
if not self.client.indices.exists(index=self.index_name) and not self.disabled:
|
||||||
logger.debug(f'Creating {self.index_name} index')
|
logger.debug(f'Creating {self.index_name} index')
|
||||||
self.create_index()
|
self.create_index()
|
||||||
|
@ -147,9 +136,7 @@ class SearchService:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if mapping != expected_mapping:
|
if mapping != expected_mapping:
|
||||||
logger.debug(
|
logger.debug(f'Recreating {self.index_name} index due to incorrect mapping')
|
||||||
f'Recreating {self.index_name} index due to incorrect mapping'
|
|
||||||
)
|
|
||||||
self.recreate_index()
|
self.recreate_index()
|
||||||
|
|
||||||
def recreate_index(self):
|
def recreate_index(self):
|
||||||
|
@ -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,10 +155,8 @@ class SearchService:
|
||||||
search_body = {
|
search_body = {
|
||||||
'query': {'match': {'_all': query}},
|
'query': {'match': {'_all': query}},
|
||||||
}
|
}
|
||||||
|
if self.client:
|
||||||
search_response = self.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']
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
@ -181,6 +166,7 @@ class SearchService:
|
||||||
}
|
}
|
||||||
for hit in hits
|
for hit in hits
|
||||||
]
|
]
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
search = SearchService()
|
search = SearchService()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user