Better deal with non-existing documents when searching

This commit is contained in:
jonaswinkler 2021-01-13 13:34:52 +01:00
parent 59f955e08b
commit 40842689fd
3 changed files with 26 additions and 10 deletions

View File

@ -16,11 +16,13 @@
<div *ngIf="!errorMessage" [class.result-content-searching]="searching" infiniteScroll (scrolled)="onScroll()"> <div *ngIf="!errorMessage" [class.result-content-searching]="searching" infiniteScroll (scrolled)="onScroll()">
<p i18n>{resultCount, plural, =0 {No results} =1 {One result} other {{{resultCount}} results}}</p> <p i18n>{resultCount, plural, =0 {No results} =1 {One result} other {{{resultCount}} results}}</p>
<app-document-card-large *ngFor="let result of results" <ng-container *ngFor="let result of results">
[document]="result.document" <app-document-card-large *ngIf="result.document"
[details]="result.highlights" [document]="result.document"
[searchScore]="result.score / maxScore" [details]="result.highlights"
[moreLikeThis]="true"> [searchScore]="result.score / maxScore"
[moreLikeThis]="true">
</app-document-card-large>
</ng-container>
</app-document-card-large>
</div> </div>

View File

@ -28,7 +28,11 @@ export class SearchService {
} }
return this.http.get<SearchResult>(`${environment.apiBaseUrl}search/`, {params: httpParams}).pipe( return this.http.get<SearchResult>(`${environment.apiBaseUrl}search/`, {params: httpParams}).pipe(
map(result => { map(result => {
result.results.forEach(hit => this.documentService.addObservablesToDocument(hit.document)) result.results.forEach(hit => {
if (hit.document) {
this.documentService.addObservablesToDocument(hit.document)
}
})
return result return result
}) })
) )

View File

@ -1,3 +1,4 @@
import logging
import os import os
import tempfile import tempfile
from datetime import datetime from datetime import datetime
@ -458,12 +459,21 @@ class SearchView(APIView):
self.ix = index.open_index() self.ix = index.open_index()
def add_infos_to_hit(self, r): def add_infos_to_hit(self, r):
doc = Document.objects.get(id=r['id']) try:
doc = Document.objects.get(id=r['id'])
except Document.DoesNotExist:
logging.getLogger(__name__).warning(
f"Search index returned a non-existing document: "
f"id: {r['id']}, title: {r['title']}. "
f"Search index needs reindex."
)
doc = None
return {'id': r['id'], return {'id': r['id'],
'highlights': r.highlights("content", text=doc.content), 'highlights': r.highlights("content", text=doc.content) if doc else None, # NOQA: E501
'score': r.score, 'score': r.score,
'rank': r.rank, 'rank': r.rank,
'document': DocumentSerializer(doc).data, 'document': DocumentSerializer(doc).data if doc else None,
'title': r['title'] 'title': r['title']
} }