La recherche (search/
)
Module situé dans zds/search/
.
Modèles (models.py
)
- class zds.search.models.AbstractSearchIndexable
Mixin for indexable objects.
Define a number of different functions that can be overridden to tune the behavior of indexing into the search_engine.
You (may) need to override:
get_indexable()
;get_schema()
(not mandatory, but otherwise, the search engine will choose the schema by itself);get_document()
(not mandatory, but may be useful if data differ from schema or extra stuffs need to be done).
You also need to maintain
search_engine_id
, which is actually a string. For objects that are also stored in the database, we use the database primary key. We have to define it here (and not in child classAbstractSearchIndexableModel
) because there are objects indexed in the search engine, but not stored in the database.- get_document_source(excluded_fields=[])
Create a document from the instance of the class, based on the schema.
Attention
You may need to override this method if the data differ from the schema for some reason.
- Paramètres:
excluded_fields (list) – exclude some field from the default method
- Renvoie:
document
- Type renvoyé:
dict
- classmethod get_indexable(force_reindexing=False)
Return objects to index.
Attention
You need to override this method (otherwise nothing will be indexed).
- Paramètres:
force_reindexing (bool) – force to return all objects, even if they may already be indexed.
- Type renvoyé:
list
- classmethod get_search_document_schema()
Setup schema for the model (data scheme).
See https://typesense.org/docs/0.23.1/api/collections.html#with-pre-defined-schema
Attention
You may want to override this method (otherwise the search engine choose the schema by itself).
- Renvoie:
schema object. A dictionary containing the name and fields of the collection.
- Type renvoyé:
dict
- classmethod get_search_document_type()
Name of the collection in the search engine for the class.
- class zds.search.models.AbstractSearchIndexableModel(*args, **kwargs)
Version of AbstractSearchIndexable for a Django object, with some improvements:
Already include
pk
in schema;Makes the search engine ID field to be equal to the database primary key;
Define a
search_engine_requires_index
database field to be able to index only new and modified data;Override
save()
to mark the object as requiring to be indexed;Define a
get_indexable_objects()
method that can be overridden to change the queryset to fetch objects to index.
- classmethod get_indexable(force_reindexing=False)
Override
get_indexable()
in order to use the Django querysets and batch objects.- Renvoie:
a queryset
- Type renvoyé:
django.db.models.query.QuerySet
- classmethod get_indexable_objects(force_reindexing=False)
Returns objects that will be indexed in the search engine.
This method can be overridden to filter Django objects from database and prevent to index filtered out objects.
- Paramètres:
force_reindexing (bool) – force to return all indexable objects, even those already indexed.
- Renvoie:
query
- Type renvoyé:
django.db.models.query.QuerySet
- save(*args, **kwargs)
Override the
save()
method to flag the object as requiring to be reindexed (since a save assumes a modification of the object).Note
Flagging can be prevented using
save(search_engine_requires_index=False)
.
Vues (views.py
)
- class zds.search.views.SearchView(**kwargs)
Search view.
- get(request, *args, **kwargs)
Overridden to catch the request and fill the form.
- get_context_data(**kwargs)
Get the context for this view. This method is surcharged to modify the paginator and information given at the template.
- get_queryset()
Return the list of items for this view.
The return value must be an iterable and may be an instance of QuerySet in which case QuerySet specific behavior will be enabled.
- class zds.search.views.SimilarTopicsView(**kwargs)
This view allows you to suggest similar topics when creating a new topic on a forum. The idea is to avoid the creation of a topic on a subject already treated on the forum.
- class zds.search.views.SuggestionContentView(**kwargs)
Staff members can choose at the end of a publication to suggest another content of the site. When they want to add a suggestion, they write in a text field the name of the content to suggest, content proposals are then made, using the search engine through this view.
- zds.search.views.opensearch(request)
Generate OpenSearch Description file.