Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

_highlight property doesn't work in django templates #205

Closed
matthieubellon opened this Issue Feb 21, 2014 · 6 comments

Comments

Projects
None yet
3 participants

Trying to get highlighted fields from SearchResult object I successfully get them in a Django View following the doc (http://elasticutils.readthedocs.org/en/latest/api.html#elasticutils.S.highlight)

Problem is in Django template : methods with underscore are not allowed.

Should I use a custom template filter/tag for accessing the method ? Does not sound great doesn't it ?

Member

willkg commented Feb 28, 2014

Can you give me a little more context? Which SearchResult class is getting used? Are you getting back some MappingType subclass or tuples or dicts or what?

I built my query and fired it with results = s.all()
I then throwed results(dict object) to my template.
While iterating through results django throw me back errors stating that underscored method are not allowed.

Hacking your code at https://github.com/mozilla/elasticutils/blob/master/elasticutils/__init__.py#L1825 and deleting the underscore "solved" the "issue".

Again I might not code this correctly and underscore methods are here for a reason.

Member

willkg commented Mar 2, 2014

@robhudson @jezdez So we use underscores to reduce the likelihood of name clashes between fields in the document and methods on the object and these "metadata" bits. I'm loathe to just remove the underscores. I really want access to the "metadata" bits to be obvious, simple and easy to deal with, but also reduce the likelihood of hard-to-diagnose problems.

I think possible options here might be something like:

  1. Prepending meta to all those names so we'd have meta_id, meta_highlight, ... It's still possible to have a clash, but maybe the likelihood is very remote it's not an issue?
  2. Putting all these metadata things in a dict called eu_metadata or es_metadata or something like that. Then we reduce the clash to one possible name.
  3. Keeping the underscore names and providing Django filters for extracting. Maybe something like {{ my_result|eumeta:"highlight" }}?

Do any of these appeal to you? Are there better options?

Also, I think this is worth fixing for 0.9.

Member

robhudson commented Mar 3, 2014

Of the 3 I like 2 the best... whatever you name it. Would we also keep the attributes on the object and add the dict in addition?

Member

willkg commented Mar 3, 2014

We could but I think I'm inclined to either do one or the other and not both if only to reduce unneeded complexity.

@willkg willkg added this to the 0.9 milestone Mar 7, 2014

@willkg willkg changed the title from _highlight method in django template to _highlight property doesn't work in django templates Mar 18, 2014

Member

willkg commented Mar 18, 2014

I'm toying with an es_meta "object" that has the things as attributes. So then you can do:

{{ obj.es_meta.highlight }}

I think that works pretty well, it's very unlikely that "es_meta" will be a field in the document (we need to document that, but I'm not sure where the best place for that would be--maybe troubleshooting?), and I think it meets all our requirements.

However, we use obj._id in a lot of places, so I'm inclined to have the id available as obj._id as well as in the es_meta object as obj.es_meta.id.

Does that sound icky? If it does, I'll just update the Elasticutils code.

@willkg willkg closed this in 5b1e66c Mar 19, 2014

eire1130 added a commit to eire1130/elasticutils that referenced this issue Mar 27, 2014

Move results metadata to es_meta object
Django templates don't allow you to expose variables that start with an
_. So having:

    obj._highlight

sucked.

We've got a fair number of these metadata items and I'm concerned with
each new one we add, we increase the likelihood that there's a naming
conflict with actual data.

So this moves all those items into an es_meta object which you can
access like this:

    obj.es_meta.highlight

The one exception is _id--you can now access the id Elasticsearch gives
the document with both:

    obj._id

and:

    obj.es_meta.id

Fixes #205
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment