Commit b11f6842 authored by Patrick Kimber's avatar Patrick Kimber

Include deleted contacts in the ElasticSearch index

parent dc5e8fef
Pipeline #18662686 passed with stage
in 1 minute and 46 seconds
......@@ -64,6 +64,9 @@ class ContactIndex:
"type": "text",
"analyzer": "english",
},
"is_deleted": {
"type": "boolean",
},
"postcode": {
"type": "text",
"analyzer": "autocomplete",
......@@ -110,7 +113,7 @@ class ContactIndex:
"""Name of the Elasticsearch index (will have the domain added)."""
return 'contact'
def query(self, criteria):
def query(self, criteria, include_deleted):
"""The Elasticsearch query."""
should = [
{
......@@ -142,24 +145,51 @@ class ContactIndex:
}
},
]
query = {
"should": should,
}
if not include_deleted:
# using a 'bool' field with 'filter', 'term' assigns a score of 0
# to contacts which only match the 'is_deleted' field:
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#_scoring_with_literal_bool_filter_literal
#
# not sure how 'minimum_should_match' works, but it seems to
# exclude contacts which only match the 'is_deleted' flag
query.update({
'filter': {
'term': {
'is_deleted': False,
},
},
'minimum_should_match': 1,
})
# query['filter'] = [
# {
# 'term': {
# 'is_deleted': False,
# },
# },
# ]
# query['minimum_should_match'] = 1
# import json
# print(json.dumps(query, indent=4))
return {
"query": {
"bool": {
"should": should,
}
'query': {
'bool': query,
},
"sort": [
"_score",
'sort': [
'_score',
{
"postcode.keyword": "asc",
'postcode.keyword': 'asc',
},
]
}
def queryset(self, pk):
"""Build the Elasticsearch index using this queryset."""
if pk is None:
qs = Contact.objects.all().order_by('user__username')
qs = Contact.objects.all().order_by('pk')
else:
qs = Contact.objects.filter(pk=pk)
return qs
......@@ -171,13 +201,18 @@ class ContactIndex:
(see the ``Contact`` model for more information).
"""
return {
result = {
'address': row.address(exclude_postcode=True),
'email': row.user.email,
'full_name': row.full_name,
'postcode': row.postcode,
'username': row.user.username,
'is_deleted': row.is_deleted,
}
print()
import json
print(json.dumps(result, indent=4))
return result
def table_headings(self):
"""Table headings for the results."""
......
......@@ -16,9 +16,9 @@ def test_drop_create():
@pytest.mark.django_db
@pytest.mark.elasticsearch
def test_search():
def test_search_x():
c1 = ContactFactory(
company_name='Applewood',
company_name='wood',
postcode='EX2 05B',
)
c2 = ContactFactory(
......@@ -49,8 +49,9 @@ def test_search():
search_index.drop_create()
assert 4 == search_index.update()
result, total = search_index.search('apple')
assert 3 == total
assert [c3.pk, c4.pk, c2.pk] == [x['pk'] for x in result]
# assert 3 == total
print([x['score'] for x in result])
assert [c3.pk, c4.pk, c2.pk] == [x['pk'] for x in result], c1.pk
assert [
'contact',
'contact',
......@@ -85,6 +86,29 @@ def test_search():
] == check
@pytest.mark.django_db
@pytest.mark.elasticsearch
def test_search_deleted():
user = UserFactory()
c1 = ContactFactory(company_name='KB', postcode='EX20 3CD')
c2 = ContactFactory(company_name='Connect', postcode='EX20 2AB')
c3 = ContactFactory(company_name='Apple', postcode='EX2 05B')
c4 = ContactFactory(company_name='Apple Cake', postcode='EX2 2AB')
c2.set_deleted(user)
c4.set_deleted(user)
index = SearchIndex(ContactIndex())
index.drop_create()
assert 4 == index.update()
# exclude deleted
result, total = index.search('EX2')
assert 2 == total
assert [c3.pk, c1.pk] == [x['pk'] for x in result]
# include deleted
result, total = index.search('EX2', include_deleted=True)
assert 4 == total
assert [c3.pk, c4.pk, c2.pk, c1.pk] == [x['pk'] for x in result]
@pytest.mark.django_db
@pytest.mark.elasticsearch
def test_search_email():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment