如何在 api 命中中调用具有特定索引的 url 弹性搜索
How to invoke elastic search the url with particular index in api hit
我使用 aws-lambda-python
创建了 test-index
我已经创建了 api,我需要用上面的索引名称 test-index
调用 url
索引模式低于
result = es.search(index="my-index-1", body={"query": {"match_all": {}}})
如何在击中一个URL时得到结果中的内容?
import boto3
import json
from requests_aws4auth import AWS4Auth
from elasticsearch import Elasticsearch, RequestsHttpConnection
session = boto3.session.Session()
credentials = session.get_credentials()
awsauth = AWS4Auth(credentials.access_key,
credentials.secret_key,
session.region_id, 'es',
session_token=credentials.token)
es = Elasticsearch(
['https://xx.amazonaws.com'],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection
)
def lambda_handler(event, context):
es.cluster.health()
es.indices.delete(index='data', ignore=[400, 404])
es.indices.create(index='data', ignore=400)
r = [{'id': '1', 'data': 'Health'},
{'id': '2', 'data': 'countries'},
{'id': '3', 'data': 'currency'},
{'id': '4', 'data': 'language'}]
for e in enumerate(r):
es.index(index="data", body=e[1])
result = es.search(index="data", body={"query": {"match_all": {}}})
return{
'statusCode': 200,
#'body': json.dumps('API INVOKES!')
'body':result
}
我使用 aws-lambda-python
test-index
我已经创建了 api,我需要用上面的索引名称 test-index
调用 url
索引模式低于
result = es.search(index="my-index-1", body={"query": {"match_all": {}}})
如何在击中一个URL时得到结果中的内容?
import boto3
import json
from requests_aws4auth import AWS4Auth
from elasticsearch import Elasticsearch, RequestsHttpConnection
session = boto3.session.Session()
credentials = session.get_credentials()
awsauth = AWS4Auth(credentials.access_key,
credentials.secret_key,
session.region_id, 'es',
session_token=credentials.token)
es = Elasticsearch(
['https://xx.amazonaws.com'],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection
)
def lambda_handler(event, context):
es.cluster.health()
es.indices.delete(index='data', ignore=[400, 404])
es.indices.create(index='data', ignore=400)
r = [{'id': '1', 'data': 'Health'},
{'id': '2', 'data': 'countries'},
{'id': '3', 'data': 'currency'},
{'id': '4', 'data': 'language'}]
for e in enumerate(r):
es.index(index="data", body=e[1])
result = es.search(index="data", body={"query": {"match_all": {}}})
return{
'statusCode': 200,
#'body': json.dumps('API INVOKES!')
'body':result
}