python - Main field name (document=True) -
django haystack docs say:
**warning** when take document=true field, should consistently named across of searchindex classes avoid confusing backend. convention name field text. there nil special text field name used in of examples. anything; phone call pink_polka_dot , won’t matter. it’s convention phone call text.
but don't means. illustration model:
import datetime haystack import indexes myapp.models import note
class noteindex(indexes.searchindex, indexes.indexable): text = indexes.charfield(document=true, use_template=true) author = indexes.charfield(model_attr='user') pub_date = indexes.datetimefield(model_attr='pub_date') def get_model(self): homecoming note def index_queryset(self, using=none): """used when entire index model updated.""" homecoming self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())
is text quoted referring model main field , saying should phone call "text" or class defined in search_indexes.py?
if class in search_indexes.py, field name it's attached in illustration above? doesn't have model_attr!
text = indexes.charfield(document=true, use_template=true)
and if actual app models, how expected refactor project many many apps phone call main text field "text"!
please advise. thanks.
your searchindex
definition not need reflect model definition, needs map info different models mutual search document.
model_attr
keyword) the haystack documentation advising searchindex
field should named consistently across searchindex
definitions - not model fields need named consistently. there's major distinction between search index definitions , model definitions. not need , should not worry 1-1 mapping between model fields , search fields.
step models , think first want search. searching several different models through mutual search view? let's have 2 models:
class note(models.model): title = models.charfield(max_length=40) body = models.textfield() class memo(models.model): subject = models.charfield(max_length=50) content = models.textfield() author = models.foreignkey(staffmember)
we want create simple search view searches primary content of model title or name of content object (name, title, subject, etc.).
here's bad illustration (do not this):
class noteindex(indexes.searchindex, indexes.indexable): body = indexes.charfield(document=true, use_template=true) title = indexes.charfield(model_attr='title') def get_model(self): homecoming note class memoindex(indexes.searchindex, indexes.indexable): content = indexes.charfield(document=true, use_template=true) subject = indexes.charfield(model_attr='subject') def get_model(self): homecoming memo
in bad example, each search index does define primary content field , content name field (title or subject). how search now? if run query against content based on subject
you'll miss note
content, , if query against body
.
better illustration (do this):
class noteindex(indexes.searchindex, indexes.indexable): text = indexes.charfield(document=true, use_template=true) title = indexes.charfield(model_attr='title') def get_model(self): homecoming note class memoindex(indexes.searchindex, indexes.indexable): text = indexes.charfield(document=true, use_template=true) title = indexes.charfield(model_attr='subject') def get_model(self): homecoming memo
note field names not match model field names. define model attribute searchindex
field should source data.
you search documents in search engine, not rows in database, seachindex
definition maps content database (one table or query on many) search document. searchindex
definition transformation, , each searchfield
transforms info specify.
as question missing model_attr
, that's 1 way pull in content. can render textual content template, text
field above (see searchfield api documentation on one). model_attr
source works simple character fields.
python django django-haystack
No comments:
Post a Comment