wagtail/docs/topics/search/searching.rst

141 wiersze
3.8 KiB
ReStructuredText
Czysty Zwykły widok Historia

2014-09-28 09:11:34 +00:00
.. _wagtailsearch_searching:
=========
Searching
=========
.. _wagtailsearch_searching_pages:
Searching Pages
===============
Wagtail provides a ``search`` method on the QuerySet for all page models:
.. code-block:: python
# Search future EventPages
>>> from wagtail.wagtailcore.models import EventPage
>>> EventPage.objects.filter(date__gt=timezone.now()).search("Hello world!")
All methods of ``PageQuerySet`` are supported by ``wagtailsearch``:
2014-09-28 09:11:34 +00:00
.. code-block:: python
# Search all live EventPages that are under the events index
>>> EventPage.objects.live().descendant_of(events_index).search("Event")
[<EventPage: Event 1>, <EventPage: Event 2>]
2014-07-09 13:30:39 +00:00
.. _wagtailsearch_frontend_views:
2014-02-18 17:45:31 +00:00
An example page search view
===========================
2014-07-09 13:30:39 +00:00
Here's an example Django view that could be used to add a "search" page to your site:
2014-05-10 05:23:58 +00:00
.. code-block:: python
2014-05-10 05:23:58 +00:00
# views.py
2014-05-10 05:23:58 +00:00
from django.shortcuts import render
2014-05-10 20:49:05 +00:00
from wagtail.wagtailcore.models import Page
from wagtail.wagtailsearch.models import Query
2014-05-10 05:23:58 +00:00
def search(request):
# Search
search_query = request.GET.get('query', None)
if search_query:
search_results = Page.objects.live().search(search_query)
2014-05-10 05:23:58 +00:00
# Log the query so Wagtail can suggest promoted results
Query.get(search_query).add_hit()
else:
search_results = Page.objects.none()
2014-05-10 20:49:05 +00:00
# Render template
return render(request, 'search_results.html', {
'search_query': search_query,
'search_results': search_results,
})
2014-05-10 05:23:58 +00:00
2014-05-10 20:49:05 +00:00
And here's a template to go with it:
2014-05-10 05:23:58 +00:00
.. code-block:: html
2014-05-10 05:23:58 +00:00
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block title %}Search{% endblock %}
{% block content %}
<form action="{% url 'search' %}" method="get">
<input type="text" name="query" value="{{ search_query }}">
<input type="submit" value="Search">
</form>
{% if search_results %}
<ul>
{% for result in search_results %}
<li>
<h4><a href="{% pageurl result %}">{{ result }}</a></h4>
{% if result.search_description %}
{{ result.search_description|safe }}
{% endif %}
</li>
{% endfor %}
</ul>
{% elif search_query %}
No results found
{% else %}
Please type something into the search box
{% endif %}
{% endblock %}
2014-05-10 05:23:58 +00:00
2015-07-06 10:35:38 +00:00
Promoted search results
=======================
2014-05-10 05:23:58 +00:00
2015-07-06 10:35:38 +00:00
"Promoted search results" allow editors to explicitly link relevant content to search terms, so results pages can contain curated content in addition to results from the search engine.
2014-05-10 05:23:58 +00:00
This functionality is provided by the :mod:`~wagtail.contrib.wagtailsearchpromotions` contrib module.
2014-05-10 20:49:05 +00:00
Searching Images, Documents and custom models
=============================================
2014-05-10 20:49:05 +00:00
You can search these by using the ``search`` method on the search backend:
2014-05-10 20:49:05 +00:00
.. code-block:: python
>>> from wagtail.wagtailimages.models import Image
>>> from wagtail.wagtailsearch.backends import get_search_backend
2014-09-28 09:11:34 +00:00
# Search images
>>> s = get_search_backend()
>>> s.search("Hello", Image)
[<Image: Hello>, <Image: Hello world!>]
2014-09-28 09:11:34 +00:00
You can also pass a QuerySet into the ``search`` method which allows you to add filters to your search results:
2014-09-28 09:11:34 +00:00
.. code-block:: python
2014-09-28 09:11:34 +00:00
>>> from wagtail.wagtailimages.models import Image
>>> from wagtail.wagtailsearch.backends import get_search_backend
2014-09-28 09:11:34 +00:00
# Search images
>>> s = get_search_backend()
>>> s.search("Hello", Image.objects.filter(uploaded_by_user=user))
[<Image: Hello>]
2014-09-28 09:11:34 +00:00
This should work the same way for Documents and :ref:`custom models <wagtailsearch_indexing_models>` as well.