How it works...

Django Elasticsearch DSL documents are similar to model forms. There you define which fields of the model to save to the index that later will be used for the search queries. In our IdeaDocument example, we are saving the UUID, rating, author, categories, titles, contents, and URL paths in all languages and a picture thumbnail URL. The Index class defines the settings of the Elasticsearch index for this document. The Django class defines where to populate the index fields from. There is the related_models setting that tells after which model changes to also update this index. In our case, it is a Category model. Note that with django-elasticsearch-dsl, the indexes will be updated automatically whenever the models are saved. That is done using signals.

The get_instances_from_related() method tells how to retrieve the Idea model instances when a Category instance is changed.

The prepare() and prepare_*() methods of IdeaDocument tell where to take the data from and how to save the data for specific fields. For example, we are reading the data for title_lt from the title field of the IdeaTranslations model where the language field equals "lt"

The last properties and methods of the IdeaDocument are there to use for retrieval of information from the index in the currently active language.

Then, we have a view with the search form. There is a query field in the form called q. When it is submitted, we are searching for the queried word in the title, content, or category's title field of the current language. Then, we wrap the search results with a lazily evaluated SearchResults class, so that we could use it with the default Django paginator.

The template of the view will have the search form in the sidebar and the search results in the main column, and it will look something like this:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.103.202