Skip to content
Snippets Groups Projects
Commit 8e46ed85 authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Merge branch 'sort-volumes' into 'master'

Sort search results by volume names

See merge request !120
parents d0bb7818 ed1a6373
No related branches found
No related tags found
1 merge request!120Sort search results by volume names
......@@ -19,7 +19,7 @@ class PageSearch(SearchAPIView):
serializer_class = PageSearchResultSerializer
template_path = 'elastic/search_nested.json'
es_source = False
es_sort = ["_score", ]
es_sort = ["volumes", "_score", ]
es_index = settings.ES_INDEX_PAGES
es_type = Page.INDEX_TYPE
......@@ -34,7 +34,7 @@ class ActSearch(SearchAPIView):
serializer_class = ActSearchResultSerializer
template_path = 'elastic/search_nested.json'
es_source = False
es_sort = ['_score', ]
es_sort = ["volumes", "_score", ]
es_index = settings.ES_INDEX_ACTS
es_type = Act.INDEX_TYPE
......
......@@ -27,6 +27,7 @@ class Indexer(object):
Act.INDEX_TYPE: {
"properties": {
"corpus": {"type": "keyword"},
"volumes": {"type": "keyword"},
"transcriptions": {
"type": "nested",
"properties": {
......@@ -51,6 +52,7 @@ class Indexer(object):
Page.INDEX_TYPE: {
"properties": {
"corpus": {"type": "keyword"},
"volumes": {"type": "keyword"},
"transcriptions": {
"type": "nested",
"properties": {
......
......@@ -385,6 +385,7 @@ class Page(Element):
"""
return {
'corpus': self.corpus_id,
'volumes': [v.name for v in Element.objects.get_ascending(self.id, type=ElementType.Volume)],
'transcriptions': [
{
'id': t.id,
......@@ -451,6 +452,7 @@ class Act(Element):
]
return {
'corpus': self.corpus_id,
'volumes': [v.name for v in Element.objects.get_ascending(self.id, type=ElementType.Volume)],
'transcriptions': [
{
'id': t.id,
......
......@@ -55,7 +55,6 @@ def search_nested_post(model, data):
results = data['hits']['hits']
elt_ids = [uuid.UUID(r['_id']) for r in results]
elt_scores = {uuid.UUID(r['_id']): r['_score'] for r in results}
if not elt_ids:
return
tr_ids = [
......@@ -73,7 +72,6 @@ def search_nested_post(model, data):
for t in Transcription.objects.filter(id__in=tr_ids).prefetch_related('zone__image__server')
}
elts = model.objects.filter(id__in=elt_ids).prefetch_related('corpus')
elts_tr_ids = {
uuid.UUID(result['_id']): [
uuid.UUID(hit['_source']['id'])
......@@ -81,11 +79,15 @@ def search_nested_post(model, data):
] for result in results
}
all_paths = Element.objects.get_ascendings_paths(*elt_ids)
elts = list(model.objects.filter(id__in=elt_ids).prefetch_related('corpus'))
# Preserve the ordering given by ElasticSearch
ordered_elts = list(filter(None, map(lambda eid: next((e for e in elts if e.id == eid), None), elt_ids)))
for elt in elts:
all_paths = Element.objects.get_ascendings_paths(*(e.id for e in ordered_elts))
for elt in ordered_elts:
elt.transcriptions_results = list(filter(None, [transcriptions.get(tid) for tid in elts_tr_ids[elt.id]]))
elt.total_transcriptions = tr_totals[elt.id]
elt.parent_paths = all_paths.get(elt.id, [])
return sorted(elts, key=lambda e: elt_scores[e.id], reverse=True)
return ordered_elts
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment