调用json out时返回null的API

2024-04-20 07:04:50 发布

您现在位置:Python中文网/ 问答频道 /正文

代码如下

  • 我已经创建了一个弹性搜索域

  • 通过lambda function将文档推入索引my index的弹性搜索

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

我所做的一切

我已经创建了一个api(post)

你需要做什么

如何通过lambda函数将弹性搜索索引中的数据提取到api中

我的报税表无效

生成弹性搜索的代码

  • 如何创建包 1.在本地环境中创建环境

2.使用pip安装必要的库(elasticsearch、requests、requests\u aws4auth、boto3)

3.使用lambda_function.py在env\Lib\site packages\内创建文件,并添加以下代码

4.压缩上述文件夹并将其命名为lambda_function.Zip,然后上传到lambda function中,您可以在其中创建具有必要IAM角色的函数

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
    }

下面是我的索引中的内容

{"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"}}]}}

1条回答
网友
1楼 · 发布于 2024-04-20 07:04:50
return missing in the code

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'}]
    for e in enumerate(r):
        es.index(index="my-index-1", body=e[1])
        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")
    return{
        'statusCode': 200,
        #'body': json.dumps('API INVOKES!')
        'body':result
    }

相关问题 更多 >