Django MySQL full text search

后端 未结 6 1636
终归单人心
终归单人心 2020-12-05 05:20

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

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

class MyItem(model         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-05 05:29

    From django docs regarding full-text search:

    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 database to add the full-text index. By default Django uses BOOLEAN MODE for full text searches. See the MySQL documentation for additional details.

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

    • Open command prompt, and enter mysql -u root -p. On prompt enter the root password.
    • Enter use your_db_name to switch to your django database.
    • Enter 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 API to do full-text searches.

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

提交回复
热议问题