Unescape search results with django haystack and elasticsearch
Asked Answered
O

1

1

I'm using django-haystack with elasticsearch backend. The data contains names of books that may contain special characters like &, ' or "". The indexed data escapes these characters and the search results shows the escaped data. How do I tell haystack or elasticsearch to

  1. turn off escaping
    OR
  2. unescape the characters when I want to use the results in a non-HTML context i.e. as plain text ?

Here's my code:

#search_indexes.py
class Book(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)

    def get_model(self):
        return Book

#template
{{object.name}}

#query
SearchQuerySet().autocomplete(text=my_query)
Ostensive answered 26/10, 2014 at 7:41 Comment(0)
L
1

In your template you can use filters and tags like:

{% autoescape on %}
    {{ object.name }}
{% endautoescape %}

or

{{ object.name|striptags }}
Lermontov answered 13/3, 2017 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.