Search code examples
pythondjangoelasticsearchdjango-haystackwhoosh

Django haystack indexing many to many field but search failing if there are more than one word


I have followed this link-->Django Haystack and Taggit and had indexed the fields using haystack and whoosh as the search engine. Search failed if the many to many field had more than one word as the attribute value. The indexing is done as -->

>>> results[7]
<Hit {'isbn_13': u'9780750661751', u'django_id': u'39858',
'text':u'Reinventing\n9780750661751\n\nJohn Worthington\n\n\n\n\n',
'authors': u'John Worthington', u'django_ct': u'books.book',
'content_auto': u'Reinventing', u'id': u'books.book.39858'}>

For this index the search is failing on searching John Worthington. authors is the MultivalueField and the search fails as it has 2 words. But when there is one word in the MultivalueField and the index is like->

results[0]
<Hit {'isbn_13': u'9.78147E+12', u'django_id': u'39851',
'text': u'Analytic\n9.78147E+12\n\nChau\n\n\n\n\n',
'authors': u'Chau', u'django_ct': u'books.book',
'content_auto': u'Analytic', u'id': u'books.book.39851'}>

For the above index on searching Chau the search works properly. Can somebody please help me out.


Solution

  • You looking up using autocomplete which designed for auto-completing while you should use filter :

      books = searchQuerySet().filter(content=haystack.inputs.AutoQuery(query))