-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ZEP-13] corrige le comportement d'Elasticsearch #4262
Changes from 5 commits
c8c9ec4
21cf7ef
3b8cb72
22b918f
83bf3b2
11828c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,11 @@ <h3 class="content-title" itemprop="itemListElement"> | |
{% trans "Article publié" %} | ||
{% elif search_result.content_type == 'TUTORIAL' %} | ||
{% trans "Tutoriel publié" %} | ||
{% elif search_result.content_type == 'OPINION' %} | ||
{% trans "Opinion" %} | ||
{% if search_result.picked %} | ||
{% trans "mise en avant" %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Du coup "mis en avant". |
||
{% endif %} | ||
{% else %} | ||
{% trans "Contenu publié" %} | ||
{% endif %} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,15 +234,21 @@ def delete_document_in_elasticsearch(instance): | |
""" | ||
|
||
index_manager = ESIndexManager(**settings.ES_SEARCH_INDEX) | ||
index_manager.delete_document(instance) | ||
index_manager.refresh_index() | ||
|
||
if index_manager.index_exists: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vue la répétition de ces trois lignes, créer une méthode |
||
index_manager.delete_document(instance) | ||
index_manager.refresh_index() | ||
|
||
|
||
def get_django_indexable_objects(): | ||
"""Return all indexable objects registered in Django""" | ||
return [model for model in apps.get_models() if issubclass(model, AbstractESDjangoIndexable)] | ||
|
||
|
||
class NeedIndex(Exception): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Juste ajouter une petite docstring pour faire beau dans la doc :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done ;) |
||
pass | ||
|
||
|
||
class ESIndexManager(object): | ||
"""Manage a given index with different taylor-made functions""" | ||
|
||
|
@@ -260,6 +266,7 @@ def __init__(self, name, shards=5, replicas=0, connection_alias='default'): | |
""" | ||
|
||
self.index = name | ||
self.index_exists = False | ||
|
||
self.number_of_shards = shards | ||
self.number_of_replicas = replicas | ||
|
@@ -282,6 +289,9 @@ def __init__(self, name, shards=5, replicas=0, connection_alias='default'): | |
else: | ||
self.logger.info('connected to ES cluster') | ||
|
||
if self.connected_to_es: | ||
self.index_exists = self.es.indices.exists(self.index) | ||
|
||
def clear_es_index(self): | ||
"""Clear index | ||
""" | ||
|
@@ -293,6 +303,8 @@ def clear_es_index(self): | |
self.es.indices.delete(self.index) | ||
self.logger.info('index cleared') | ||
|
||
self.index_exists = False | ||
|
||
def reset_es_index(self, models): | ||
"""Delete old index and create an new one (with the same name). Setup the number of shards and replicas. | ||
Then, set mappings for the different models. | ||
|
@@ -327,6 +339,8 @@ def reset_es_index(self, models): | |
} | ||
) | ||
|
||
self.index_exists = True | ||
|
||
self.logger.info('index created') | ||
|
||
def setup_custom_analyzer(self): | ||
|
@@ -351,6 +365,9 @@ def setup_custom_analyzer(self): | |
if not self.connected_to_es: | ||
return | ||
|
||
if not self.index_exists: | ||
raise NeedIndex() | ||
|
||
self.es.indices.close(self.index) | ||
|
||
document = { | ||
|
@@ -422,9 +439,6 @@ def clear_indexing_of_model(self, model): | |
:type model: class | ||
""" | ||
|
||
if not self.connected_to_es: | ||
return | ||
|
||
if issubclass(model, AbstractESDjangoIndexable): # use a global update with Django | ||
objs = model.get_es_django_indexable(force_reindexing=True) | ||
objs.update(es_flagged=True, es_already_indexed=False) | ||
|
@@ -456,6 +470,9 @@ def es_bulk_indexing_of_model(self, model, force_reindexing=False): | |
if not self.connected_to_es: | ||
return | ||
|
||
if not self.index_exists: | ||
raise NeedIndex() | ||
|
||
# better safe than sorry | ||
if model.__name__ == 'FakeChapter': | ||
self.logger.warn('Cannot index FakeChapter model. Please index its parent model.') | ||
|
@@ -568,6 +585,9 @@ def refresh_index(self): | |
if not self.connected_to_es: | ||
return | ||
|
||
if not self.index_exists: | ||
raise NeedIndex() | ||
|
||
self.es.indices.refresh(self.index) | ||
|
||
def update_single_document(self, document, doc): | ||
|
@@ -584,6 +604,9 @@ def update_single_document(self, document, doc): | |
if not self.connected_to_es: | ||
return | ||
|
||
if not self.index_exists: | ||
raise NeedIndex() | ||
|
||
arguments = {'index': self.index, 'doc_type': document.get_es_document_type(), 'id': document.es_id} | ||
if self.es.exists(**arguments): | ||
self.es.update(body={'doc': doc}, **arguments) | ||
|
@@ -599,6 +622,9 @@ def delete_document(self, document): | |
if not self.connected_to_es: | ||
return | ||
|
||
if not self.index_exists: | ||
raise NeedIndex() | ||
|
||
arguments = {'index': self.index, 'doc_type': document.get_es_document_type(), 'id': document.es_id} | ||
if self.es.exists(**arguments): | ||
self.es.delete(**arguments) | ||
|
@@ -621,6 +647,9 @@ def delete_by_query(self, doc_type='', query=MatchAll()): | |
if not self.connected_to_es: | ||
return | ||
|
||
if not self.index_exists: | ||
raise NeedIndex() | ||
|
||
response = self.es.delete_by_query(index=self.index, doc_type=doc_type, body={'query': query}) | ||
|
||
self.logger.info('delete_by_query {}s ({})'.format(doc_type, response['deleted'])) | ||
|
@@ -641,6 +670,9 @@ def analyze_sentence(self, request): | |
if not self.connected_to_es: | ||
return | ||
|
||
if not self.index_exists: | ||
raise NeedIndex() | ||
|
||
document = {'text': request} | ||
tokens = [] | ||
for token in self.es.indices.analyze(index=self.index, body=document)['tokens']: | ||
|
@@ -660,4 +692,7 @@ def setup_search(self, request): | |
if not self.connected_to_es: | ||
return | ||
|
||
if not self.index_exists: | ||
raise NeedIndex() | ||
|
||
return request.index(self.index).using(self.es) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,10 +129,22 @@ def get_queryset_publishedcontents(self): | |
'filter': Match(content_type='TUTORIAL'), | ||
'weight': settings.ZDS_APP['search']['boosts']['publishedcontent']['if_tutorial'] | ||
}, | ||
{ | ||
'filter': Match(content_type='TUTORIAL') & Match(has_chapters=True), | ||
'weight': settings.ZDS_APP['search']['boosts']['publishedcontent']['if_medium_or_big_tutorial'] | ||
}, | ||
{ | ||
'filter': Match(content_type='ARTICLE'), | ||
'weight': settings.ZDS_APP['search']['boosts']['publishedcontent']['if_article'] | ||
}, | ||
{ | ||
'filter': Match(content_type='OPINION'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. j'aime ce boost There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <3 |
||
'weight': settings.ZDS_APP['search']['boosts']['publishedcontent']['if_opinion'] | ||
}, | ||
{ | ||
'filter': Match(content_type='OPINION') & Match(picked=False), | ||
'weight': settings.ZDS_APP['search']['boosts']['publishedcontent']['if_opinion_not_picked'] | ||
}, | ||
] | ||
|
||
scored_query = FunctionScore(query=query, boost_mode='multiply', functions=functions_score) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Le mot de base, c'est "Billiet".