feat: várias melhorias e evoluções no projeto
This commit is contained in:
@ -1,92 +1,67 @@
|
||||
from django_elasticsearch_dsl import Document, fields
|
||||
from django_elasticsearch_dsl.registries import registry
|
||||
|
||||
from .models import DiarioOficial
|
||||
|
||||
|
||||
@registry.register_document
|
||||
class DiarioOficialDocument(Document):
|
||||
tipo = fields.ObjectField(properties={
|
||||
'nome': fields.TextField()
|
||||
})
|
||||
|
||||
numero = fields.TextField()
|
||||
numero = fields.KeywordField()
|
||||
data = fields.DateField()
|
||||
link = fields.TextField()
|
||||
|
||||
# Campo para armazenar todas as páginas para busca
|
||||
content = fields.TextField(
|
||||
analyzer='pt_analyzer',
|
||||
link = fields.KeywordField()
|
||||
tipo = fields.ObjectField(properties={"nome": fields.KeywordField()})
|
||||
|
||||
# Campo para páginas
|
||||
paginas = fields.NestedField(
|
||||
properties={
|
||||
"id": fields.IntegerField(),
|
||||
"numero": fields.IntegerField(),
|
||||
"conteudo": fields.TextField(
|
||||
analyzer="custom_portuguese",
|
||||
fields={
|
||||
"keyword": fields.KeywordField(),
|
||||
"search": fields.TextField(analyzer="custom_portuguese"),
|
||||
},
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
# Campo para armazenar páginas individualmente
|
||||
pages = fields.NestedField(properties={
|
||||
'number': fields.IntegerField(),
|
||||
'content': fields.TextField(
|
||||
analyzer='pt_analyzer',
|
||||
)
|
||||
})
|
||||
|
||||
class Index:
|
||||
name = 'diarios_oficiais'
|
||||
name = "diario_oficial"
|
||||
settings = {
|
||||
'number_of_shards': 1,
|
||||
'number_of_replicas': 0,
|
||||
'analysis': {
|
||||
'filter': {
|
||||
'portuguese_stop': {
|
||||
'type': 'stop',
|
||||
'stopwords': '_portuguese_'
|
||||
},
|
||||
'portuguese_stemmer': {
|
||||
'type': 'stemmer',
|
||||
'language': 'portuguese'
|
||||
},
|
||||
'synonym_filter': {
|
||||
'type': 'synonym',
|
||||
'synonyms': [
|
||||
'lei, legislação, norma',
|
||||
'processo, procedimento, autos',
|
||||
'contrato, acordo, convênio',
|
||||
]
|
||||
"number_of_shards": 1,
|
||||
"number_of_replicas": 0,
|
||||
"analysis": {
|
||||
"analyzer": {
|
||||
"custom_portuguese": {
|
||||
"type": "custom",
|
||||
"tokenizer": "standard",
|
||||
"filter": [
|
||||
"lowercase",
|
||||
"asciifolding",
|
||||
"portuguese_stop",
|
||||
],
|
||||
}
|
||||
},
|
||||
'analyzer': {
|
||||
'pt_analyzer': {
|
||||
'tokenizer': 'standard',
|
||||
'filter': [
|
||||
'lowercase',
|
||||
'portuguese_stop',
|
||||
'portuguese_stemmer',
|
||||
'synonym_filter'
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
"filter": {
|
||||
"portuguese_stop": {"type": "stop", "stopwords": "_portuguese_"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class Django:
|
||||
model = DiarioOficial
|
||||
fields = [
|
||||
'id'
|
||||
]
|
||||
|
||||
def prepare_tipo(self, instance):
|
||||
if instance.tipo:
|
||||
return {
|
||||
'nome': instance.tipo.nome
|
||||
}
|
||||
return {}
|
||||
|
||||
def prepare_content(self, instance):
|
||||
"""Concatena todo o conteúdo de todas as páginas em um único campo para busca"""
|
||||
if instance.page_content:
|
||||
return " ".join([page.get('content', '') for page in instance.page_content])
|
||||
return ""
|
||||
|
||||
def prepare_pages(self, instance):
|
||||
"""Prepara o campo de páginas individuais para exibição e destaque"""
|
||||
if instance.page_content:
|
||||
return instance.page_content
|
||||
return []
|
||||
fields = ["id"]
|
||||
|
||||
def prepare_tipo(self, instance):
|
||||
return {"nome": instance.tipo.nome if instance.tipo else "Sem Tipo"}
|
||||
|
||||
def prepare_link(self, instance):
|
||||
return instance.link or ""
|
||||
|
||||
def prepare_paginas(self, instance):
|
||||
# Preparar páginas ordenadas
|
||||
paginas = instance.paginas.all().order_by("numero")
|
||||
return [
|
||||
{"id": pagina.id, "numero": pagina.numero, "conteudo": pagina.conteudo}
|
||||
for pagina in paginas
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user