@ppr_index.doc_type
class RubricPageDocument(DocType):
sections = fields.NestedField(properties={
'blocks': fields.NestedField(properties={
'content': fields.StringField(),
'file_url': fields.StringField(),
'pdf_content': fields.StringField(),
'pk': fields.IntegerField(),
'title': fields.StringField(),
}),
'content': fields.StringField(),
'pk': fields.IntegerField(),
'title': fields.StringField(),
})
branch = fields.NestedField(properties={
'pk': fields.IntegerField(),
'name': fields.StringField(),
})
class Meta:
model = RubricPage
# The fields of the model to be indexed in Elasticsearch
fields = [
'title',
]
# Ensure the rubric index is upated when a section or a branch is updated
related_models = [BlockSectionRubricPage, SectionRubricPage]
def get_instances_from_related(self, related_instance):
"""
Define how to retrieve a RubricPage instance
from a SectionRubricPage or a BlockSectionRubricPage
"""
if isinstance(related_instance, SectionRubricPage):
return related_instance.rubricpage_set.all()
elif isinstance(related_instance, BlockSectionRubricPage):
return RubricPage.objects.filter(sections__blocks__pk=related_instance.pk)
elif isinstance(related_instance, Branch):
return RubricPage.objects.filter(branch__pk=related_instance.pk)
The problem is when I try to search the index with this code:
def search_query_in_rubric_pages(query, branch):
# query for title
q_title = _build_match_query(query, 'title')
# add query for sections
section_fields = ['sections.title', 'sections.content']
q_sections = Nested(
path='sections',
query=Bool(should=[_build_match_query(query, field)
for field in section_fields]),
inner_hits=_build_inner_hits(section_fields)
)
# add query for blocks
block_fields = ['sections.blocks.title',
'sections.blocks.content', 'sections.blocks.pdf_content']
q_blocks = Nested(
path='sections.blocks',
query=Bool(should=[_build_match_query(query, field)
for field in block_fields]),
inner_hits=_build_inner_hits(block_fields)
)
q = q_title | q_sections | q_blocks
s = RubricPageDocument.search().query(q)
s = s.filter("nested", path="branch", query=Match(
**{'branch.name': branch.name}))
# Add highlight
s = s.highlight(
'title',
fragment_size=settings.SEARCH_FRAGMENT_SIZE,
pre_tags=settings.SEARCH_PRE_TAGS,
post_tags=settings.SEARCH_POST_TAGS,
)
response = s.execute()
return [_format_hit(hit, 'rubric') for hit in response]
Hi,
I am trying to make a migration from ES 5.6.4 to 6.5.4.
I currently use
I have a model like this:
And a Document like this
The problem is when I try to search the index with this code:
I get a 400 error from ElasticSearch.
The ES error:
Basicaly, my sections property seems not to be recognized a nested by ES and when I check the ES schema, I get this:
Where the
"type": "nested"
is indeed missing for the Many to Many fields.Any clue what's going on ?
Thanks :)