예제 #1
0
def VideohubChannel(included_ids=None, excluded_ids=None):
    f = MatchAll()
    if included_ids:
        f &= Nested(path="videohub_ref",
                    filter=Terms(**{"videohub_ref.channel_id": included_ids}))
    if excluded_ids:
        f &= ~Nested(path="videohub_ref",
                     filter=Terms(**{"videohub_ref.channel_id": excluded_ids}))
예제 #2
0
def Tags(slugs):  # noqa
    included, excluded = _parse_slugs(slugs)

    f = MatchAll()
    if included:
        f &= Nested(path="tags", filter=Terms(**{"tags.slug": included}))
    if excluded:
        f &= ~Nested(path="tags", filter=Terms(**{"tags.slug": excluded}))

    return f
예제 #3
0
def FeatureTypes(slugs):  # noqa
    included, excluded = _parse_slugs(slugs)

    f = MatchAll()
    if included:
        f &= Nested(path="feature_type",
                    filter=Terms(**{"feature_type.slug": included}))
    if excluded:
        f &= ~Nested(path="feature_type",
                     filter=Terms(**{"feature_type.slug": excluded}))

    return f
예제 #4
0
def groups_filter_from_query(query, field_map={}):
    """Creates an F object for the groups of a search query."""
    f = None
    # filter groups
    for group in query.get("groups", []):
        group_f = MatchAll()
        for condition in group.get("conditions", []):
            field_name = condition["field"]
            field_name = field_map.get(field_name, field_name)
            operation = condition["type"]
            values = condition["values"]
            if values:
                values = [v["value"] for v in values]
                if operation == "all":
                    # NOTE: is there a better way to express this?
                    for value in values:
                        if "." in field_name:
                            path = field_name.split(".")[0]
                            group_f &= Nested(
                                path=path, filter=Term(**{field_name: value}))
                        else:
                            group_f &= Term(**{field_name: value})
                elif operation == "any":
                    if "." in field_name:
                        path = field_name.split(".")[0]
                        group_f &= Nested(path=path,
                                          filter=Terms(**{field_name: values}))
                    else:
                        group_f &= Terms(**{field_name: values})
                elif operation == "none":
                    if "." in field_name:
                        path = field_name.split(".")[0]
                        group_f &= ~Nested(
                            path=path, filter=Terms(**{field_name: values}))
                    else:
                        group_f &= ~Terms(**{field_name: values})

        date_range = group.get("time")
        if date_range:
            group_f &= date_range_filter(date_range)
        if f:
            f |= group_f
        else:
            f = group_f
    return f
예제 #5
0
def TagBoost(slugs, boost_mode="multiply", weight=5):
    included, excluded = _parse_slugs(slugs)
    return FunctionScore(boost_mode=boost_mode,
                         functions=[{
                             "filter":
                             Nested(path="tags",
                                    filter=Terms(**{"tags.slug": included})),
                             "weight":
                             weight
                         }])
예제 #6
0
def get_condition_filter(condition, field_map={}):
    """
    Return the appropriate filter for a given group condition.

    #  TODO: integrate this into groups_filter_from_query function.
    """
    field_name = condition.get("field")
    field_name = field_map.get(field_name, field_name)
    operation = condition["type"]
    values = condition["values"]

    condition_filter = MatchAll()

    if values:
        values = [v["value"] for v in values]
        if operation == "all":
            for value in values:
                if "." in field_name:
                    path = field_name.split(".")[0]
                    condition_filter &= Nested(
                        path=path, filter=Term(**{field_name: value}))
                else:
                    condition_filter &= Term(**{field_name: value})
        elif operation == "any":
            if "." in field_name:
                path = field_name.split(".")[0]
                condition_filter &= Nested(
                    path=path, filter=Terms(**{field_name: values}))
            else:
                condition_filter &= Terms(**{field_name: values})
        elif operation == "none":
            if "." in field_name:
                path = field_name.split(".")[0]
                condition_filter &= ~Nested(
                    path=path, filter=Terms(**{field_name: values}))
            else:
                condition_filter &= ~Terms(**{field_name: values})
        else:
            raise ValueError(
                """ES conditions must be one of the following values: ['all', 'any', 'none']"""
            )
    return condition_filter
예제 #7
0
def InstantArticle(active=True):  # noqa
    return Nested(path="feature_type",
                  filter=Term(**{"feature_type.instant_article": active}))
예제 #8
0
def VideohubVideo():
    return Nested(path='videohub_ref', filter=Exists(field='videohub_ref.id'))