Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#2023 | include retired filter
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Dec 10, 2024
1 parent 97e23fe commit 203d9ab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
9 changes: 6 additions & 3 deletions core/concepts/search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from elasticsearch_dsl import TermsFacet, Q
from pydash import flatten
from pydash import flatten, is_number

from core.common.constants import FACET_SIZE
from core.common.search import CustomESFacetedSearch, CustomESSearch
Expand Down Expand Up @@ -79,12 +79,14 @@ def get_exact_and_contains_criteria(field, value, boost=0):
}))

@classmethod
def search(cls, data, repo_url): # pylint: disable=too-many-locals, too-many-branches
def search(cls, data, repo_url, include_retired=False): # pylint: disable=too-many-locals, too-many-branches
from core.concepts.documents import ConceptDocument
search = ConceptDocument.search()
repo_params = cls.get_target_repo_params(repo_url)
for field, value in repo_params.items():
search = search.query('match', **{field: value})
if not include_retired:
search = search.query('match', retired=False)
for field in cls.filter_fields:
value = data.get(field, None)
if value:
Expand Down Expand Up @@ -118,7 +120,8 @@ def search(cls, data, repo_url): # pylint: disable=too-many-locals, too-many-br
criterion = criteria if criterion is None else criterion | criteria
if criterion is not None:
search = search.query(criterion)
search = search.highlight(*flatten([*cls.fuzzy_fields, *cls.priority_fields]))
highlight = [field for field in flatten([*cls.fuzzy_fields, *cls.priority_fields]) if not is_number(field)]
search = search.highlight(*highlight)
return search.sort({'_score': {'order': 'desc'}})

@classmethod
Expand Down
9 changes: 6 additions & 3 deletions core/concepts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,23 @@ def get_references(self, obj):
return None

def get_mappings(self, obj):
from core.mappings.serializers import MappingDetailSerializer
from core.mappings.serializers import MappingDetailSerializer, MappingMinimalSerializer
context = get(self, 'context')
request = get(context, 'request')
is_mapping_brief = request.query_params.get('mappingBrief', None) in TRUTHY
serializer_class = MappingMinimalSerializer if is_mapping_brief else MappingDetailSerializer
is_collection = 'collection' in self.view_kwargs
collection_version = self.view_kwargs.get('version', HEAD) if is_collection else None
parent_uri = to_parent_uri_from_kwargs(self.view_kwargs) if is_collection else None
if self.include_indirect_mappings:
mappings = obj.get_bidirectional_mappings_for_collection(
parent_uri, collection_version
) if is_collection else obj.get_bidirectional_mappings()
return MappingDetailSerializer(mappings, many=True, context=context).data
return serializer_class(mappings, many=True, context=context).data
if self.include_direct_mappings:
mappings = obj.get_unidirectional_mappings_for_collection(
parent_uri, collection_version) if is_collection else obj.get_unidirectional_mappings()
return MappingDetailSerializer(mappings, many=True, context=context).data
return serializer_class(mappings, many=True, context=context).data

return []

Expand Down
3 changes: 2 additions & 1 deletion core/concepts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ def get_serializer_class(self):
def filter_queryset(self, _=None):
row = self.request.data.get('row')
target_repo_url = self.request.data.get('target_repo_url')
include_retired = self.request.query_params.get(INCLUDE_RETIRED_PARAM) in get_truthy_values()
if not row or not target_repo_url:
raise Http400()
offset = max(to_int(self.request.GET.get('offset'), 0), 0)
Expand All @@ -770,7 +771,7 @@ def filter_queryset(self, _=None):
start = offset or (page - 1) * self.limit
end = start + self.limit

search = ConceptFuzzySearch.search(row, target_repo_url)
search = ConceptFuzzySearch.search(row, target_repo_url, include_retired)
es_search = CustomESSearch(search[start:end], ConceptDocument)
es_search.to_queryset()
self.total_count = es_search.total - offset
Expand Down

0 comments on commit 203d9ab

Please sign in to comment.