【问题标题】:Django and PostgreSQL Full-Text Search: Some terms not found by search lookupDjango 和 PostgreSQL 全文搜索:搜索查找未找到的一些术语
【发布时间】:2018-07-16 08:29:40
【问题描述】:

我正在使用 Django 2.0 和 postgres (PostgreSQL) 9.6.1

我有以下带有标题和正文文本的模型:

class Entry(models.Model):
    headline = models.CharField(max_length=255)
    body_text = models.TextField()

    def __str__(self):
        return self.headline

以下是我的内容

headline: cheese making

body_text:

The simplest way to use full text search is to search a single term against a single column in the database. For example: >>> Entry.objects.filter(body_text__search='Cheese') [<Entry: Cheese on Toast recipes>, <Entry: Pizza Recipes>]. This creates a to_tsvector in the database from the body_text field and a plainto_tsquery ...

以下搜索结果使用search lookup。我在 INSTALLED_APPS 中添加了 'django.contrib.postgres'。

Case 1: Works
In [1]: Entry.objects.filter(body_text__search='Cheese')
Out[1]: <QuerySet [<Entry: cheese making>]>

Case 2: Not working
In [2]: Entry.objects.filter(body_text__search='Pizza')
Out[2]: <QuerySet []>
(the word Pizza is there in the body_text still is not searching)

Case 3: Not working
In [3]: Entry.objects.filter(body_text__search='vector')
Out[3]: <QuerySet []>
(the word vector is there in to_tsvector

Case 4: Not working
In [9]: Entry.objects.filter(body_text__search='Entry')
Out[9]: <QuerySet []>

Case 5: Not working
In [10]: Entry.objects.filter(body_text__search='data')
Out[10]: <QuerySet []>

如何搜索不起作用的术语。

【问题讨论】:

    标签: django postgresql full-text-search


    【解决方案1】:

    我们在工作中的一些项目中使用了 django 中的 postgresql 全文搜索模块,我认为全文搜索正在从您的条目的 body_text 中删除 html 标签,并且它会删除 &lt;Entry: Cheese on Toast recipes&gt;, &lt;Entry: Pizza Recipes&gt;,因为 &lt;&gt;

    我尝试在您的示例中应用to_tsvector,有&lt;&gt;,没有它们,结果向量不同:

    SQL Fiddle

    SELECT to_tsvector('The simplest way to use full text search is to search a single term against a single column in the database. For example: >>> Entry.objects.filter(body_text__search=''Cheese'') [<Entry: Cheese on Toast recipes>, <Entry: Pizza Recipes>]. This creates a to_tsvector in the database from the body_text field and a plainto_tsquery ...');
    

    'bodi':25,39 'chees':28 'column':18 'creat':30 'databas':21,36 'entry.objects.filter':24 'exampl':23 'field':41 'full':6 'plainto':44 '搜索':8,11,27 '最简单':2 'singl':13,17 '术语':14 '文本':7,26,40 'tsqueri':45 'tsvector':33 '使用':5 '方式':3

    SELECT to_tsvector('The simplest way to use full text search is to search a single term against a single column in the database. For example: >>> Entry.objects.filter(body_text__search=''Cheese'') [Entry: Cheese on Toast recipes, Entry: Pizza Recipes]. This creates a to_tsvector in the database from the body_text field and a plainto_tsquery ...');
    

    'bodi':25,47 'chees':28,30 'column':18 'creat':38 'databas':21,44 'entri':29,34 'entry.objects.filter':24 'exampl':23 'field':49 'full':6 'pizza':35 'plainto':52 'recip':33,36 'search':8,11,27 '最简单':2 'singl':13,17 'term':14 'text':7,26,48 'toast':32 'tsqueri':53 'tsvector':41 '使用':5 '方式':3

    因此请尝试从您的body_text 中删除&lt;&gt;

    注意

    "to_tsvector" 是一个 PostgreSQL 函数,用于将文档转换为 tsvector 数据类型。

    https://www.postgresql.org/docs/9.6/static/textsearch-controls.html

    django.contrib.postgres 在内部使用它来提供搜索查找 (__search)

    https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/search/#the-search-lookup

    【讨论】:

    • "to_tsvector" 这是我数据库中的一个表。我们能看到吗。我对这件事有点陌生。
    • case5 或部分单词搜索怎么样
    • "to_tsvector" 是一个 PostgreSQL 函数,用于将文档转换为 tsvector 数据类型。 postgresql.org/docs/9.6/static/textsearch-controls.html django.contrib.postgres 在内部使用它来提供 __search docs.djangoproject.com/en/2.0/ref/contrib/postgres/search/…
    • Django 使用 plainto_tsquery() 函数,而不是 to_tsquery():所以如何进行部分搜索,如“chee”而不是“cheese”,它应该得到单词
    • 如何在 DJANGO 中使用 to_tsvector 和 SQL 来做到这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多