Fixed #27899 -- Added support for phrase/raw searching in SearchQuery.

Thanks Tim Graham, Nick Pope, and Claude Paroz for contribution and review.
This commit is contained in:
Claude Paroz 2018-09-17 18:03:52 +02:00 committed by Tim Graham
parent c5e450ac95
commit f5e347a640
6 changed files with 84 additions and 6 deletions

View file

@ -70,13 +70,28 @@ and ``weight`` parameters.
``SearchQuery``
===============
.. class:: SearchQuery(value, config=None)
.. class:: SearchQuery(value, config=None, search_type='plain')
``SearchQuery`` translates the terms the user provides into a search query
object that the database compares to a search vector. By default, all the words
the user provides are passed through the stemming algorithms, and then it
looks for matches for all of the resulting terms.
If ``search_type`` is ``'plain'``, which is the default, the terms are treated
as separate keywords. If ``search_type`` is ``'phrase'``, the terms are treated
as a single phrase. If ``search_type`` is ``'raw'``, then you can provide a
formatted search query with terms and operators. Read PostgreSQL's `Full Text
Search docs`_ to learn about differences and syntax. Examples:
.. _Full Text Search docs: https://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES
>>> from django.contrib.postgres.search import SearchQuery
>>> SearchQuery('red tomato') # two keywords
>>> SearchQuery('tomato red') # same results as above
>>> SearchQuery('red tomato', search_type='phrase') # a phrase
>>> SearchQuery('tomato red', search_type='phrase') # a different phrase
>>> SearchQuery("'tomato' & ('red' | 'green')", search_type='raw') # boolean operators
``SearchQuery`` terms can be combined logically to provide more flexibility::
>>> from django.contrib.postgres.search import SearchQuery
@ -87,6 +102,10 @@ looks for matches for all of the resulting terms.
See :ref:`postgresql-fts-search-configuration` for an explanation of the
``config`` parameter.
.. versionadded:: 2.2
The `search_type` parameter was added.
``SearchRank``
==============

View file

@ -90,6 +90,10 @@ Minor features
* :class:`~django.contrib.postgres.indexes.BrinIndex` now has the
``autosummarize`` parameter.
* The new ``search_type`` parameter of
:class:`~django.contrib.postgres.search.SearchQuery` allows searching for
a phrase or raw expression.
:mod:`django.contrib.redirects`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~