.. _ref-autocomplete: ============ Autocomplete ============ Autocomplete is becoming increasingly common as an add-on to search. Haystack makes it relatively simple to implement. There are two steps in the process, one to prepare the data and one to implement the actual search. Step 1. Setup The Data ====================== To do autocomplete effectively, the search backend uses n-grams (essentially a small window passed over the string). Because this alters the way your data needs to be stored, the best approach is to add a new field to your ``SearchIndex`` that contains the text you want to autocomplete on. You have two choices: ``NgramField`` & ``EdgeNgramField``. Though very similar, the choice of field is somewhat important. * If you're working with standard text, ``EdgeNgramField`` tokenizes on whitespace. This prevents incorrect matches when part of two different words are mashed together as one n-gram. **This is what most users should use.** * If you're working with Asian languages or want to be able to autocomplete across word boundaries, ``NgramField`` should be what you use. Example (continuing from the tutorial):: import datetime from haystack import indexes from 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') # We add this for autocomplete. content_auto = indexes.EdgeNgramField(model_attr='content') def get_model(self): return Note def index_queryset(self, using=None): """Used when the entire index for model is updated.""" return Note.objects.filter(pub_date__lte=datetime.datetime.now()) As with all schema changes, you'll need to rebuild/update your index after making this change. Step 2. Performing The Query ============================ Haystack ships with a convenience method to perform most autocomplete searches. You simply provide a field & the query you wish to search on to the ``SearchQuerySet.autocomplete`` method. Given the previous example, an example search would look like:: from haystack.query import SearchQuerySet SearchQuerySet().autocomplete(content_auto='old') # Result match things like 'goldfish', 'cuckold' & 'older'. The results from the ``SearchQuerySet.autocomplete`` method are full search results, just like any regular filter. If you need more control over your results, you can use standard ``SearchQuerySet.filter`` calls. For instance:: from haystack.query import SearchQuerySet sqs = SearchQuerySet().filter(content_auto=request.GET.get('q', '')) This can also be extended to use ``SQ`` for more complex queries (and is what's being done under the hood in the ``SearchQuerySet.autocomplete`` method). Example Implementation ====================== The above is the low-level backend portion of how you implement autocomplete. To make it work in browser, you need both a view to run the autocomplete & some Javascript to fetch the results. Since it comes up often, here is an example implementation of those things. .. warning:: This code comes with no warranty. Don't ask for support on it. If you copy-paste it & it burns down your server room, I'm not liable for any of it. It worked this one time on my machine in a simulated environment. And yeah, semicolon-less + 2 space + comma-first. Deal with it. A stripped-down view might look like:: # views.py import simplejson as json from django.http import HttpResponse from haystack.query import SearchQuerySet def autocomplete(request): sqs = SearchQuerySet().autocomplete(content_auto=request.GET.get('q', ''))[:5] suggestions = [result.title for result in sqs] # Make sure you return a JSON object, not a bare list. # Otherwise, you could be vulnerable to an XSS attack. the_data = json.dumps({ 'results': suggestions }) return HttpResponse(the_data, content_type='application/json') The template might look like:: Autocomplete Example

Autocomplete Example