-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
129 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.db.models.expressions import Func | ||
|
||
|
||
class JsonbArrayElements(Func): | ||
"""https://forum.djangoproject.com/t/django-4-2-behavior-change-when-using-arrayagg-on-unnested-arrayfield-postgresql-specific/21547/5""" | ||
|
||
arity = 1 | ||
contains_subquery = True | ||
function = "JSONB_ARRAY_ELEMENTS" | ||
|
||
|
||
class LevenshteinLessEqual(Func): | ||
arity = 3 | ||
function = "LEVENSHTEIN_LESS_EQUAL" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from django.db import models | ||
|
||
from arches_lingo.query_utils import LevenshteinLessEqual | ||
|
||
|
||
class LabelValueQuerySet(models.QuerySet): | ||
|
||
def fuzzy_search(self, term, max_edit_distance): | ||
from arches_lingo.models import VwLabelValue | ||
|
||
return ( | ||
VwLabelValue.objects.all() | ||
.annotate( | ||
edit_distance=LevenshteinLessEqual( | ||
models.F("value"), | ||
models.Value(term), | ||
models.Value(max_edit_distance), | ||
output_field=models.FloatField(), | ||
) | ||
) | ||
.filter(edit_distance__lte=max_edit_distance) | ||
.order_by("edit_distance") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from django.core.paginator import Paginator | ||
from django.utils.decorators import method_decorator | ||
from django.views.generic import View | ||
|
||
from arches.app.models.system_settings import settings | ||
from arches.app.utils.decorators import group_required | ||
from arches.app.utils.response import JSONResponse | ||
|
||
from arches_lingo.models import VwLabelValue | ||
from arches_lingo.concepts import ConceptBuilder | ||
|
||
|
||
@method_decorator( | ||
group_required("RDM Administrator", raise_exception=True), name="dispatch" | ||
) | ||
class ConceptTreeView(View): | ||
def get(self, request): | ||
builder = ConceptBuilder() | ||
data = { | ||
"schemes": [builder.serialize_scheme(scheme) for scheme in builder.schemes] | ||
} | ||
return JSONResponse(data) | ||
|
||
|
||
@method_decorator( | ||
group_required("RDM Administrator", raise_exception=True), name="dispatch" | ||
) | ||
class ValueSearchView(ConceptTreeView): | ||
def get(self, request): | ||
term = request.GET.get("term") | ||
max_edit_distance = int( | ||
request.GET.get("maxEditDistance", self.default_sensitivity()) | ||
) | ||
page_number = request.GET.get("page", 1) | ||
items_per_page = request.GET.get("items", 25) | ||
|
||
if term: | ||
concept_query = VwLabelValue.objects.fuzzy_search(term, max_edit_distance) | ||
else: | ||
concept_query = VwLabelValue.objects.all().order_by("concept_id") | ||
concept_query = concept_query.values_list("concept_id", flat=True).distinct() | ||
|
||
paginator = Paginator(concept_query, items_per_page) | ||
page = paginator.get_page(page_number) | ||
|
||
data = [] | ||
if page: | ||
builder = ConceptBuilder() | ||
data = [ | ||
builder.serialize_concept( | ||
str(concept_uuid), parents=True, children=False | ||
) | ||
for concept_uuid in page | ||
] | ||
|
||
return JSONResponse(data) | ||
|
||
@staticmethod | ||
def default_sensitivity(): | ||
"""Remains to be seen whether the existing elastic sensitivity setting | ||
should be the fallback, but stub something out for now. | ||
This sensitivity setting is actually inversely related to edit distance, | ||
because it's prefix_length in elastic, not fuzziness, so invert it. | ||
""" | ||
elastic_prefix_length = settings.SEARCH_TERM_SENSITIVITY | ||
if elastic_prefix_length <= 0: | ||
return 5 | ||
if elastic_prefix_length >= 5: | ||
return 0 | ||
return 5 - elastic_prefix_length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters