Example #1
0
def exists_(path):
    """ """
    if ITerm.providedBy(path):
        path_ = path.path
    else:
        path_ = path
    term = ExistsTerm(path_)
    term.unary_operator = OPERATOR.pos
    return term
Example #2
0
    def __init__(self, path, value=EMPTY_VALUE, match_type=None):
        """ """
        super(Term, self).__init__(path, value, match_type)

        self.value = self.ensure_term_value(value)

        if ITerm.providedBy(path):
            self.__merge__(path)
        elif isinstance(path, str):
            self.path = ElementPath.from_el_path(path)
        else:
            self.path = path
Example #3
0
def _prepare_term_or_group(path, value=EMPTY_VALUE):
    """ """
    term_or_group = EMPTY_VALUE
    if IGroupTerm.providedBy(path):
        term_or_group = path
    elif ITerm.providedBy(path):
        term_or_group = path
        if value is not EMPTY_VALUE:
            return term_or_group == value

    elif isinstance(path, str):
        term_or_group = T_(path, value)

    return term_or_group
Example #4
0
def test_expression_or(engine):
    """ """
    term = or_("Task.for.reference", "Patient/PAT-001")
    term.finalize(engine)
    assert ITerm.providedBy(term)
    assert term.arithmetic_operator == OPERATOR.or_
    assert term.path.context.parent.prop_name == "for_fhir"

    term = T_("Patient.name.period.start")

    term = or_(-term, datetime.now().isoformat(timespec="seconds"))
    term.finalize(engine)
    assert term.unary_operator == OPERATOR.neg

    term = T_("Patient.name.period.start")
    group = G_(term <= datetime.now().isoformat(timespec="seconds"))
    group = or_(group)

    group.finalize(engine)

    assert len(group.terms) == 1
    assert group.terms[0]._finalized is True
Example #5
0
def test_expression_add(engine):
    """ """
    term = and_("Patient.name.given", "Krog")
    term.finalize(engine)
    assert ITerm.providedBy(term)
    assert term.arithmetic_operator == OPERATOR.and_
    assert term.path.context.multiple is True

    term = T_("Patient.name.period.start")
    term = and_(-term, datetime.now().isoformat(timespec="seconds"))
    term.finalize(engine)
    assert term.unary_operator == OPERATOR.neg

    term = T_("Patient.name.period.start")
    group = G_(term <= datetime.now().isoformat(timespec="seconds"))
    group = and_(group)

    group.finalize(engine)

    assert IGroupTerm.providedBy(group) is True
    assert len(group.terms) == 1
    assert group.terms[0]._finalized is True
Example #6
0
    def resolve_term(self, term, mapping, root_replacer):
        """ """
        if IGroupTerm.providedBy(term):
            unary_operator = OPERATOR.pos
            if term.type == GroupType.DECOUPLED:

                if term.match_operator == MatchType.ANY:
                    qr = {"bool": {"should": list()}}
                    container = qr["bool"]["should"]
                    qr["bool"]["minimum_should_match"] = 1

                elif term.match_operator == MatchType.ALL:
                    qr = {"bool": {"filter": list()}}
                    container = qr["bool"]["filter"]

                elif term.match_operator == MatchType.NONE:
                    qr = {"bool": {"must_not": list()}}
                    container = qr["bool"]["must_not"]

                alsoProvides(term, IIgnoreNestedCheck)

            elif term.type == GroupType.COUPLED:
                if term.match_operator == MatchType.NONE:
                    qr = {"bool": {"must_not": list()}}
                    container = qr["bool"]["must_not"]
                else:
                    qr = {"bool": {"filter": list()}}
                    container = qr["bool"]["filter"]
            else:
                raise NotImplementedError

            for t_ in term.terms:
                # single term resolver should not look at this
                if term.type == GroupType.COUPLED:
                    alsoProvides(t_, IIgnoreNestedCheck)

                resolved, operator = self.resolve_term(t_, mapping,
                                                       root_replacer)
                if operator == OPERATOR.neg:
                    container.append({"bool": {"must_not": [resolved]}})
                else:
                    container.append(resolved)

            if not IIgnoreNestedCheck.providedBy(term):
                qr = ElasticSearchDialect.attach_nested_on_demand(
                    term.path.context, qr, root_replacer)

            return qr, unary_operator

        elif IInTerm.providedBy(term):

            unary_operator = term.unary_operator
            qr = {"bool": {"should": list()}}
            container = qr["bool"]["should"]

            for t_ in term:
                resolved = self.resolve_term(t_, mapping, root_replacer)
                container.append(resolved[0])
            if len(container) > 0:
                qr["bool"]["minimum_should_match"] = 1
            return qr, unary_operator

        elif IExistsTerm.providedBy(term):
            return ElasticSearchDialect.resolve_exists_term(
                term, root_replacer=root_replacer)

        elif ITerm.providedBy(term):

            if term.path.context.type_is_primitive:

                if term.path.context.type_name in (
                        "string",
                        "xhtml",
                        "uri",
                        "url",
                        "canonical",
                        "code",
                        "oid",
                        "id",
                        "uuid",
                        "boolean",
                ):
                    # xxx: may do something special?
                    multiple = term.path.context.multiple
                    dotted_path = ElasticSearchDialect.create_dotted_path(
                        term, root_replacer)
                    value = term.get_real_value()

                    # FIXME when searching on all resources, we don't have the mapping
                    map_info = (ElasticSearchDialect.get_path_mapping_info(
                        mapping, dotted_path) if mapping else None)

                    if map_info and map_info.get("type", None) == "text":
                        resolved = ElasticSearchDialect.resolve_string_term(
                            term, map_info, root_replacer)

                    else:
                        if term.comparison_operator == OPERATOR.sa:
                            q = ElasticSearchDialect.create_sa_term(
                                dotted_path, value)
                        elif term.comparison_operator == OPERATOR.eb:
                            q = ElasticSearchDialect.create_eb_term(
                                dotted_path, value)
                        elif term.comparison_operator == OPERATOR.contains:
                            q = ElasticSearchDialect.create_contains_term(
                                dotted_path, value)
                        else:
                            # FIXME find a cleaner way to do that
                            # If root_replacer is "*", we're searching on all resources
                            all_resources = root_replacer == "*"
                            q = ElasticSearchDialect.create_term(
                                dotted_path,
                                value,
                                multiple=multiple,
                                match_type=term.match_type,
                                all_resources=all_resources,
                            )
                        resolved = q, term.unary_operator

                elif term.path.context.type_name in (
                        "dateTime",
                        "date",
                        "time",
                        "instant",
                ):

                    resolved = self.resolve_datetime_term(term, root_replacer)

                elif term.path.context.type_name in (
                        "integer",
                        "decimal",
                        "unsignedInt",
                        "positiveInt",
                ):

                    resolved = ElasticSearchDialect.resolve_numeric_term(
                        term, root_replacer)
                else:
                    raise NotImplementedError

                if IIgnoreNestedCheck.providedBy(term):
                    return resolved

                # check for nested
                qr, unary_operator = resolved
                qr = ElasticSearchDialect.attach_nested_on_demand(
                    term.path.context, qr, root_replacer)
                return qr, unary_operator
            else:
                raise NotImplementedError("Line 425")

        elif INonFhirTerm.providedBy(term):
            assert IFhirPrimitiveType.providedBy(term.value)
            return self.resolve_nonfhir_term(term)
Example #7
0
    def _add(self, other):
        """ """
        required_not_finalized(self)
        self.terms.append(ITerm(other))

        return self.clone()
Example #8
0
def exact_(path, value=EMPTY_VALUE):
    """ """
    term = _prepare_term_or_group(path, value=value)
    ITerm(term).set_match_type(TermMatchType.EXACT)
    return term