ChristopherRabotin / bungiesearch

UNMAINTAINED CODE -- Elasticsearch-dsl-py django wrapper with mapping generator
BSD 3-Clause "New" or "Revised" License
68 stars 20 forks source link

[QUESTION] Preventing fetching django models #111

Closed NullSoldier closed 8 years ago

NullSoldier commented 8 years ago

I want to prevent this query from executing, because I really just want raw results (id field mostly). https://github.com/Sparrho/bungiesearch/blob/master/bungiesearch/__init__.py#L185

The reason for this is that after I search ElasticSearch I need to perform more complex logic in our database with the ids before I can decide if they should be loaded from the database and loading all those models from is a performance cost when I may only actually end up wanting to materialize half of them.

Looking at the code, it's not exactly clear. I'll keep looking and close this if I figure out how.

NullSoldier commented 8 years ago

The answer is incredibly simple, to use custom_search instead of search. That is located here, https://github.com/Sparrho/bungiesearch/blob/21cf0cf6e7a7e5c77b904f773f2c3b1cc173e14d/bungiesearch/managers.py#L26

ChristopherRabotin commented 8 years ago

Yes. Alternatively, you can also take a slice with the step parameter set to True, as follows:

lazy = Article.objects.search.query('match', _all='Description')
print len(lazy) # Prints the number of hits by only fetching the number of items.
for item in lazy[5:10:True]:
    print item # Will print an ES Result object

I'm also just realizing that this functionality is not documented, although it is in the tests.