Python wagtail Indexing














































Python wagtail Indexing



In the previous few article we have discuss about Search.If you have not gone through it you see through this link Search .In which i have discuss one of the most important think while writing that is Indexing.In this article we learn more about Indexing more.

Indexing


To make a model searchable, you'll need to add it into the search index. All pages, images and documents are indexed for you, you can do it by using Updating the index .

If you have created some extra fields in a subclass of Page or Image, you may want to add these new fields to the search index too so that a user's search query will match on their content. You have to use  Indexing extra fields for doing this.

If you have a custom model that you would like to make searchable, You can use Indexing custom models.

Here we will learn all three part one by one:-

                                                Updating the index

If the search index is kept separate from the database , you need to keep them both in sync. There are two ways to do this: using the search signal handlers, or calling the update_index command periodically. For best speed and reliability, it's best to use both if possible.
          

                                       The update_index command

Wagtail also provides a command for rebuilding the index from scratch.

./manage.py update_index

                                            Indexing extra fields


Fields must be explicitly added to the search_fields property of your Page-derived model, in order for you to be able to search/filter on them. This is done by overriding search_fields to append a list of extra SearchField/FilterField objects to it

This creates an EventPage model with two fields: description and date. description is indexed as a and date is indexed as a SearchField and date is indexed as a FilterField

Example

from wagtail.search import index
from django.utils import timezone

class EventPage(Page):
description = models.TextField()
date = models.DateField()

search_fields = Page.search_fields + [ # Inherit search_fields from Page
index.SearchField('description'),
index.FilterField('date'),
]


# Get future events which contain the string "Christmas" in the title or description
>>> EventPage.objects.filter(date__gt=timezone.now()).search("Christmas")

                          Indexing custom models


Any Django model can be indexed and searched.

To do this, inherit from index.Indexed and add some search_field to the model.

from wagtail.search import index

class Book(index.Indexed, models.Model):
title = models.CharField(max_length=255)
genre = models.CharField(max_length=255, choices=GENRE_CHOICES)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
published_date = models.DateTimeField()

search_fields = [
index.SearchField('title', partial_match=True, boost=10),
index.SearchField('get_genre_display'),

index.FilterField('genre'),
index.FilterField('author'),
index.FilterField('published_date'),
]

# As this model doesn't have a search method in its QuerySet, we have to call search directly on the backend
>>> from wagtail.search.backends import get_search_backend
>>> s = get_search_backend()

# Run a search for a book by Roald Dahl
>>> roald_dahl = Author.objects.get(name="Roald Dahl")
>>> s.search("chocolate factory", Book.objects.filter(author=roald_dahl))
[<Book: Charlie and the chocolate factory>]

More Articles of SAYENDEEP MAHANTY:

Name Views Likes
C++ Exception Handling-|| 85 3
C++ Exception Handling 116 3
C++ File Handling EOF 93 5
C++ File Handling Error Handling During file Operation 1135 5
C++ File Handling seekp() Function 105 5
C++ File Handling seekg() Function 105 6
C++ File Handling tellg() Function 127 5
C++ File Handling tellp() Function 133 5
C++ File Handling close() Function 78 5
C++ File Handling:: open() 106 5
C++ File Handling Introduction 131 5
C++ Container Library:: std ::array-II 101 4
Top Django Interview Questions and Answers You Need to Know 302 6
C++ Container Library::std::array 96 6
C++ Container Library Introduction 103 5
Essential Engineering Skills for Your Resume 127 5
Mistakes To Avoid During Technical Interview 133 5
Mistakes You Must Avoid During Work From Home 178 6
Seven Common Mistakes that Beginners Should Avoid While Learning to Code 109 6
Simple Linear Regression 96 6
Python wagtailmenus 122 6
Python wagtail application 119 6
Python guppy GSL 136 5
Python wagtail guppy subpackage heapy 174 6
Python guppy Introduction 215 6
Python wagtail Structural Block 258 6
Python wagtail Integrating into Django Project 370 6
Python wagtail testing 280 7
Python wagtail Basic Block 349 7
Python wagtail image tag 216 7
Python wagtail Templates 179 6
Python wagtail Backends 206 6
Python wagtail Indexing 190 6
Python wagtail Search 173 7
Python wagtail Snippets 192 6
Python wagtail Writing your own page models 165 9
Python wagtail model class PageRevision 196 10
Python wagtail model class Site 198 9
Python wagtail wagtail core model 218 9
Python wagtail page models 189 10
Python wagtail 166 11

Comments