This document covers the basics of writing a Search Document. It’s the central config point for creating a Solr schema, transforming documents and returning results.
Here’s an example a search.py from a simple blog:
from example.models import Post
import solango
class PostDocument(solango.SearchDocument):
title = solango.fields.TextField(copy=True)
pub_date = solango.fields.DateField(copy=True)
content = solango.fields.TextField(copy=True)
#Overrides the default transform
def transform_content(self, instance):
return instance.body
solango.register(Post, PostDocument)
This allows solango to register Document with the service.
This may seem rather redundant. I mean you already have to build the model, why do I have to do the same thing over again? Yeah, agreed, but without this, you can’t have the level of control that you will need to facet. pub_date is a great example. Every model usually has a date associated with it, but they are all named differently. pub_date, date, submit_date, et al. In order to have one date field date we allow a transform so all those dates can work through the same facet.
The default search document looks like this:
class SearchDocument(BaseSearchDocument):
id = search_fields.PrimaryKeyField()
model = search_fields.ModelField()
site_id = search_fields.SiteField()
url = search_fields.UrlField()
text = search_fields.TextField(multi_valued=True)
Each field takes a number of options. They determine how Solr should react to each.
Like Django forms the search document has a media class. Declare your template here:
class Media:
template = 'coltrane/entry_document.html'
__init__
transform
clean
add
delete
to_xml
render_html
transform_*
Transforms a model instance field into a SearchDocument field. The transform function is given an instance of the model so you can retrieve model attributes from it. To take author as an example the value for that field will be the full name of the author. Where the title doesn’t need a transform function because it’s already an attribute of the instance:
class EntryDocument(solango.SearchDocument):
author = solango.fields.CharField()
...
def transform_author(self, instance):
return instance.author.get_full_name()
clean_*
is_indexable
In the default search document, everything is indexable. You can override this method to provide selective indexing for a given model. For instance, a typical is_indexable method might look like this:
def is_indexable(self, instance):
return instance.is_public and not instance.is_deleted
get_boost