django-es / django-elasticsearch-dsl

This is a package that allows indexing of django models in elasticsearch with elasticsearch-dsl-py.
Other
1.02k stars 262 forks source link

Is custom queryset available #268

Open khaldon opened 4 years ago

khaldon commented 4 years ago

I want to use custom queryset I tried add that in to_queryset but doesn't work

user = get_object_or_404(CustomUser,username=request.user.username)
results = RoomDocument.search().to_queryset(user.teacher_rooms.all())
alexgarel commented 4 years ago

You didn't understand the purpose of to_queryset.

The docstring says : This method return a django queryset from an elasticsearch result.

So RoomDocument.search().to_queryset() would simply (in most configuration) be equivalent to Room.objects.all() (but much more costly, for it rely on pks list)

I'm not aware of any method to do what you want to do. Use normal filter instead.

khaldon commented 4 years ago

I am just need to instead of Room.objects.all() I need to get all rooms that user created

On Apr 25 2020, at 4:41 pm, Alex Garel notifications@github.com wrote:

You didn't understand the purpose of to_queryset. The docstring says : This method return a django queryset from an elasticsearch result. So RoomDocument.search().to_queryset() would simply (in most configuration) be equivalent to Room.objects.all() (but much more costly, for it rely on pks list) I'm not aware of any method to do what you want to do. Use normal filter instead. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub (https://github.com/sabricot/django-elasticsearch-dsl/issues/268#issuecomment-619389506), or unsubscribe (https://github.com/notifications/unsubscribe-auth/ACOQIEXBKWKITMZVXBYEDC3ROLZAHANCNFSM4MQQLGIA).

alexgarel commented 4 years ago

@khaldon, this place is for bug report. Please next, time use stackoverflow for support.

Maybe you wan't something like :

user = get_object_or_404(CustomUser,username=request.user.username)
results = RoomDocument.search().filter("terms", _id=user.teacher_rooms.all())

But this is really wild guess, it depends on your schema and how you index rooms. Normally you 'd better have a teacher field in RoomDocument, to directly query on it, because normally elasticsearch is faster than your DB.

You should really read carefully the docs to understand things better. Elasticsearch DSL doc and Django Elasticsearch DSL doc

jhill-cmd commented 4 years ago

trying to do something like

es=EventDocument.search(body=body,size=10)['hits']['hits']

is it compatible?