Skip to content
This repository was archived by the owner on May 5, 2026. It is now read-only.
This repository was archived by the owner on May 5, 2026. It is now read-only.

.count() can return a different result when search has already been executed #1900

Description

@knoebber

Reproduction:

>>> from elasticsearch_dsl import Search
>>> search_with_post_filter = Search(index='product').post_filter('terms', categories=['B'])
>>> search_with_post_filter.count()
2818
>>> search_with_post_filter.execute()
<Response: [...]>
>>> search_with_post_filter.count()
1

The first count() call does a normal count request, which does not respect post filters. The second .count() call uses the cached response's total hits property, which does respect post filters.

I see that this is somewhat intentional based on the comments from #518 . But, In my opinion, this behavior is a bug because the value of a method call changes depending on hidden internal state, which doesn't align with this libraries immutable API design.

I'm not sure what the ideal behavior would be - maybe never used the cached result for .count() if the search has a post filter?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions