question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API search order has no effect

See original GitHub issue

Steps to reproduce:

  • use wagtail 1.9 & elasticsearch 2 or any version of wagtail-pg-search-backend
  • install Wagtail’s API
  • go to http://localhost:8000/api/v2/pages/?search=a+non-empty+search&order=first_published_at where you replace a+non-empty+search with any search query that returns results
  • go to http://localhost:8000/api/v2/pages/?search=a+non-empty+search&order=-first_published_at (with the same replacement as before)
  • compare the results

As you should see, ordering has no effect on the actual order of results. I discovered this issue while trying to implement search ordering (not to be confused with ranking) in wagtail-pg-search-backend. And I couldn’t find how to do it with the current way search is implemented.

So I guess searching doesn’t handle custom ordering. Unfortunately, the opposite is written in the docs, even with details on how to use it.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
m1kolacommented, May 23, 2019

#5318 should hopefully fix the issue with API. To make code snippets work from my previous comment - we will also need to make changes in the elastcisearch backends. I think it’s a separate issue so I created #5319

0reactions
m1kolacommented, May 22, 2019

I recently hand a similar issue with:

  • Elasticsearch 5
  • Django 2.1.3 and 2.1.5
  • Wagtail 2.3 and 2.4. Pretty sure it affects 2.5 too.

This happens because Wagtail API sets reverse order using the reverse() on a QuerySet:

https://github.com/wagtail/wagtail/blob/58135ccf6ea06095379c32e07578095545b37762/wagtail/api/v2/filters.py#L79-L95

And Elasticsearch backends do not detect reverse order set using this method:

https://github.com/wagtail/wagtail/blob/58135ccf6ea06095379c32e07578095545b37762/wagtail/search/backends/base.py#L120-L140

Note that wagtail.contrib.postgres_search.backend doesn’t use _get_order_by method, so it works with fine.


Examples using code. This one works as expected with Elasticsearch search backend:

from wagtail.core.models import Page
from wagtail.search.backends import get_search_backend

search_query = 'social'
queryset = Page.objects.order_by('-first_published_at')

sb = get_search_backend()
results = sb.search(search_query, queryset, operator=None, order_by_relevance=False)

for r in results[:10]:
    print(r.pk, r.title)

This one will return results in different (ascending?) order:

from wagtail.core.models import Page
from wagtail.search.backends import get_search_backend

search_query = 'social'
queryset = Page.objects.order_by('first_published_at')
queryset = queryset.reverse()

sb = get_search_backend()
results = sb.search(search_query, queryset, operator=None, order_by_relevance=False)

for r in results[:10]:
    print(r.pk, r.title)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Deezer API Search does not honor the "order" and sometimes ...
The order param is not currently implementing for advanced search ; that's why it is not considered on your request.
Read more >
Views + Search API not properly sorting by Commerce Price ...
I've tried indexing the price field as an integer, decimal, and string but it always has the same effect. There are no additional...
Read more >
Stock not decrementing when order created using API
Hello! I am currently encountering an issue with the create order endpoint in the API not decrementing stock.
Read more >
findItemsByKeywords - API Reference - Finding API
This call searches for items on eBay by a keyword query (keywords). ... Sorting has no affect upon the quantity of returned results....
Read more >
Search REST API Reference - Algolia
Results will be received in the same order as the requests. Errors: 400 : Bad request argument; 404 : Index does not exist....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found