相关疑难解决方法(0)

在视图中使用全文搜索 + GIN (Django 1.11)

我需要一些帮助来在 django 视图中使用 GIN 索引为全文搜索构建正确的查询。我有一个相当大的数据库(约 40 万行),需要对其中的 3 个字段进行全文搜索。尝试使用django 文档搜索,这是 GIN 之前的代码。它有效,但需要 6 秒以上的时间来搜索所有字段。接下来,我尝试实现GIN索引以加快搜索速度。已经有很多问题如何构建它。但我的问题是 -使用 GIN 索引进行搜索时,视图查询如何更改?我应该搜索哪些字段?

在 GIN 之前:

models.py

class Product(TimeStampedModel):
    product_id = models.AutoField(primary_key=True)
    shop = models.ForeignKey("Shop", to_field="shop_name")
    brand = models.ForeignKey("Brand", to_field="brand_name")
    title = models.TextField(blank=False, null=False)
    description = models.TextField(blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

视图.py

   
def get_cosmetic(request):
    if request.method == "GET":
        pass
    else:
        search_words = request.POST.get("search")
        search_vectors = (
            SearchVector("title", weight="B")
            + SearchVector("description", weight="C")
            + SearchVector("brand__brand_name", weight="A")
        )

        products = (
            Product.objects.annotate(
                search=search_vectors, rank=SearchRank(search_vectors, search)
            ) …
Run Code Online (Sandbox Code Playgroud)

postgresql gwt-gin django-models django-queryset django-views

7
推荐指数
1
解决办法
3730
查看次数