Django MySQL 全文搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2248743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:16:30  来源:igfitidea点击:

Django MySQL full text search

mysqldjangofull-text-search

提问by edkirin

I need to implement full text search for my Django application, running MySQL as backend.

我需要为我的 Django 应用程序实现全文搜索,运行 MySQL 作为后端。

Let's say I've got a model as follows:

假设我有一个模型如下:

class MyItem(models.Model):
    title = models.CharField()
    short_description = models.TextField()
    description = models.TextField()

I would like to have results first for search term occurences in title, then in short_description and at the end in description field. I'll be happier if I don't have to use additional modules/applications for this task.

我想首先在标题中获得搜索词出现的结果,然后在 short_description 中,最后在 description 字段中。如果我不必为此任务使用其他模块/应用程序,我会更高兴。

回答by Silver Light

You can use full text search in django

您可以在 django 中使用全文搜索

MyItem.objects.filter(title__search="some search text")

One thing is - you can't define a fulltext index from a Django model, you need to do in directly in a database (using PHPMyAdmin or SQL query)

一件事是 - 您不能从 Django 模型定义全文索引,您需要直接在数据库中进行(使用 PHPMyAdmin 或 SQL 查询)

See Django documentation for field lookup called search

有关名为的字段查找,请参阅 Django 文档 search

回答by tbr

The previously highest rated answeris deprecated. As of Django 1.10 there is no more searchfield lookup for MySQL databases (see the search section in the 1.10 documentation).

之前收视率最高的答案被弃用。从 Django 1.10 开始,不再search对 MySQL 数据库进行字段查找(请参阅1.10 文档中搜索部分)。

The release notes for 1.10also propose a solution to this, by defining a custom lookup:

1.10发行说明还提出了一个解决方案,通过定义自定义查找:

__search query lookup

The search lookup, which supports MySQL only and is extremely limited in features, is deprecated. Replace it with a custom lookup:

from django.db import models

class Search(models.Lookup):
   lookup_name = 'search'

   def as_mysql(self, compiler, connection):
       lhs, lhs_params = self.process_lhs(compiler, connection)
       rhs, rhs_params = self.process_rhs(compiler, connection)
       params = lhs_params + rhs_params
       return 'MATCH (%s) AGAINST (%s IN BOOLEAN MODE)' % (lhs, rhs), params

models.CharField.register_lookup(Search)
models.TextField.register_lookup(Search)

__search 查询查找

不推荐使用仅支持 MySQL 且功能极其有限的搜索查找。用自定义查找替换它:

from django.db import models

class Search(models.Lookup):
   lookup_name = 'search'

   def as_mysql(self, compiler, connection):
       lhs, lhs_params = self.process_lhs(compiler, connection)
       rhs, rhs_params = self.process_rhs(compiler, connection)
       params = lhs_params + rhs_params
       return 'MATCH (%s) AGAINST (%s IN BOOLEAN MODE)' % (lhs, rhs), params

models.CharField.register_lookup(Search)
models.TextField.register_lookup(Search)

回答by michael

If you are looking for a beefy solution I recommend http://haystacksearch.org/

如果您正在寻找一个强大的解决方案,我推荐http://haystacksearch.org/

It is very well thought out.

这是经过深思熟虑的。

回答by Dominique Barton

I don't know if it helps now but I've created a new Python Library for Django which supports MySQLs' and MariaDBs' native full-textsearch engine over one or multiple columns:

我不知道它现在是否有帮助,但我为 Django 创建了一个新的 Python 库,它支持MySQLs 和 MariaDBs 的列或多列本机全文搜索引擎:

You can have a look at it on the GitHub repository

您可以在GitHub 存储库上查看它

There's also a description how to install it, use it and how to create the FULLTEXT INDEXstuff via Django migrations (Django 1.7+).

还有一个描述如何安装、使用它以及如何FULLTEXT INDEX通过 Django 迁移(Django 1.7+)创建这些东西。

If you've configured the indexes and set the SearchManagerfor your model you should be able to run something like:

如果您已经配置了索引并SearchManager为您的模型设置了索引,您应该能够运行以下内容:

Mymodel.objects.search('Something')
Mymodel.objects.search('Somet*')
Mymodel.objects.search('+Something -Awesome')

Just wanted to update this topic because I didn't find an acceptable solution so far and it might help someone out there as well :)

只是想更新这个主题,因为到目前为止我没有找到一个可以接受的解决方案,它也可能对那里的人有所帮助:)

Cheers Domi

干杯多米

回答by Sна?ош?а?

From django docsregarding full-text search:

来自关于全文搜索的django文档

Example:

Entry.objects.filter(headline__search="+Django -jazz Python")

SQL equivalent:

SELECT ... WHERE MATCH(tablename, headline) AGAINST (+Django -jazz Python IN BOOLEAN MODE);

Note this is only available in MySQL and requires direct manipulation of the databaseto add the full-text index. By default Django uses BOOLEAN MODE for full text searches. See the MySQL documentationfor additional details.

例子:

Entry.objects.filter(headline__search="+Django -jazz Python")

SQL 等效项:

SELECT ... WHERE MATCH(tablename, headline) AGAINST (+Django -jazz Python IN BOOLEAN MODE);

请注意,这仅在 MySQL 中可用,并且需要直接操作数据库以添加全文索引。默认情况下,Django 使用 BOOLEAN MODE 进行全文搜索。有关其他详细信息,请参阅MySQL 文档

Now to the direct manipulation of the database. In MySQL you can create full-text index by following these steps (source article):

现在来直接操作数据库。在 MySQL 中,您可以按照以下步骤创建全文索引(来源文章):

  • Open command prompt, and enter mysql -u root -p. On prompt enter the root password.
  • Enter use your_db_nameto switch to your django database.
  • Enter CREATE FULLTEXT INDEX index_name ON table_name (column_names).
  • 打开命令提示符,然后输入mysql -u root -p. 在提示输入 root 密码。
  • 输入use your_db_name以切换到您的 django 数据库。
  • 输入CREATE FULLTEXT INDEX index_name ON table_name (column_names)

That's it! FTS indexing in enabled in your django database. Now you can use the django's rich QuerySet APIto do full-text searches.

就是这样!在 django 数据库中启用 FTS 索引。现在您可以使用 django 丰富的QuerySet API进行全文搜索。

Edit:The above quote no longer exists in the django version >1.9.

编辑:上述引用不再存在于 django 版本 >1.9 中。