django-elasticsearch-dsl-drf icon indicating copy to clipboard operation
django-elasticsearch-dsl-drf copied to clipboard

cannot import name 'AggsProxy' from elasticsearch_dsl.search

Open selimt opened this issue 1 year ago • 16 comments
trafficstars

When running on Python 3.9 with the latest elasticsearch_dsl release (8.13.0), we are getting the following traceback:

from django_elasticsearch_dsl_drf.filter_backends import (  # noqa: F401
  File "/usr/local/lib/python3.9/site-packages/django_elasticsearch_dsl_drf/filter_backends/__init__.py", line 29, in <module>
    from .suggester import (
  File "/usr/local/lib/python3.9/site-packages/django_elasticsearch_dsl_drf/filter_backends/suggester/__init__.py", line 5, in <module>
    from .functional import FunctionalSuggesterFilterBackend
  File "/usr/local/lib/python3.9/site-packages/django_elasticsearch_dsl_drf/filter_backends/suggester/functional.py", line 70, in <module>
    from elasticsearch_dsl.search import AggsProxy
ImportError: cannot import name 'AggsProxy' from 'elasticsearch_dsl.search' (/usr/local/lib/python3.9/site-packages/elasticsearch_dsl/search.py)

Pinning elasticsearch_dsl to 8.12.0 gets around the problem.

Thanks -Selim

selimt avatar Apr 04 '24 22:04 selimt

change from elasticsearch_dsl.search import AggsProxy tofrom elasticsearch_dsl.search_base import AggsProxy, it may work

1One0 avatar Apr 07 '24 18:04 1One0

Yes but this import is in the django-elasticsearch-dsl-drf code. Are you suggesting I test and submit a PR ?

selimt avatar Apr 07 '24 22:04 selimt

Folks, I'm very like to use this package again for a project soon. I'll make sure to solve all the issues then.

barseghyanartur avatar Apr 11 '24 19:04 barseghyanartur

@barseghyanartur The issue here is that your EmptySearch class relies on some internal elements of elasticsearch-dsl-py. Our library went through a significant restructure to add support for asyncio recently, and while we were very careful to not break the public interfaces, that caused a lot of internal elements to move or change.

The easy solution would be for you to import this AggsProxy class from its new location, but this is not a great solution, because we limit our backwards compatibility support to publicly available interfaces. For example, we have made, and will continue to make, changes to the Search class and some of these may break your EmtpySearch class again.

If we were to implement our own version of EmptySearch as a public class, would you be willing to drop your version and use ours? I think this would eliminate any risk of new breakages in the future. Thanks!

miguelgrinberg avatar Apr 15 '24 09:04 miguelgrinberg

@miguelgrinberg:

Thanks for the suggestion. Please, show me what you have.

Alternatively, if that's something that needs to be easily swappable, I can think of ways to make it swappable.

barseghyanartur avatar Apr 15 '24 10:04 barseghyanartur

@barseghyanartur when you have a moment have a look at the EmptySearch class that I just have added:

from elasticsearch_dsl import EmptySearch

This is not in a released version yet so you'll need to install the main branch of elasticsearch-dsl-py to get it. This should be a more robust version of yours that is less likely to break in the future.

miguelgrinberg avatar Apr 15 '24 15:04 miguelgrinberg

@barseghyanartur we have just released 8.13.1, including our version of EmptySearch. I believe replacing your version with ours will address this issue. Please let me know if you need any additional support.

miguelgrinberg avatar Apr 30 '24 18:04 miguelgrinberg

@miguelgrinberg:

Thanks. Question. Regarding the compatibility, is elasticsearch-dsl-py 7.x branch still supported? Or is it no longer?

Simply switching to 8.x would mean compatibility issues. I could however, make this EmptySearch quite compatible with whatever you have in your branch. Or really make it swappable. Perhaps, that's the best option.

barseghyanartur avatar Apr 30 '24 23:04 barseghyanartur

@barseghyanartur The 7.x branch is still supported. What I suggest that you do is to try to import EmptySearch from elasticsearch-dsl and only if that fails use your own version. In your utils.py module you could do something like this:

try:
    from elasticsearch_dsl import EmptySearch
except ImportError:
    class EmptySearch:
        # your own version of this class

I think this would allow you to support all the 7.x and 8.x versions. Would this work for you?

miguelgrinberg avatar May 01 '24 09:05 miguelgrinberg

@barseghyanartur just checking to see where we are with this ticket. We rather not pin the elasticsearch-dsl version.

Thanks.

selimt avatar Aug 07 '24 17:08 selimt

@barseghyanartur , if @priyanshu69code 's change looks good to you , can we apply that change here as well ?

Thanks.

selimt avatar Aug 30 '24 16:08 selimt

@barseghyanartur , if @priyanshu69code 's change looks good to you , can we apply that change here as well ?

Thanks.

Hi, @selimt, @barseghyanartur and @priyanshu69code

There is one more place where this import needs to be updated: Can we please update this in both locations? It would help so many users Thanks!

Below is the link --> https://github.com/priyanshu69code/django-elasticsearch-dsl-drf/blob/c9d466951070557d30eeb0412e4b636888c9f991/src/django_elasticsearch_dsl_drf/filter_backends/suggester/functional.py#L70

metalshanked avatar Aug 30 '24 17:08 metalshanked

@metalshanked

Ok sure But I think I have updated the imports at both the place. But let me confirm that.

priyanshu69code avatar Aug 30 '24 17:08 priyanshu69code