1

I am connecting to an AWS elasticsearch server and trying to fetch only one field in the response. In addition, I am not aware of the format but I am sure it works with the normal curl / postman requests.

The code :

    import collections
    from time import sleep

    import requests

    import simplejson as json
    from aws_requests_auth.aws_auth import AWSRequestsAuth
    from elasticsearch import Elasticsearch, RequestsHttpConnection, helpers
    from pyelasticsearch import ElasticSearch, bulk_chunks

    es = ElasticSearch(urls='http://'+es_host,
                      port=80)

    auth = AWSRequestsAuth(aws_access_key='XXXXXXXX',
                   aws_secret_access_key='XXXXXXXXX',
                   aws_host=es_host,
                   aws_region='us-west-2',
                   aws_service='es')

           es_client = Elasticsearch(host=es_host,
                          port=80,

        connection_class=RequestsHttpConnection,
                          http_auth=auth)

      print (es_client.info())
      print (es_client.count(index='X_data'))
      result = es_client.search(index='X_data', body={"terms":{'field':"pid"}})
      print (result)

This gives below format error. Any changes to fetch only PID field as results?

File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py", line 105, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, u'parsing_exception')

2 Answers 2

1

Use this:

client.search('X_data', _source=['pid'])
Sign up to request clarification or add additional context in comments.

Comments

-1

You are missing an outer level in your body. Specifically for "query". It should look like this:

result = es_client.search(index='X_data', body={"query": {"term":{'field':"pid"}}})

8 Comments

That would still give me : File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py", line 105, in _raise_error raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info) elasticsearch.exceptions.RequestError: TransportError(400, u'parsing_exception')
Sorry, typo. Try term query instead of terms. terms expects a list of values. Answer is updated.
Thats not helping, it only gets shard values (shards': {u'successful': 5, u'failed': 0, u'total': 5})
I would argue it did help since it fixed your original issue.. I'm not aware of a way to not get the hits key in the response. What version of ES? Also what does your data look like?
Also.. the shards key starts with an underscore, so it looks like you are truncating string data maybe? Maybe something more basic with your terminal or program output? Full result print should look like {u'hits': {u'hits': [], u'total': 0, u'max_score': None}, u'_shards': {u'successful': 5, u'failed': 0, u'skipped': 0, u'total': 5}, u'took': 0, u'timed_out': False}
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.