带有多个单词的Django全文搜索 [英] Django Full-Text Search with multiple words
问题描述
(据我所知),这确实不是一个复杂的问题。
This isn't really a complex problem (to my knowledge).
我知道在MongoDB中,您可以输入一个字符串,它会自动标记并执行全-使用该字符串作为查询进行文本搜索。
I know in MongoDB you can feed in a string and it automatically tokenizes and performs full-text search using that string as a query.
但是,在Django中,我尚未找到类似的功能,并且我看到的所有示例都做了一些工作的行:
However, in Django, I have yet to find similar functionality, and all of the examples I've seen have done something along the lines of:
from django.contrib.postgres.search import SearchQuery
query = SearchQuery('foo')
人们之所以只使用一个单词是因为 SearchQuery只能使用一个单词 >?
Is the reason people only use one word because SearchQuery can only use one word?
我想知道的是如何执行多个单词的全文搜索。就像从django.contrib.postgres.search导入
What I want to know is how to perform full-text search with multiple words. Is it as easy as doing
from django.contrib.postgres.search import SearchQuery
query = SearchQuery('foo and also bar')
吗?还是它需要比这更复杂?
? Or does it need to be more complicated than that?
推荐答案
要使用Django执行全文本搜索,您必须结合使用 GiN索引
和 SearchVector
。
这是完整的工作示例我在某处使用的示例。它也可以处理查询中的2个以上单词,并在3个字段中进行搜索。
To perform Full Text Search with Django you have to combine using GiN index
with SearchVector
.
Here is full working example what I used somewhere. It works also on 2+ words in a query and searches them in 3 fields.
这篇关于带有多个单词的Django全文搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!