Skip to content

Filtering elements before indexing them #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
flaa opened this issue Jul 17, 2020 · 2 comments
Open

Filtering elements before indexing them #285

flaa opened this issue Jul 17, 2020 · 2 comments

Comments

@flaa
Copy link

flaa commented Jul 17, 2020

Is there currently an easy way to filter elements before indexing them? If for example I had Car model and would like to index cars, but only red ones?

I could probably use ELASTICSEARCH_DSL_SIGNAL_PROCESSOR in settings to provide my own class for handling on-save signal, where I would filter cars before indexing them.
But how could I populate the whole index from scratch? I think search_index command will index each and every Car

@rafascar
Copy link

rafascar commented Jul 17, 2020

This issue might help you: #111

Basically you can override the get_queryset method from the Document class, which is called by the search_index command to populate your index.

Something like:

class CarDocument(Document):
    ...

    class Django:
        model = Car

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(color='red')

@flaa
Copy link
Author

flaa commented Jul 17, 2020

@rafascar oh this is so simple yet perfect, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants