gpt4 book ai didi

Django 和 PostgreSQL 全文搜索 : Some terms not found by search lookup

转载 作者:行者123 更新时间:2023-11-29 12:16:54 25 4
gpt4 key购买 nike

我正在使用 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。我在 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 的全文搜索模块,我认为全文搜索从您的条目的 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 'column':18 'creat':30 'databas':21,36 'entry.objects.filter':24 'exampl':23 'field':41 'full':6 'plainto':44 'search':8,11,27 'simplest':2 'singl':13,17 'term':14 'text':7,26,40 'tsqueri':45 'tsvector':33 'use':5 'way':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 'simplest':2 'singl':13,17 'term':14 'text':7,26,48 'toast':32 'tsqueri':53 'tsvector':41 'use':5 'way':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

关于Django 和 PostgreSQL 全文搜索 : Some terms not found by search lookup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48640269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com