0

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.