mozilla / elasticutils

[deprecated] A friendly chainable ElasticSearch interface for python
http://elasticutils.rtfd.org
BSD 3-Clause "New" or "Revised" License
243 stars 76 forks source link

_highlight property doesn't work in django templates #205

Closed mathieubellon closed 10 years ago

mathieubellon commented 10 years ago

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 ?

willkg commented 10 years ago

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?

mathieubellon commented 10 years ago

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.

willkg commented 10 years ago

@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.

robhudson commented 10 years ago

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?

willkg commented 10 years ago

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 commented 10 years ago

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.