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

response#hits will raise a KeyError if use filter_path=hits.hits._id on an empty result #1605

Open
BrickXu opened this issue Jun 2, 2022 · 0 comments
Labels

Comments

@BrickXu
Copy link

BrickXu commented Jun 2, 2022

Hi all,

My query specify the filter_path=hits.hits.<some_fields>, response will be an empty json object like the following if no documents matched:

curl http://127.0.0.1:9200/twitter/_search\?filter_path\=hits.hits._id
{}

And I use high-level client to execute this query, it will raise a KeyError instead.

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

client = Elasticsearch('http://localhost:9200')
s = Search(using=client, index='twitter').params(filter_path='hits.hits._id')
response = s.execute(ignore_cache=True)

print response.hits

=====================
Traceback (most recent call last):
  File "/Users/brick/Workspaces/python/elasticsearch-py-demo/demo.py", line 8, in <module>
    print response.hits
  File "/Users/brick/.venv/elasticsearch-py-demo/lib/python2.7/site-packages/elasticsearch_dsl/response/__init__.py", line 64, in hits
    h = self._d_["hits"]
KeyError: 'hits'

I think it would use h.get("hits", []) instead of h["hits"]:

hits = AttrList(map(self._search._get_result, h["hits"]))

@miguelgrinberg miguelgrinberg added the Category: Bug Something isn't right label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants