Django和PostgreSQL全文搜索:搜索查找找不到一些术语

时间:2018-02-06 10:02:52

标签: django postgresql full-text-search

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

我正在使用带有标题和body_text的以下模型:

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的搜索结果。我添加了&#39; django.contrib.postgres&#39;在INSTALLED_APPS。

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 []>

如何搜索不起作用的条款。

1 个答案:

答案 0 :(得分:1)

我们在django中使用了postgresql的全文搜索模块来处理工作中的一些项目,我认为全文搜索是从条目的body_text中删除html标记,并且因为<Entry: Cheese on Toast recipes>, <Entry: Pizza Recipes>而剥离< >

我尝试在您的示例中应用to_tsvector <>,但没有它们,结果向量不同:

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'栏':18'创造':30'数据库':21,36   'entry.objects.filter':24'exampl':23'field':41'full':6'platto':44   '搜索':8,11,27'最简单':2'单独':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'栏':18'创造':38'数据库':21,44   'entri':29,34'entry.objects.filter':24'exampl':23'field':49   '完整':6'披萨':35'普通话':52'收件人':33,36'搜索':8,11,27   '最简单':2'单独':13,17'术语':14'文字':7,26,48'吐司':32   'tsqueri':53'tsvector':41'使用':5'方式':3

请尝试从您的<中删除>body_text

注意

“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