def add_json_query_facet(self, facet_name, q, facets, **kwargs):
        """
        Adds a json facet of type query.

        :param facet_name: the name of the facet. It will be used to generate
            the final field name for the buckets, joining it each facet
            name (facets). It doesn't need to be any model field.
        :param facets: each of the facets to be calculated. A field name and
            the aggregation function to be applied to any of the fields of
            the model. Ex. {"avg_age": "avg(age)", "min_age": "min(age)"}
            You can find here the avaialble functions:
            http://yonik.com/solr-facet-functions/
        :param kwargs: more parameters to be added to the facet definition.
            These parameters could be any of the available here
            http://yonik.com/json-facet-api/. Ex. offset/limit for pagination
            mincount, sort, missing, numBuckets, allbuckets,
        """
        details = {
            "type": "query",
            "facet": facets,
        }

        if not q:
            raise FacetingError(
                "The JSON Facet of type ('query') needs " "a ('q') parameter in kwargs with a valid" " solr query"
            )
        else:
            details["q"] = q

        details.update(kwargs)

        self.json_facets[facet_name] = details
예제 #2
0
 def add_date_facet(self, field, start_date, end_date, gap_by, gap_amount=1):
     """Adds a date-based facet on a field."""
     if not gap_by in VALID_GAPS:
         raise FacetingError("The gap_by ('%s') must be one of the following: %s." % (gap_by, ', '.join(VALID_GAPS)))
     
     details = {
         'start_date': start_date,
         'end_date': end_date,
         'gap_by': gap_by,
         'gap_amount': gap_amount,
     }
     self.date_facets[self.backend.site.get_facet_field_name(field)] = details
예제 #3
0
    def add_date_facet(self, field, start_date, end_date, gap_by, gap_amount=1):
        """Adds a date-based facet on a field."""
        from haystack import connections
        if not gap_by in VALID_GAPS:
            raise FacetingError("The gap_by ('%s') must be one of the following: %s." % (gap_by, ', '.join(VALID_GAPS)))

        details = {
            'start_date': start_date,
            'end_date': end_date,
            'gap_by': gap_by,
            'gap_amount': gap_amount,
        }
        self.date_facets[connections[self._using].get_unified_index().get_facet_fieldname(field)] = details
    def add_json_terms_facet(self, facet_name, field, facets, **kwargs):
        """
        Adds a json facet of type terms.

        :param facet_name: the name of the facet. It will be used to generate
            the final field name for the buckets, joining it each facet
            name (facets). It doesn't need to be any model field.
        :param facets: each of the facets to be calculated. A field name and
            the aggregation function to be applied to any of the fields of
            the model. Ex. {"avg_age": "avg(age)", "min_age": "min(age)"}
            You can find here the avaialble functions:
            http://yonik.com/solr-facet-functions/
        :param kwargs: more parameters to be added to the facet definition.
            These parameters could be any of the available here
            http://yonik.com/json-facet-api/. Ex. offset/limit for pagination
            mincount, sort, missing, numBuckets, allbuckets,
        """
        from haystack import connections

        details = {
            "type": "terms",
            "facet": facets,
        }

        if not field:
            raise FacetingError(
                "The JSON Facet of type ('terms') needs "
                "a ('field') parameter in kwargs pointing to a valid"
                " model field name"
            )
        else:
            details["field"] = connections[self._using].get_unified_index().get_facet_fieldname(field)

        details.update(kwargs)

        self.json_facets[facet_name] = details