且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在django中使用sqlite3数据库中的全文搜索?

更新时间:2023-02-01 08:34:10

I认为当sqlite是一个惊人的软件,它的全文搜索功能是非常有限的。相反,您可以使用干草堆 Django应用程序对某些后端像弹性搜索。有了这个设置(并且仍然能够访问你的sqlite数据库)似乎是FTS中最强大和灵活的方式。

I think that while sqlite is an amazing piece of software, its full-text search capabilities are quite limited. Instead you could index your database using Haystack Django app with some backend like Elasticsearch. Having this setup (and still being able to access your sqlite database) seems to me the most robust and flexible way in terms of FTS.

Elasticsearch有一个模糊搜索(简而言之,它将处理你的egsample查询)。所以你需要的是一个正确的查询类型:

Elasticsearch has a fuzzy search based on the Levenshtein distance (in a nutshell, it would handle your "egsample" queries). So all you need is to make a right type of query:

from haystack.forms import SearchForm
from haystack.generic_views import SearchView
from haystack import indexes


class QScriptIndex(indexes.SearchIndex, indexes.Indexable):
    v = indexes.CharField(document=True)

    def get_model(self):
        return QScript


class QScriptSearchForm(SearchForm):
    text_fuzzy = forms.CharField(required=False)    

    def search(self):        
        sqs = super(QScriptSearchForm, self).search()

        if not self.is_valid():
            return self.no_query_found()

        text_fuzzy = self.cleaned_data.get('text_fuzzy')
        if text_fuzzy:
            sqs = sqs.filter(text__fuzzy=text_fuzzy)

        return sqs


class QScriptSearchView(SearchView):        
    form_class = QScriptSearchForm

更新:只要 PostgreSQL 具有Levenshtein距离功能,您还可以利用它作为Haystack后端以及独立的搜索引擎。如果您选择第二种方式,则必须执行自定义查询表达式,如果您使用最新版本的Django,则相对容易。

Update: As long as PostgreSQL has the Levenshtein distance function, you could also leverage it as the Haystack backend as well as a standalone search engine. If you choose the second way, you'll have to implement a custom query expression, which is relatively easy if you're using a recent version of Django.