search-thread
This commit is contained in:
parent
24cbba0746
commit
b4f86526a2
|
@ -1,6 +1,5 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from multiprocessing import Manager
|
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from opensearchpy import OpenSearch
|
from opensearchpy import OpenSearch
|
||||||
|
@ -39,7 +38,8 @@ index_settings = {
|
||||||
'properties': {
|
'properties': {
|
||||||
'body': {'type': 'text', 'analyzer': 'ru'},
|
'body': {'type': 'text', 'analyzer': 'ru'},
|
||||||
'title': {'type': 'text', 'analyzer': 'ru'},
|
'title': {'type': 'text', 'analyzer': 'ru'},
|
||||||
# 'author': {'type': 'text'},
|
'lead': {'type': 'text', 'analyzer': 'ru'},
|
||||||
|
'subtitle': {'type': 'text', 'analyzer': 'ru'},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -50,12 +50,10 @@ expected_mapping = index_settings['mappings']
|
||||||
class SearchService:
|
class SearchService:
|
||||||
def __init__(self, index_name='search_index'):
|
def __init__(self, index_name='search_index'):
|
||||||
self.index_name = index_name
|
self.index_name = index_name
|
||||||
self.manager = Manager()
|
|
||||||
self.client = None
|
self.client = None
|
||||||
|
|
||||||
# Используем менеджер для создания Lock и Value
|
# Используем менеджер для создания Lock и Value
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
self.initialized_flag = self.manager.Value('i', 0)
|
|
||||||
|
|
||||||
# Only initialize the instance if it's not already initialized
|
# Only initialize the instance if it's not already initialized
|
||||||
if not self.initialized_flag.value and ELASTIC_HOST:
|
if not self.initialized_flag.value and ELASTIC_HOST:
|
||||||
|
@ -129,13 +127,18 @@ class SearchService:
|
||||||
def recreate_index(self):
|
def recreate_index(self):
|
||||||
if self.lock.acquire(blocking=False):
|
if self.lock.acquire(blocking=False):
|
||||||
try:
|
try:
|
||||||
self.delete_index()
|
thread = threading.Thread(target=self._recreate_index)
|
||||||
self.check_index()
|
thread.start()
|
||||||
|
thread.join()
|
||||||
finally:
|
finally:
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
else:
|
else:
|
||||||
logger.debug(' cant reindex at the moment')
|
logger.debug(' cant reindex at the moment')
|
||||||
|
|
||||||
|
def _recreate_index(self):
|
||||||
|
self.delete_index()
|
||||||
|
self.check_index()
|
||||||
|
|
||||||
def index(self, shout):
|
def index(self, shout):
|
||||||
if self.client:
|
if self.client:
|
||||||
id_ = str(shout.id)
|
id_ = str(shout.id)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user