from django_elasticsearch_dsl import DocType, Index, fields from elasticsearch_dsl import analyzer, token_filter, tokenizer from .models import Stage, AvisStage, AvisLieu from .statics import PAYS_OPTIONS PAYS_DICT = dict(PAYS_OPTIONS) stage = Index('stages') stage.settings( number_of_shards=1, number_of_replicas=0 ) text_analyzer = analyzer( 'default', tokenizer="standard", filter=['lowercase', 'standard', 'asciifolding', token_filter("frstop", type="stop", stopwords="_french_"), token_filter("frsnow", type="snowball", language="French")]) stage.analyzer(text_analyzer) @stage.doc_type class StageDocument(DocType): lieux = fields.ObjectField(properties={ 'nom': fields.StringField(), 'ville': fields.StringField(), 'pays': fields.StringField(), }) auteur = fields.ObjectField(properties={ 'nom': fields.StringField(), }) thematiques = fields.StringField() matieres = fields.StringField() class Meta: model = Stage fields = [ 'sujet', 'encadrants', 'type_stage', 'niveau_scol', 'structure', 'date_debut', 'date_fin' ] def prepare_thematiques(self, instance): return ", ".join(instance.thematiques.all().values_list("name", flat=True)) def prepare_matieres(self, instance): return ", ".join(instance.matieres.all().values_list("nom", flat=True)) def prepare_niveau_scol(self, instance): return instance.get_niveau_scol_display() def prepare_type_stage(self, instance): return instance.type_stage_fancy def prepare_date_fin(self, instance): return instance.date_fin.year def prepare_date_debut(self, instance): return instance.date_debut.year # Hook pour l'affichage des noms de pays def prepare(self, instance): data = super(StageDocument, self).prepare(instance) for lieu in data['lieux']: lieu['pays'] = PAYS_DICT[lieu['pays']] return data