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.

WagtailSearch ElasticSearch backend setting to set results size

See original GitHub issue

Issue Summary

There is no way to specify results count ('size'), returned by elasticsearch2 backend which results into always getting the defaults (10) results.

Steps to Reproduce

  1. Set up WagtailSearch with elasticsearch2 backend.
  2. Run .search('QUERY').results() always returns 10 results

There is nothing in documentation about specifying the results count. Should be a django setting? Here is what I am doing:

In [7]: query = 'shoes'

In [8]: TenantPage.objects.count()
Out[8]: 88

In [9]: TenantPage.objects.search(query).count()
Out[9]: 80

In [10]: len(TenantPage.objects.search(query).results())
Out[10]: 10

I also tried to add ‘size’ to OPTIONS in Django settings as below:

WAGTAILSEARCH_BACKENDS = {
    'default': {
        'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch2',
        'INDEX': 'wagtail',
        'INDEX_SETTINGS': {},
        'OPTIONS': {'from': 0, 'size': 100},  # has no effect
        'TIMEOUT': 5,
        'URLS': ['http://localhost:9200']
    }
}

If you trace the Elasticsearch2SearchBackends result class (Elasticsearch2SearchResults) to it’s parent, you end up here:

class ElasticsearchSearchResults(BaseSearchResults):
    fields_param_name = 'fields'
    
    ...

    def _do_search(self):
        # Params for elasticsearch query
        params = dict(
            index=self.backend.get_index_for_model(self.query.queryset.model).name,
            body=self._get_es_body(),
            _source=False,
            from_=self.start,
        )

        params[self.fields_param_name] = 'pk'

        # Add size if set
        if self.stop is not None:
            params['size'] = self.stop - self.start
    ...

If I hardcode params['size']=100 it works. I see some logic to “Add size if set” but nothing about how to actually set it. Is it documented? But I assume it’s being used for pagination and offsetting the results.

Technical details

  • Python version: 2.7.3
  • Django version: 1.9.6
  • Wagtail version: 1.9

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gasmancommented, Mar 8, 2017

Slicing is implemented internally by passing the relevant from / size parameters to Elasticsearch (just as Django querysets implement slicing by adding a LIMIT clause to the SQL), so there shouldn’t be any performance implication there.

I think it would make sense for our ES backend to set a default size of e.g. 100 when no slice has been given - rather than ES’s own default of 10 - so that people are less likely to run into this “taking a slice gives you more results” weirdness. Perhaps we should also have it throw a warning when we run a query with no explicit range specified, similarly to how Django now throws a warning when paginating an unordered queryset?

0reactions
nimasmicommented, Mar 21, 2017

Aside: the [:100] workaround strips the annotation added by .annotate_score(), as does retrieving a result by its index.

>>> results = MyObject.search('foo').annotate_score('_score')
>>> results[0]._score
AttributeError: 'MyObject' object has no attribute '_score'
>>> [x._score for x in results[:100]]
AttributeError: 'MyObject' object has no attribute '_score'
>>> [x._score for x in results]
[0.38437524, 0.24610822, 0.24095446, 0.23447372, 0.22940964, 0.21996033, 0.21083516, 0.1894834, 0.18731217, 0.1755507]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Backends — Wagtail 2.12.2 documentation
Configure URLS in the Elasticsearch entry in WAGTAILSEARCH_BACKENDS using the Cluster URL from your Bonsai dashboard; Run ./manage.py update_index ...
Read more >
Release 1.0b2 Torchbox - Wagtail Documentation
ElasticSearch ',. 'INDEX': 'myapp'. } } The search settings customise the search results templates as well as choosing a custom backend for ...
Read more >
Result Settings Guide | Elastic App Search Documentation [8.5]
Result Settings help you perfect the search results delivered by your search experience. How? You can change the default result returned by the...
Read more >
Wagtail Search in CMS - Stack Overflow
If you're running 2.14.x or earlier, you'll also need to set up an alternative search backend such as Postgres or Elasticsearch - the...
Read more >
wagtail Changelog - pyup.io
IndexView` for the Users index listing and search results (Mehrdad Moradizadeh) ... Adjust breadcrumb text alignment and size in page listings & page...
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