Adding full-text search to the blog
Next, we will add search capabilities to the blog. Searching for data in the database with user input is a common task for web applications. The Django ORM allows you to perform simple matching operations using, for example, the contains
filter (or its case-insensitive version, icontains
). You can use the following query to find posts that contain the word framework
in their body:
from blog.models import Post
Post.objects.filter(body__contains='framework')
However, if you want to perform complex search lookups, retrieving results by similarity, or by weighting terms based on how frequently they appear in the text or by how important different fields are (for example, relevancy of the term appearing in the title versus in the body), you will need to use a full-text search engine. When you consider large blocks of text, building queries with operations on a string of characters is not enough. A full-text search examines the actual...