简体   繁体   中英

Add CharField to the search index in haystack

I use in my django app (1.8), haystack (2.4.1) to search. And I want to be able to serach words with autocomplete (EdgeNgramField) and words when I put only the part of name, for example 'zo-zo on' (this isn't working with EdgeNgramField)

Below I tired added: text_sec = indexes.CharField(use_template=True) but this isn't working for me.

Here is my code, but he doesn't works:

class EventIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)
    text_sec = indexes.CharField(use_template=True)
    id = indexes.CharField(model_attr='id')
    get_absolute_url = indexes.CharField(model_attr='get_absolute_url')
    description = indexes.CharField(model_attr='description', null=True)
    is_past = indexes.CharField(model_attr='is_past', default='false')
    date_start = indexes.DateTimeField(model_attr='date_start')

You will need to setup to different fields in your schema to power autocomplete and normal search. In the following i have defined two fields ie one is text and other is content_auto one which is populated with title from your model.

class EventIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    content_auto = indexes.EdgeNgramField(model_attr='title')
    text_sec = indexes.CharField(use_template=True)
    id = indexes.CharField(model_attr='id')
    get_absolute_url = indexes.CharField(model_attr='get_absolute_url')
    description = indexes.CharField(model_attr='description', null=True)
    is_past = indexes.CharField(model_attr='is_past', default='false')
    date_start = indexes.DateTimeField(model_attr='date_start')

When you want to do normal search you should search on text field, for autosuggest on content_auto.

You should read up on docs http://haystacksearch.org/ for more on this,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM