Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数组fileld postgres中最常见的单词_Python_Django_Postgresql_Counter_Aggregate Functions - Fatal编程技术网

Python 数组fileld postgres中最常见的单词

Python 数组fileld postgres中最常见的单词,python,django,postgresql,counter,aggregate-functions,Python,Django,Postgresql,Counter,Aggregate Functions,我有一个模型,里面有一个数组字段。此字段包含每个实体的一些单词。模型如下: class Entity(models.Model): words = ArrayField( models.CharField(max_length=255, blank=True), null=True, blank=True ) 我想积累这个模型的所有对象的列表,并找到这些数组中出现的100个最常见的单词。在Postgresql中有什么方法可以做到这

我有一个模型,里面有一个数组字段。此字段包含每个实体的一些单词。模型如下:

class Entity(models.Model):
    words = ArrayField(
        models.CharField(max_length=255, blank=True),
        null=True,
        blank=True
    )

我想积累这个模型的所有对象的列表,并找到这些数组中出现的100个最常见的单词。在Postgresql中有什么方法可以做到这一点,或者有效的方法是什么?

如果近似答案足够好,并且表格统计数据相当准确,您可以使用Postgresql收集的数组元素统计数据来解决您的问题

假设表名为
public.entity
,属性为
words
,此查询将生成所需的结果:

SELECT mce.elem
FROM pg_stats s
   CROSS JOIN LATERAL
      unnest(most_common_elems::text::text[],
             most_common_elem_freqs) mce(elem, freq)
WHERE s.schemaname = 'public'
  AND s.tablename = 'entity'
  AND s.attname = 'words'
  AND mce.elem IS NOT NULL
ORDER BY mce.freq DESC
LIMIT 100;

Django中是否有解决方案,而不是执行原始查询?我不知道Django,但我怀疑没有什么东西不能读取整个表并计算所有内容。