'Uses exec_js to execute'

def _item_frequencies_exec_js(self, field, normalize=False):

    freq_func = "\n                                                function(path)    {\n                                                                var    path    =    path.split('.');\n\n                                                                var    total    =    0.0;\n                                                                db[collection].find(query).forEach(function(doc)    {\n                                                                                var    field    =    doc;\n                                                                                for    (p    in    path)    {\n                                                                                                if    (field)\n                                                                                                                field    =    field[path[p]];\n                                                                                                    else\n                                                                                                                break;\n                                                                                }\n                                                                                if    (field    &&    field.constructor    ==    Array)    {\n                                                                                            total    +=    field.length;\n                                                                                }    else    {\n                                                                                            total++;\n                                                                                }\n                                                                });\n\n                                                                var    frequencies    =    {};\n                                                                var    types    =    {};\n                                                                var    inc    =    1.0;\n\n                                                                db[collection].find(query).forEach(function(doc)    {\n                                                                                field    =    doc;\n                                                                                for    (p    in    path)    {\n                                                                                                if    (field)\n                                                                                                                field    =    field[path[p]];\n                                                                                                else\n                                                                                                                break;\n                                                                                }\n                                                                                if    (field    &&    field.constructor    ==    Array)    {\n                                                                                                field.forEach(function(item)    {\n                                                                                                                frequencies[item]    =    inc    +    (isNaN(frequencies[item])    ?    0:    frequencies[item]);\n                                                                                                });\n                                                                                }    else    {\n                                                                                                var    item    =    field;\n                                                                                                types[item]    =    item;\n                                                                                                frequencies[item]    =    inc    +    (isNaN(frequencies[item])    ?    0:    frequencies[item]);\n                                                                                }\n                                                                });\n                                                                return    [total,    frequencies,    types];\n                                                }\n                                "
    (total, data, types) = self.exec_js(freq_func, field)
    values = {types.get(k): int(v) for (k, v) in data.iteritems()}
    if normalize:
        values = {k: (float(v) / total) for (k, v) in values.items()}
    frequencies = {}
    for (k, v) in values.iteritems():
        if isinstance(k, float):
            if (int(k) == k):
                k = int(k)
        frequencies[k] = v
    return frequencies
