gpt4 book ai didi

amazon-web-services - 如何通过lambda在aws Elasticsearch 中搜索?

转载 作者:行者123 更新时间:2023-12-03 01:13:45 24 4
gpt4 key购买 nike

我正在按照以下AWS文档-https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/search-example.html进行操作,以实质上创建一个lambda函数来搜索创建的 flex 搜索域。我不清楚这里的搜索方式是如何100%清楚的,下面的示例代码正在向URL请求获取-'https://'+ host +'/'+ index +'/ _search'。这里的“/ _search”是什么。索引也是URL的一部分。索引中的索引和搜索在 flex 搜索中如何工作如果在ES域中有多个索引,并且我们想设置api网关和lambda,我们如何使其能够在多个索引中进行搜索?

import boto3
import json
import requests
from requests_aws4auth import AWS4Auth

region = '' # For example, us-west-1
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

host = '' # For example, search-mydomain-id.us-west-1.es.amazonaws.com
index = 'movies'
url = 'https://' + host + '/' + index + '/_search'

# Lambda execution starts here
def handler(event, context):

# Put the user query into the query DSL for more accurate search results.
# Note that certain fields are boosted (^).
query = {
"size": 25,
"query": {
"multi_match": {
"query": event['queryStringParameters']['q'],
"fields": ["fields.title^4", "fields.plot^2", "fields.actors", "fields.directors"]
}
}
}

# ES 6.x requires an explicit Content-Type header
headers = { "Content-Type": "application/json" }

# Make the signed HTTP request
r = requests.get(url, auth=awsauth, headers=headers, data=json.dumps(query))

# Create the response and add some extra content to support CORS
response = {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": '*'
},
"isBase64Encoded": False
}

# Add the search results to the response
response['body'] = r.text
return response

最佳答案

我认为您可以为Python使用Elasticsearch客户端:
https://elasticsearch-py.readthedocs.io/en/master/
像这样的“Pythonic”查询Elasticsearch。

关于amazon-web-services - 如何通过lambda在aws Elasticsearch 中搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63366477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com