Mike-Heneghan / ALISS

ALISS (A Local Information System for Scotland) is a service to help you find help and support close to you when you need it most.
https://aliss.org
0 stars 0 forks source link

Refactor the organisation index, views and templates to filter db records with elastic search results. #35

Closed Mike-Heneghan closed 5 years ago

Mike-Heneghan commented 5 years ago

Currently, the ElasticSearch model is being used to display the results which are is good practice as it is not it's intended purpose. Instead, only fields which are searched against should be in the elastic search model. The hits from the search should be used to filter DB records to ensure the newest information is displayed.

Organisation.objects.filter(id__in=[])

ids = { x.id for x in result} Organisation.objects.filter(id__in=ids)

Mike-Heneghan commented 5 years ago

Updated so that the content displayed is from the db rather than the elastic search model.

It appeared that pagination was not working so the TemplateView was refactored as a ListView. This appeared not to be successful until the paginate_by was reduced to 5 which showed it was in fact working but the number of results was lower than expected.

Number of DB records:

>>> Organisation.objects.all().count()
25

Number of ElasticSearch responses:

>>> queryset = Search(index='organisation_search', doc_type='organisation').count()
>>> queryset
25

Number of returned orgs after it is executed:

>>> queryset = len(Search(index='organisation_search', doc_type='organisation').execute())
>>> queryset
10
Mike-Heneghan commented 5 years ago

Resolved the issue of the limited results by removing the .execute() call although I'm unsure when I introduced that in the first place. Also resolved an issue with the location and service partials not formatting correctly.

Mike-Heneghan commented 5 years ago

To do:

Mike-Heneghan commented 5 years ago

From a comment online on ES

es_search = Search(...)
es_search = es_search.extra(_source={"includes": ["id"]})
res = es_search.execute()
Mike-Heneghan commented 5 years ago

At the moment returning to an old commit which displayed the ES results until furhter investigation is done.

The branch from the old commit is: https://github.com/Mike-Heneghan/ALISS/tree/enhance/organisation-search-es-display

Updated the index so that the services fields are removed and hence they do not appear in the search results.

Also rmeoved the filter for has services by commenting it out in the template.

Mike-Heneghan commented 5 years ago

For adding the related services function to avoid hitting the db again try to prefetch the data.

https://docs.djangoproject.com/en/1.11/ref/models/querysets/#prefetch-related or selected related.

Mike-Heneghan commented 5 years ago

Check that only published results appear for users who aren't logged in or have basic permissions.

Mike-Heneghan commented 5 years ago

When requesting records for services related to the organisations returned from the search to reduce trips to the db need to look into:

Prefetch related:

or

Select Related:

Mike-Heneghan commented 5 years ago

Checked out the enhance/organisation-search-view branch which was using the results from the es search to query the db.

Need to:

Mike-Heneghan commented 5 years ago

Means of filtering on number of services using QuerySet api.

orgs = Organisation.objects.annotate(num_services=Count('services')).filter(num_services__gt = 0)
Mike-Heneghan commented 5 years ago

Merged into master