问题
堆栈是 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_trgm
或 fuzzystrmatch
模块这样的东西。
看看这个给出的答案,它可能对你有帮助:Finding similar strings with PostgreSQL quickly
关于django - 如何对名称相似的项目进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480322/