【问题标题】:how to search in aws elastic search via lambda?如何通过 lambda 在 aws 弹性搜索中进行搜索?
【发布时间】:2020-12-01 14:37:21
【问题描述】:

我正在通过以下 aws 文档 - https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/search-example.html 工作,以创建本质上的 lambda 函数来搜索创建的弹性搜索域。我不是 100% 清楚这里的搜索是如何工作的,下面的示例代码正在向 url 发出获取请求 - 'https://' + host + '/' + index + '/_search'。这里的“/_search”是什么。并且 index 也是 URL 的一部分。索引中的索引和搜索如何在弹性搜索中工作。如果 ES 域中有多个索引,并且我们要设置 api gateway 和 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

【问题讨论】:

    标签: amazon-web-services elasticsearch aws-lambda


    【解决方案1】:

    这是一个例子。
    主机是 elasticSearch 的端点, datacards/datacard 是您的索引, _search 是搜索的主要关键字。如果您使用 Kibana,则此关键字将用于您的所有搜索。

    url = 主机 + '/datacards/datacard/_search'

    【讨论】:

      【解决方案2】:

      我认为你可以使用 Python 的 Elasticsearch 客户端:

      https://elasticsearch-py.readthedocs.io/en/master/

      更像是这样的“Pythonic”查询 Elasticsearch。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-26
        • 2018-07-26
        • 2017-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-02
        • 1970-01-01
        相关资源
        最近更新 更多