搜索

    We'll refer to the same models used in 进行查询.

    Text-based fields have a selection of simple 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 exactsubstring of the author's name. A better approach could be a case-insensitivematch (), but this is only marginally better.

    If you're using PostgreSQL, Django provides a selection of databasespecific tools to allow you to leverage morecomplex querying options. Other databases have different selections of tools,possibly via plugins or user-defined functions. Django doesn't include anysupport for them at this time. We'll use some examples from PostgreSQL todemonstrate the kind of functionality databases may have.

    All of the searching tools provided by areconstructed entirely on public APIs such as custom lookups and . Depending on your database, you shouldbe able to construct queries to allow similar APIs. If there are specificthings which cannot be achieved this way, please open a ticket.

    In the above example, we determined that a case insensitive lookup would bemore useful. When dealing with non-English names, a further improvement is touse unaccented comparison:

    This shows another issue, where we are matching against a different spelling ofthe 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 optionwould be to use a comparison, which comparessequences of letters.

    例如:

    The correct choice of comparison functions here depends on your particular dataset, for example the language(s) used and the type of text being searched. Allof the examples we've seen are on short strings where the user is likely toenter something close (by varying definitions) to the source data.

    Simple database operations are too simple an approach when you startconsidering large blocks of text. Whereas the examples above can be thought ofas operations on a string of characters, full text search looks at the actualwords. Depending on the system used, it's likely to use some of the followingideas:

    • Stemming words, so that "pony" and "ponies" are considered similar.
    • Weighting words based on different criteria such as how frequently theyappear in the text, or the importance of the fields, such as the title orkeywords, that they appear in.
      There are many alternatives for using searching software, some of the mostprominent are Elastic and . These are full document-based searchsolutions. To use them with data from Django models, you'll need a layer whichtranslates your data into a textual document, including back-references to thedatabase ids. When a search using the engine returns a certain document, youcan then look it up in the database. There are a variety of third-partylibraries which are designed to help with this process.

    PostgreSQL support

    PostgreSQL has its own full text search implementation built-in. While not aspowerful as some other search engines, it has the advantage of being insideyour database and so can easily be combined with other relational queries suchas categorization.

    The module provides some helpers to make thesequeries. For example, a simple query might be to select all the blog entrieswhich mention "cheese":

    See the contrib.postgres Full text search document forcomplete details.