Example #1
0
    def _get_expression(self, params):
        values = self._get_values_from_params(params.get(self.alias, {}))
        if not values:
            return None

        if len(values) == 1:
            return self.field == values[0]

        if self._conj_operator == QueryFilter.CONJ_AND:
            return Bool.must(*(self.field == v for v in values))
        else:
            return self.field.in_(values)
Example #2
0
    def _get_expression(self, params):
        values = params.get(self.alias, {}).get('exact')
        if not values:
            if self.default:
                values = [[self.default]]
        if not values:
            return None

        expressions = []
        for v in values:
            w = v[0]
            filter_value = self.get_value(w)
            if filter_value and not isinstance(filter_value.expr, MatchAll):
                expressions.append(filter_value.expr)

        if not expressions:
            return None

        if self._conj_operator == QueryFilter.CONJ_AND:
            return Bool.must(*expressions)
        else:
            return Bool.should(*expressions)
def test_weight():
    assert Weight(3).to_elastic() == {"weight": 3}
    assert Weight(2, filter=Bool.must(
        PostDocument.status.in_([0, 1]),
        PostDocument.created_date >= 'now/d-7d')
    ).to_elastic() == {
        "weight": 2,
        "filter": {
            "bool": {
                "must": [
                    {"terms": {"status": [0, 1]}},
                    {"range": {"created_date": {"gte": "now/d-7d"}}}
                ]
            }
        }
    }
Example #4
0
    def _apply_agg(self, search_query):
        exclude_tags = {self.qf._name}
        if self._conj_operator == QueryFilter.CONJ_OR:
            exclude_tags.add(self.name)
        filters = self._get_agg_filters(
            search_query.iter_post_filters_with_meta(), exclude_tags
        )

        terms_agg = agg.Terms(self.field, instance_mapper=self._instance_mapper, **self._agg_kwargs)
        if filters:
            aggs = {
                self._filter_agg_name: agg.Filter(
                    Bool.must(*filters), aggs={self._agg_name: terms_agg}
                )
            }
        else:
            aggs = {self._agg_name: terms_agg}

        return search_query.aggregations(**aggs)
Example #5
0
    def _apply_agg(self, search_query):
        exclude_tags = {self.qf._name}
        if self._conj_operator == QueryFilter.CONJ_OR:
            exclude_tags.add(self.name)
        filters = self._get_agg_filters(
            search_query.iter_post_filters_with_meta(), exclude_tags
        )

        filter_aggs = {}
        for fv in self.values:
            filter_aggs[self._make_agg_name(fv.value)] = agg.Filter(fv.expr, **self.agg_kwargs)

        if filters:
            aggs = {
                self._filter_agg_name: agg.Filter(
                    Bool.must(*filters), aggs=filter_aggs
                )
            }
        else:
            aggs = filter_aggs

        return search_query.aggregations(**aggs)
Example #6
0
    def _apply_agg(self, search_query):
        filters = self._get_agg_filters(
            search_query.iter_post_filters_with_meta(), {self.qf._name, self.name}
        )

        aggs = {}
        if self._compute_enabled:
            aggs.update({
                self._enabled_agg_name: agg.Filter(self.field != None),
            })

        if self._compute_min_max:
            stat_aggs = {
                self._min_agg_name: agg.Min(self.field),
                self._max_agg_name: agg.Max(self.field),
            }
            if filters:
                aggs.update({
                    self._filter_agg_name: agg.Filter(Bool.must(*filters), aggs=stat_aggs)
                })
            else:
                aggs.update(stat_aggs)

        return search_query.aggregations(**aggs)