Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1583 | Fuzzy search is not applied on id an…
Browse files Browse the repository at this point in the history
…d codes
  • Loading branch information
snyaggarwal committed Jul 31, 2023
1 parent e92387f commit 9b4659d
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 11 deletions.
12 changes: 11 additions & 1 deletion core/collections/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_exact_match_attrs():
}

@staticmethod
def get_boostable_search_attrs():
def get_wildcard_search_attrs():
return {
'mnemonic': {
'boost': 1,
Expand All @@ -80,6 +80,16 @@ def get_boostable_search_attrs():
}
}

@staticmethod
def get_fuzzy_search_attrs():
return {
'name': {
'boost': 0.8,
'lower': True,
'wildcard': True
},
}

@staticmethod
def prepare_locale(instance):
return get(instance.supported_locales, [])
Expand Down
9 changes: 6 additions & 3 deletions core/collections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,14 @@ def clean_fields(_fields):
val, document.get_match_phrase_attrs(), clean_fields(document.get_exact_match_attrs())
))
if include_wildcard or include_fuzzy:
fields = clean_fields(document.get_boostable_search_attrs())
if include_wildcard:
search = search.query(CustomESSearch.get_wildcard_match_criterion(val, fields))
search = search.query(
CustomESSearch.get_wildcard_match_criterion(val, clean_fields(document.get_wildcard_search_attrs()))
)
if include_fuzzy:
search = search.query(CustomESSearch.get_fuzzy_match_criterion(val, fields, 10000, 2))
search = search.query(
CustomESSearch.get_fuzzy_match_criterion(val, document.get_fuzzy_search_attrs(), 10000, 2)
)
return search

def apply_filters(self, queryset, resource_klass):
Expand Down
7 changes: 5 additions & 2 deletions core/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def get_sort_attributes(self):
def get_fuzzy_search_criterion(self, boost_divide_by=10, expansions=5):
return CustomESSearch.get_fuzzy_match_criterion(
search_str=self.get_search_string(decode=False),
fields=self.get_wildcard_search_fields(),
fields=self.get_fuzzy_search_fields(),
boost_divide_by=boost_divide_by,
expansions=expansions
)
Expand Down Expand Up @@ -675,7 +675,10 @@ def _get_sort_attribute(self):
return self.get_sort_attributes() or [{'_score': {'order': 'desc'}}]

def get_wildcard_search_fields(self):
return self.clean_fields(self.document_model.get_boostable_search_attrs() or {})
return self.clean_fields(self.document_model.get_wildcard_search_attrs() or {})

def get_fuzzy_search_fields(self):
return self.document_model.get_fuzzy_search_attrs() or {}

def __get_queryset_from_search_results(self, search_results):
offset = max(to_int(self.request.GET.get('offset'), 0), 0)
Expand Down
13 changes: 12 additions & 1 deletion core/concepts/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_exact_match_attrs():
}

@staticmethod
def get_boostable_search_attrs():
def get_wildcard_search_attrs():
return {
'id': {
'boost': 25
Expand All @@ -94,6 +94,17 @@ def get_boostable_search_attrs():
},
}

@staticmethod
def get_fuzzy_search_attrs():
return {
'name': {
'boost': 23
},
'synonyms': {
'boost': 0.3,
},
}

@staticmethod
def prepare_numeric_id(instance):
if len(instance.mnemonic) > 19: # long (-9223372036854775808 - 9223372036854775807)
Expand Down
13 changes: 12 additions & 1 deletion core/mappings/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_exact_match_attrs():
}

@staticmethod
def get_boostable_search_attrs():
def get_wildcard_search_attrs():
return {
'id': {
'boost': 1
Expand All @@ -79,6 +79,17 @@ def get_boostable_search_attrs():
}
}

@staticmethod
def get_fuzzy_search_attrs():
return {
'from_concept': {
'boost': 0.8,
},
'to_concept': {
'boost': 0.6,
}
}

@staticmethod
def prepare_from_concept(instance):
from_concept_name = get(instance, 'from_concept_name') or get(instance, 'from_concept.display_name')
Expand Down
10 changes: 9 additions & 1 deletion core/orgs/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_exact_match_attrs():
}

@staticmethod
def get_boostable_search_attrs():
def get_wildcard_search_attrs():
return {
'mnemonic': {
'boost': 1,
Expand All @@ -59,6 +59,14 @@ def get_boostable_search_attrs():
}
}

@staticmethod
def get_fuzzy_search_attrs():
return {
'name': {
'boost': 0.8,
}
}

@staticmethod
def prepare_extras(instance):
value = {}
Expand Down
10 changes: 9 additions & 1 deletion core/sources/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_exact_match_attrs():
}

@staticmethod
def get_boostable_search_attrs():
def get_wildcard_search_attrs():
return {
'mnemonic': {
'boost': 1,
Expand All @@ -86,6 +86,14 @@ def get_boostable_search_attrs():
}
}

@staticmethod
def get_fuzzy_search_attrs():
return {
'name': {
'boost': 0.8,
},
}

@staticmethod
def prepare_locale(instance):
return get(instance.supported_locales, [])
Expand Down
10 changes: 9 additions & 1 deletion core/users/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_exact_match_attrs():
}

@staticmethod
def get_boostable_search_attrs():
def get_wildcard_search_attrs():
return {
'username': {
'boost': 1,
Expand All @@ -61,6 +61,14 @@ def get_boostable_search_attrs():
}
}

@staticmethod
def get_fuzzy_search_attrs():
return {
'name': {
'boost': 0.8,
}
}

@staticmethod
def prepare_extras(instance):
value = {}
Expand Down

0 comments on commit 9b4659d

Please sign in to comment.