def fieldAlphaHistogram(
        self, name, q='*:*', fq=None, nbins=10, includequeries=True
    ):
        """Generates a histogram of values from a string field. Output is:

        [[low, high, count, query],    ... ] Bin edges is determined by equal division
        of the fields

        """
        oldpersist = self.persistent
        self.persistent = True
        bins = []
        qbin = []
        fvals = []
        try:
            # get total number of values for the field
            # TODO: this is a slow mechanism to retrieve the number of distinct values
            # Need to replace this with something more efficient.
            ## Can probably replace with a range of alpha chars - need to check on
            ## case sensitivity
            fvals = self.fieldValues(name, q, fq, maxvalues=-1)
            nvalues = len(fvals[name]) / 2
            if nvalues < nbins:
                nbins = nvalues
            if nvalues == nbins:
                # Use equivalence instead of range queries to retrieve the values
                for i in range(0, nbins):
                    bin = [fvals[name][i * 2], fvals[name][i * 2], 0]
                    binq = '%s:%s' % (name, self.prepareQueryTerm(name, bin[0]))
                    qbin.append(binq)
                    bins.append(bin)
            else:
                delta = nvalues / nbins
                if delta == 1:
                    # Use equivalence queries, except the last one which includes the
                    # remainder of terms
                    for i in range(0, nbins - 2):
                        bin = [fvals[name][i * 2], fvals[name][i * 2], 0]
                        binq = '%s:%s' % (name, self.prepareQueryTerm(name, bin[0]))
                        qbin.append(binq)
                        bins.append(bin)
                    term = fvals[name][(nbins - 1) * 2]
                    bin = [term, fvals[name][((nvalues - 1) * 2)], 0]
                    binq = '%s:[%s TO *]' % (name, self.prepareQueryTerm(name, term))
                    qbin.append(binq)
                    bins.append(bin)
                else:
                    # Use range for all terms
                    # now need to page through all the values and get those at the edges
                    coffset = 0.0
                    delta = float(nvalues) / float(nbins)
                    for i in range(0, nbins):
                        idxl = int(coffset) * 2
                        idxu = (int(coffset + delta) * 2) - 2
                        bin = [fvals[name][idxl], fvals[name][idxu], 0]
                        # logging.info(str(bin))
                        binq = ''
                        try:
                            if i == 0:
                                binq = '%s:[* TO %s]' % (
                                    name,
                                    self.prepareQueryTerm(name, bin[1]),
                                )
                            elif i == nbins - 1:
                                binq = '%s:[%s TO *]' % (
                                    name,
                                    self.prepareQueryTerm(name, bin[0]),
                                )
                            else:
                                binq = '%s:[%s TO %s]' % (
                                    name,
                                    self.prepareQueryTerm(name, bin[0]),
                                    self.prepareQueryTerm(name, bin[1]),
                                )
                        except Exception:
                            self.logger.exception('Exception 1 in fieldAlphaHistogram:')
                        qbin.append(binq)
                        bins.append(bin)
                        coffset = coffset + delta
            # now execute the facet query request
            params = {
                'q': q,
                'rows': '0',
                'facet': 'true',
                'facet.field': name,
                'facet.limit': '1',
                'facet.mincount': 1,
                'wt': 'python',
            }
            request = urllib.parse.urlencode(params, doseq=True)
            for sq in qbin:
                try:
                    request = request + '&%s' % urllib.parse.urlencode(
                        {'facet.query': self.encoder(sq)[0]}
                    )
                except Exception:
                    self.logger.exception('Exception 2 in fieldAlphaHistogram')
            rsp = self.doPost(self.solrBase + '', request, self.formheaders)
            data = eval(rsp.read())
            for i in range(0, len(bins)):
                v = data['facet_counts']['facet_queries'][qbin[i]]
                bins[i][2] = v
                if includequeries:
                    bins[i].append(qbin[i])
        finally:
            self.persistent = oldpersist
            if not self.persistent:
                self.conn.close()
        return bins
Generates a histogram of values from a string field. Output is:

        [[low, high, count, query],    ... ] Bin edges is determined by equal division
        of the fields