barseghyanartur / django-elasticsearch-dsl-drf

Integrate Elasticsearch DSL with Django REST framework.
https://pypi.python.org/pypi/django-elasticsearch-dsl-drf
375 stars 121 forks source link

Aggregating search results from different documents #121

Open johnyoonh opened 5 years ago

johnyoonh commented 5 years ago

Thank you for this great project. One of the feature that I used on haystack library was using multiple indexes for a mapping on ES1.7 so I can aggregate search results on different documents. https://django-haystack.readthedocs.io/en/master/multiple_index.html

I am upgrading to ES6.7 using the latest commit on django-dlasticsearch-dsl https://github.com/safwanrahman/django-elasticsearch-dsl/tree/major_fix On ES6.x there is a way achieve this using explicit type field. https://www.elastic.co/guide/en/elasticsearch/reference/6.0/removal-of-types.html I was wondering if this was something you would be interested in supporting.

barseghyanartur commented 5 years ago

@johnyoonh:

Elastic recommends one index per one document type.

That's why type is going to be removed in future.

It does support multi tenancy, however. Easily configured in the ELASTICSEARCH_DSL setting in your Django settings.

I would rather ask you what are you trying to achieve by that? Merging multiple models into one search index? Something else?

In any case, it shall first be supported on the side of django-elasticsearch-dsl. This is just a wrapper around it for making it easy to use with django-rest-framework.

johnyoonh commented 5 years ago

The support is there on the side of django-elasticsearch-dsl and the change needed for this repo seems to be simple enough. I was able to achieve this simply by overrding __init__ function of BaseDocumentViewSet to specify list of indexes for search parameter index as shown here in the example:

self.search = Search(
        using=self.client,
        index=self.index, # <-- This is a list now
        doc_type=self.document._doc_type.name
    )

https://github.com/sabricot/django-elasticsearch-dsl/issues/134#issuecomment-482149563 Having unified value for using (i.e. default) and doc_type (i.e. doc) across different documents types is important but I don't see these parameters changing for 99% users out there.

barseghyanartur commented 5 years ago

@johnyoonh:

Do you have a working project with the changes mentioned above publicly available? I want to estimate how much work needs to be done here.