django - 如何对名称相似的项目进行分组?

标签 django postgresql elasticsearch

问题

堆栈是 Django 1.6、PostgreSQL 9.3 和 Elasticsearch 1.2。在 PostgreSQL 中,我在表中获取了数据(为简洁起见删除了大部分列):

Activity Name                              Location
=============                              ========
Swimming Lessons - Level 1                 Hothouse Pool
Swimming Lessons - Level 2                 Hothouse Pool
Swimming Lessons - Level 3                 Hothouse Pool
Swimming Lessons - Parent & Tot            Trinity Recreation Center
Swimming Lessons - Level 1                 Trinity Recreation Center
Swimming Lessons - Level 2                 Trinity Recreation Center
Swimming Lessons - Level 3                 Trinity Recreation Center
Swimming Lessons - Level 4                 Trinity Recreation Center
Swimming Lessons - Gold Certification      Hothouse Pool
Karate - White Belt                        Trinity Recreation Center
Karate - Yellow Belt                       Trinity Recreation Center
Karate - Black Belt                        Trinity Recreation Center

当用户在自动完成查询框中输入内容时,我希望获得以下类型的结果,因此它需要快速:

  • 温室游泳池游泳课(4 项)
  • Trinity 娱乐中心的游泳课(5 项)
  • Trinity 娱乐中心的空手道(3 项)

如果用户键入 s,我希望显示前 2 个项目。

我的想法

我认为为此利用 Elasticsearch 最有意义,但考虑到上述堆栈,我对其他解决方案持开放态度。一个 ES 完成建议器对此非常出色(而且速度很快!),但我不确定如何获取按位置和相似分组的数据 来自 PostgreSQL 的事件名称,以便对其进行索引。也许我错过了更好的方法?

此时我有点卡住了。

编辑 1:

我认为这种情况很接近(参见 Conditional group by (group similar items) in PostgreSQL ),但它依赖于 name 列的内容相同,而不仅仅是以相似的单词/字符开头。

最佳答案

我还没有试过这个,但是你可以利用像 pg_trgmfuzzystrmatch 模块这样的东西。

看看这个给出的答案,它可能对你有帮助:Finding similar strings with PostgreSQL quickly

关于django - 如何对名称相似的项目进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480322/

相关文章:

python - 我如何在我的 Django 应用程序中模拟一个到达外部服务的组件?

python - 为 Django/Python 3 配置 Azure

django - 需要在 django 模板中将字符串转换为 int

php - 在 Windows 上配置 PHP 和 PostgreSQL

elasticsearch - Elastic搜索中嵌套 bool 查询和非嵌套 bool 查询之间的性能差异

elasticsearch - 如何备份和恢复 ElasticSearch

django - 如何使用JsonResponse将错误消息发送到Django通用DeleteView确认页面

sql - 在postgresql中获取一个 child 的所有 parent

PostgreSQL 连接被拒绝

elasticsearch - Elasticsearch文档数量未反射(reflect)较高的索引编制率