ozil
ozil

Reputation: 669

how to search in aws elastic search via lambda?

I am working through following aws documentation - https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/search-example.html, to create essentially an lambda function to search elastic search domain created. I am not 100% clear on how the search works here, the sample code below, is making a get request to url - 'https://' + host + '/' + index + '/_search'. what is the "/_search" here. and also index is part of the URL. how does indexing and searching in the index work in elastic search. in the event there are multiple indexes in the ES domain, and we want to set up and api gateway and lambda , how we can make it such that we can search within multiple indexes?

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

Upvotes: 2

Views: 2464

Answers (2)

Joshua Dixon
Joshua Dixon

Reputation: 20

Here is an example.
The host is your endpoint to elasticSearch , datacards/datacard is your index and _search is primary a key word for search. If you use Kibana this keyword will be there for all of your searches.

url = host + '/datacards/datacard/_search'

Upvotes: 1

Raul Barreto
Raul Barreto

Reputation: 1124

I think you can use Elasticsearch client for Python:

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

It's more "Pythonic" query Elasticsearch like this.

Upvotes: 1

Related Questions