I am having a difficult time getting autocomplete to work with haystack and Solr in a search form. Following the instructions here Auto-complete i was able to create my index in the following way.
class PersonIndex(indexes.RealTimeSearchIndex, indexes.Indexable):
text = CharField(document=True, use_template=True)
first_name = CharField(model_attr='first_name')
last_name = CharField(model_attr='last_name')
first_name_auto = indexes.EdgeNgramField(model_attr='first_name')
def index_queryset(self):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all().order_by('first_name')
def get_model(self):
return Person
And with the way my URL route is set up, i dont have a view request that get directed to, the search method works.
url(r'^search/person/', search_view_factory(
view_class=SearchView,
template='index.html',
form_class=ModelSearchForm
), name='haystack_search'),
The instructions say that we can perform the query in this fashion
from haystack.query import SearchQuerySet
sqs = SearchQuerySet().filter(content_auto=request.GET.get('q', ''))
but where do we put this SearchQuerySet, i am not sure what to override, how to modify my url to route correctly. My search currently works out of the box this way but i want to try auto complete with EdgeNgramField ?
You’ll need to define your own custom search form and tell it how to generate the
SearchQuerySetit returns to the view, and then tell yoursearch_view_factoryto use that form instead of theModelSearchForm.Specify the way you want to generate the
SearchQuerySetused by your view by overriding theModelSearchFormsearch method:This will now perform a filter on the form’s
SearchQuerySeton thefirst_name_autofield rather than theauto_querythat it would usually do on thetextfield (seehaystack/forms.pyto see what the original search function looks like).You specify that you want to use this form in the argument list to your
search_view_factory