How to check if a doc has not a field

Hi everyone,
I imported into Elasticsearch a set of emails (Enron corpus) which contains some email that has not the to header field (these emails have been sent to undisclosed recipients). However the indexing phase goes well, but when I try to query the database and get some fields of the emails and print them in the shell something goes wrong, because the query doesn't find the to field.

So I tried to put an if clause in order to print the x-to field whenever the to one is missing.

This is the code:

s = Search(using=client, index="enron_test")
s = s.source(['message_body', 'from', 'to', 'x-to'])

response = s.execute()

for hit in response['hits']['hits']:
    print("FROM: " + hit['_source']['from'])
    print("TO: "),
    if hit['_source']['to'] not in response['hits']['hits']:
        print(hit['_source']['x-to'])
    else:
        print(hit['_source']['to'])
    print("\n" + hit['_source']['message_body'])
    print(line + "\n")

It gives me this error:

 Traceback (most recent call last):
 File "rico.py", line 24, in <module>
 if hit['_source']['to'] not in response['hits']['hits']:
 File "/home/rico/.local/lib/python2.7/site-packages/elasticsearch_dsl/utils.py", line 125, in __getitem__
 return _wrap(self._d_[key])
 KeyError: 'to'

Anyone can help me in solving this problem?

I think your if condition should be if 'to' not in hit['_source]

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.