コード例 #1
0
 def get_faceted_filters(self, split=False):
     faceted_filters = dict()
     faceted_fields = self.get_faceted_fields()
     query_params = {to_snake_case(k): v for k, v in self.request.query_params.dict().items()}
     for field in faceted_fields:
         if field in query_params:
             query_value = query_params[field]
             faceted_filters[field] = query_value.split(',') if split else query_value
     return faceted_filters
コード例 #2
0
ファイル: views.py プロジェクト: cens-chile/oclapi2
    def __search_results(self):  # pylint: disable=too-many-branches,too-many-locals
        results = None

        if self.should_perform_es_search():
            results = self.document_model.search()
            default_filters = self.default_filters.copy()
            if self.is_source_child_document_model(
            ) and self.__should_query_latest_version():
                default_filters['is_latest_version'] = True

            for field, value in default_filters.items():
                results = results.query("match", **{field: value})

            faceted_criterion = self.get_faceted_criterion()
            extras_fields = self.get_extras_searchable_fields_from_query_params(
            )
            extras_fields_exact = self.get_extras_exact_fields_from_query_params(
            )
            extras_fields_exists = self.get_extras_fields_exists_from_query_params(
            )

            if faceted_criterion:
                results = results.query(faceted_criterion)

            if self.is_exact_match_on():
                results = results.query(self.get_exact_search_criterion())
            else:
                results = results.query(self.get_wildcard_search_criterion())

            updated_since = parse_updated_since_param(
                self.request.query_params)
            if updated_since:
                results = results.query('range',
                                        last_update={"gte": updated_since})

            if extras_fields:
                for field, value in extras_fields.items():
                    results = results.filter("query_string",
                                             query=value,
                                             fields=[field])
            if extras_fields_exists:
                for field in extras_fields_exists:
                    results = results.query("exists",
                                            field="extras.{}".format(field))
            if extras_fields_exact:
                for field, value in extras_fields_exact.items():
                    results = results.query("match",
                                            **{field: value},
                                            _expand__to_dot=False)

            if self._should_exclude_retired_from_search_results():
                results = results.query('match', retired=False)

            user = self.request.user
            is_authenticated = user.is_authenticated
            username = user.username

            include_private = self._should_include_private()
            if not include_private:
                results = results.query(self.get_public_criteria())

            if self.is_owner_document_model():
                kwargs_filters = self.kwargs.copy()
                if self.user_is_self and is_authenticated:
                    kwargs_filters.pop('user_is_self', None)
                    kwargs_filters['user'] = username
            else:
                kwargs_filters = self.get_kwargs_filters()
                if self.user_is_self and is_authenticated:
                    kwargs_filters['ownerType'] = 'User'
                    kwargs_filters['owner'] = username

            for key, value in kwargs_filters.items():
                results = results.query('match', **{to_snake_case(key): value})

            sort_field = self.get_sort_attr()
            if sort_field:
                results = results.sort(sort_field)

        return results
コード例 #3
0
ファイル: tests.py プロジェクト: jwnasambu/oclapi2
 def test_to_snake_case(self):
     self.assertEqual(to_snake_case(""), "")
     self.assertEqual(to_snake_case("foobar"), "foobar")
     self.assertEqual(to_snake_case("foo_bar"), "foo_bar")
     self.assertEqual(to_snake_case("fooBar"), "foo_bar")
コード例 #4
0
ファイル: views.py プロジェクト: jwnasambu/oclapi2
    def __search_results(self):  # pylint: disable=too-many-branches
        results = None

        if self.should_perform_es_search():
            results = self.document_model.search()
            default_filters = self.default_filters.copy()
            if self.is_source_child_document_model() and 'collection' not in self.kwargs and \
                    'version' not in self.kwargs:
                default_filters['is_latest_version'] = True

            for field, value in default_filters.items():
                results = results.query("match", **{field: value})

            faceted_criterion = self.get_faceted_criterion()
            extras_fields = self.get_extras_searchable_fields_from_query_params(
            )
            extras_fields_exact = self.get_extras_exact_fields_from_query_params(
            )
            extras_fields_exists = self.get_extras_fields_exists_from_query_params(
            )

            if faceted_criterion:
                results = results.query(faceted_criterion)

            if self.is_exact_match_on():
                results = results.query(self.get_exact_search_criterion())
            else:
                results = results.filter(
                    "query_string",
                    query=self.get_wildcard_search_string(),
                    fields=self.get_searchable_fields())

            if extras_fields:
                for field, value in extras_fields.items():
                    results = results.filter("query_string",
                                             query=value,
                                             fields=[field])
            if extras_fields_exists:
                for field in extras_fields_exists:
                    results = results.query("exists",
                                            field="extras.{}".format(field))
            if extras_fields_exact:
                for field, value in extras_fields_exact.items():
                    results = results.query("match", **{field: value})

            if self._should_exclude_retired_from_search_results():
                results = results.query('match', retired=False)

            include_private = self._should_include_private()

            if not include_private:
                from core.orgs.documents import OrganizationDocument
                if self.document_model in [
                        OrganizationDocument
                ] and self.request.user.is_authenticated:
                    results.query(
                        Q('match', public_can_view=False)
                        & Q('match', user=self.request.user.username))
                elif self.is_concept_container_document_model(
                ) and self.request.user.is_authenticated:
                    results.query(
                        Q('match', public_can_view=False)
                        & Q('match', created_by=self.request.user.username))

            if self.is_owner_document_model():
                kwargs_filters = self.kwargs.copy()
                if self.user_is_self and self.request.user.is_authenticated:
                    kwargs_filters.pop('user_is_self', None)
                    kwargs_filters['user'] = self.request.user.username
            else:
                kwargs_filters = self.get_kwargs_filters()
                if self.user_is_self and self.request.user.is_authenticated:
                    kwargs_filters['ownerType'] = 'User'
                    kwargs_filters['owner'] = self.request.user.username

            for key, value in kwargs_filters.items():
                results = results.query('match', **{to_snake_case(key): value})

            sort_field = self.get_sort_attr()
            if sort_field:
                results = results.sort(sort_field)

        return results