[Fixed]-Django Haystack autocomplete does not work as expected

1👍

You can’t use a CharField with sqs.autocomplete(). From the documentation.

You have two choices: NgramField and 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.

Either you need to change your name index field to an EdgeNgramField or (if you are using name elsewhere) create a separate field that is used for autocomplete searches, e.g.:

autocomplete_name = indexes.EdgeNgramField(model_attr='name')

Then query with:

sqs = sqs.autocomplete(autocomplete_name=query)

This should give you the expected results.

Leave a comment