racedo
racedo

Reputation: 88

Elasticsearch Get API in django-haystack?

I'd like an elegant way to get a document from an elasticsearch index in haystack. Haystack seems to provide just queries but the Get API is not implemented and I found many examples where it would come handy.

I'm using SearchQuerySet with the internal _id field (found after some trial and error).

from haystack.query import SearchQuerySet

    doc_id = myapp.my_model.id

    model_instance = SearchQuerySet().filter(_id__exact=doc_id)[0].object

I think this will always be consistent but I'd like to know if there's any more elegant way.

Any idea?

Upvotes: 0

Views: 317

Answers (1)

savemu
savemu

Reputation: 83

I would suggest you use the database if you simply want to get an object for which you already have the id, instead of using anything coming out of haystack that is meant to search contents.

model_instance = SearchQuerySet().filter(_id__exact=doc_id)[0].object
                                                              ^^^^^^^

Moreover, by calling object you are already doing the same as Model.objects.get(pk=doc_id).

Upvotes: 2

Related Questions