使用PostgreSQL索引的Django全文搜索

时间:2018-02-12 15:03:13

标签: python django postgresql full-text-search full-text-indexing

解决了我在this question中询问的问题后,我试图使用索引优化FTS的性能。 我在db上发出了命令:

CREATE INDEX my_table_idx ON my_table USING gin(to_tsvector('italian', very_important_field), to_tsvector('italian', also_important_field), to_tsvector('italian', not_so_important_field), to_tsvector('italian', not_important_field), to_tsvector('italian', tags));

然后我按如下方式编辑了模型的Meta类:

class MyEntry(models.Model):
    very_important_field = models.TextField(blank=True, null=True)
    also_important_field = models.TextField(blank=True, null=True)
    not_so_important_field = models.TextField(blank=True, null=True)
    not_important_field = models.TextField(blank=True, null=True)
    tags = models.TextField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'my_table'
        indexes = [
            GinIndex(
                fields=['very_important_field', 'also_important_field', 'not_so_important_field', 'not_important_field', 'tags'],
                name='my_table_idx'
            )
        ]

但似乎没有任何改变。查找所用的时间与以前完全相同。

这是查找脚本:

from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector

# other unrelated stuff here
vector = SearchVector("very_important_field", weight="A") + \
             SearchVector("tags", weight="A") + \
             SearchVector("also_important_field", weight="B") + \
             SearchVector("not_so_important_field", weight="C") + \
             SearchVector("not_important_field", weight="D")
query = SearchQuery(search_string, config="italian")
rank = SearchRank(vector, query, weights=[0.4, 0.6, 0.8, 1.0]). # D, C, B, A
full_text_search_qs = MyEntry.objects.annotate(rank=rank).filter(rank__gte=0.4).order_by("-rank")

我做错了什么?

编辑:

上面的查找包含在我使用装饰器的函数中。该函数实际返回一个列表,如下所示:

@timeit
def search(search_string):
    # the above code here
    qs = list(full_text_search_qs)
    return qs

可能这可能是问题吗?

1 个答案:

答案 0 :(得分:0)

我不确定但是根据postgresql文档(https://www.postgresql.org/docs/9.5/static/textsearch-tables.html#TEXTSEARCH-TABLES-INDEX):

  

因为在索引中使用了to_tsvector的双参数版本   上面,只有使用2参数版本的查询引用   具有相同配置名称的to_tsvector将使用该索引。那   是,在哪里to_tsvector('英语',身体)@@' a& B'可以使用索引,   但是在哪里to_tsvector(body)@@' a& B'不能。这确保了   index将仅与用于创建的相同配置一起使用   索引条目。

我不知道django使用的配置,但您可以尝试删除第一个参数