如何仅在响应ElasticSearch中返回单个字段-Python?

时间:2018-10-15 18:47:10

标签: python elasticsearch

我正在连接到AWS Elasticsearch服务器,并尝试仅获取响应中的一个字段,但是我不知道该格式,但是我确定它可以与普通的curl / postman请求一起使用。

代码:

    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)

这会导致格式错误,任何更改以仅获取pid字段作为结果?

      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 个答案:

答案 0 :(得分:0)

您缺少体内的外部水平。专门用于“查询”。它应该看起来像这样:

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

答案 1 :(得分:0)

使用此:

client.search('X_data', _source=['pid'])