Search
We’ll refer to the same models used in Making queries.
Text-based fields have a selection of matching operations. For example, you may wish to allow lookup up an author like so:
This is a very fragile solution as it requires the user to know an exact substring of the author’s name. A better approach could be a case-insensitive match (), but this is only marginally better.
If you’re using PostgreSQL, Django provides a selection of database specific tools to allow you to leverage more complex querying options. Other databases have different selections of tools, possibly via plugins or user-defined functions. Django doesn’t include any support for them at this time. We’ll use some examples from PostgreSQL to demonstrate the kind of functionality databases may have.
Searching in other databases
In the above example, we determined that a case insensitive lookup would be more useful. When dealing with non-English names, a further improvement is to use :
This shows another issue, where we are matching against a different spelling of the name. In this case we have an asymmetry though - a search for will pick up Helena
or Hélène
, but not the reverse. Another option would be to use a comparison, which compares sequences of letters.
For example:
Now we have a different problem - the longer name of “Helena Bonham Carter” doesn’t show up as it is much longer. Trigram searches consider all combinations of three letters, and compares how many appear in both search and source strings. For the longer name, there are more combinations that don’t appear in the source string, so it is no longer considered a close match.
The correct choice of comparison functions here depends on your particular data set, for example the language(s) used and the type of text being searched. All of the examples we’ve seen are on short strings where the user is likely to enter something close (by varying definitions) to the source data.
- Stemming words, so that “pony” and “ponies” are considered similar.
- Weighting words based on different criteria such as how frequently they appear in the text, or the importance of the fields, such as the title or keywords, that they appear in.
There are many alternatives for using searching software, some of the most prominent are and Solr. These are full document-based search solutions. To use them with data from Django models, you’ll need a layer which translates your data into a textual document, including back-references to the database ids. When a search using the engine returns a certain document, you can then look it up in the database. There are a variety of third-party libraries which are designed to help with this process.
PostgreSQL support
PostgreSQL has its own full text search implementation built-in. While not as powerful as some other search engines, it has the advantage of being inside your database and so can easily be combined with other relational queries such as categorization.
The django.contrib.postgres
module provides some helpers to make these queries. For example, a query might select all the blog entries which mention “cheese”:
You can also filter on a combination of fields and on related models:
See the contrib.postgres
document for complete details.