How to fetch the data from elastic Search index to api on hit through lambda function?

老子叫甜甜 提交于 2020-07-28 04:57:18

问题


  • I have created an elastic search domain.

  • Push the document into elastic search with index my-index through lambda function

https://search-xx.us-east-1.es.amazonaws.com/my-index/_search

What I have done.

I have created an api (post)

What need to do?

How to fetch the data from elastic Search index to api on hit through lambda function?

My return is null

COde to generate elastic search

import boto3
import json
from requests_aws4auth import AWS4Auth
from elasticsearch import Elasticsearch, RequestsHttpConnection
session = boto3.session.Session()
credentials = session.get_credentials()
s3 = session.resource('s3')
bucket = s3.Bucket('test20220elastic')

awsauth = AWS4Auth(credentials.access_key,
                   credentials.secret_key,
                   session.region_name, 'es',
                   session_token=credentials.token)
es = Elasticsearch(
    ['https://search-testelastic-2276kyz2u4l3basec63onfq73a.us-east-1.es.amazonaws.com'],
    http_auth=awsauth,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection
)


def lambda_handler(event, context):
    es.cluster.health()
    es.indices.create(index='my-index-1', ignore=400)
    r = [{'Name': 'Dr. Christopher DeSimone', 'Specialised and Location': 'Health'},
 {'Name': 'Dr. Tajwar Aamir (Aamir)', 'Specialised and Location': 'Health'},
 {'Name': 'Dr. Bernard M. Aaron', 'Specialised and Location': 'Health'}]
    for e in enumerate(r):
        es.index(index="my-index-1", body=e[2])
        result = es.search(index="my-index-1", body={"query": {"match_all": {}}})
        bucket.put_object(Body=json.dumps(result),Key="my_result.json",ContentType="application/json")
    {
        'statusCode': 200,
        #'body': json.dumps('API INVOKES!')
        'body':result
    }

Below is the content in the my-index

{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":3,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"my-index_1","_type":"_doc","_id":"elqrJHMB10jKFvejVaNM","_score":1.0,"_source":{"Name":"Dr. Christopher DeSimone","Specialised and Location":"Health"}},{"_index":"my-index_1","_type":"_doc","_id":"e1qrJHMB10jKFvejVqMK","_score":1.0,"_source":{"Name":"Dr. Tajwar Aamir (Aamir)","Specialised and Location":"Health"}},{"_index":"my-index_1","_type":"_doc","_id":"fFqrJHMB10jKFvejVqMR","_score":1.0,"_source":{"Name":"Dr. Bernard M. Aaron","Specialised and Location":"Health"}}]}}

来源:https://stackoverflow.com/questions/62759273/how-to-fetch-the-data-from-elastic-search-index-to-api-on-hit-through-lambda-fun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!