Skip to content
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

Elasticsearch backend stacktrace with order_by_relevance=False #8859

Open
nutjob4life opened this issue Jul 15, 2022 · 1 comment
Open

Elasticsearch backend stacktrace with order_by_relevance=False #8859

nutjob4life opened this issue Jul 15, 2022 · 1 comment

Comments

@nutjob4life
Copy link
Contributor

Issue Summary

When a query set has certain order_by conditions added to it, such as Lower('some_field'), the Elasticsearch backend produces a stack trace when order_by_relevance=False is used to search the set.

Steps to Reproduce

$ date -u
Fri Jul 15 13:58:47 UTC 2022
$ sw_vers
ProductName:    macOS
ProductVersion: 12.4
BuildVersion:   21F79
$ python3 --version
Python 3.10.5
$ python3 -m venv venv
$ . venv/bin/activate
(venv) $ pip install --quiet --upgrade wagtail==3.0.1 pip setuptools
(venv) $ wagtail start issue
Creating a Wagtail project called issue
Success! issue has been created
(venv) $ cd issue
(venv) $ echo 'elasticsearch~=7.17' >> requirements.txt
(venv) $ pip install --quiet --requirement requirements.txt
(venv) $ cat >>issue/settings/base.py <<EOF
WAGTAILSEARCH_BACKENDS = {
    'default': {
        'ATOMIC_REBUILD': True,
        'AUTO_UPDATE': True,
        'BACKEND': 'wagtail.search.backends.elasticsearch7',
        'INDEX': 'wagtail',
        'INDEX_SETTINGS': {},
        'OPTIONS': {},
        'TIMEOUT': 5,
        'URLS': ['http://localhost:9200'],
    }
}
EOF
(venv) $ ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, home, sessions, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers
…
  Applying wagtailusers.0009_userprofile_verbose_name_plural... OK
  Applying wagtailusers.0010_userprofile_updated_comments_notifications... OK
(venv) $ ./manage.py createsuperuser --no-input --username admin --email admin@admin.com
Superuser created successfully.
(venv) $ ./manage.py shell <<EOF
from wagtail.core.models import Page, Site
from django.db.models.functions import Lower
root = Site.objects.filter(is_default_site=True).first().root_page
p1 = Page(title='hello world')
root.add_child(instance=p1)
p1.save()
p2 = Page(title='Hello World')
root.add_child(instance=p2)
p2.save()
Page.objects.all().order_by(Lower('title')).search('world', order_by_relevance=False)
EOF
Traceback (most recent call last):
  File "issue/./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
…
  File "venv/lib/python3.10/site-packages/wagtail/search/backends/base.py", line 174, in _get_order_by
    if field_name.startswith("-"):
AttributeError: 'Lower' object has no attribute 'startswith'

Any other relevant information. For example, why do you consider this a bug and what did you expect to happen instead?

It feels like a bug because the documentation at this page states that order_by_relevance=False preserves the QuerySet's ordering:

Screen Shot 2022-07-15 at 9 04 05 AM

  • I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes

Technical details

  • Python version: 3.10.5
  • Django version: 4.0.6
  • Wagtail version: 3.0.1
  • Browser version: N/A
@NXPY123
Copy link
Contributor

NXPY123 commented Sep 29, 2023

I was able to reproduce the issue by following the steps mentioned by @nutjob4life

Steps to Reproduce

source ./WagTail/WagTail/bin/activate
$ date -u
Fri Sep 29 04:01:28 UTC 2023
$ sw_vers          
ProductName:            macOS
ProductVersion:         13.3.1
ProductVersionExtra:    (a)
BuildVersion:           22E772610a
$ python3 --version
Python 3.11.4
$ pip install --quiet --upgrade wagtail                      
$ pip install --upgrade setuptools
$ wagtail start issue
$ cd issue
$ echo 'elasticsearch~=7.17' >> requirements.txt
$ pip install --quiet --requirement requirements.txt
$ cat >>issue/settings/base.py <<EOF
heredoc> WAGTAILSEARCH_BACKENDS = {
    'default': {
        'ATOMIC_REBUILD': True,
        'AUTO_UPDATE': True,
        'BACKEND': 'wagtail.search.backends.elasticsearch7',
        'INDEX': 'wagtail',
        'INDEX_SETTINGS': {},
        'OPTIONS': {},
        'TIMEOUT': 5,
        'URLS': ['http://localhost:9200'],
    }
}
EOF
$ ./manage.py migrate
$ ./manage.py createsuperuser --no-input --username admin --email admin@admin.com
Superuser created successfully.
$ ./manage.py shell <<EOF
from wagtail.models import Page, Site 
from django.db.models.functions import Lower
root = Site.objects.filter(is_default_site=True).first().root_page
p1 = Page(title='hello world')
root.add_child(instance=p1)
p1.save()
p2 = Page(title='Hello World')
root.add_child(instance=p2)
p2.save()
Page.objects.all().order_by(Lower('title')).search('world', order_by_relevance=False)
EOF
Exception raised while adding <Page: hello world> into the 'default' search backend
Traceback (most recent call last):
  File "/Users/neeraj_py/Desktop/WagTail/issue/./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
...
  File "/Users/neeraj_py/Desktop/WagTail/WagTail/lib/python3.11/site-packages/wagtail/search/backends/base.py", line 233, in _get_order_by
    if field_name.startswith("-"):
       ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Lower' object has no attribute 'startswith'

Technical Details

  • Python: 3.11.4
  • Django: 4.2.5
  • Wagtail: 5.1.2

@zerolab zerolab removed the status:Unconfirmed Issue, usually a bug, that has not yet been validated as a confirmed problem. label Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants