如何通过NodeJS调用elasticsearch api? [英] How to make calls to elasticsearch apis through NodeJS?

查看:955
本文介绍了如何通过NodeJS调用elasticsearch api?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是对弹性搜索api进行POST api调用,

I have been tasked with making a POST api call to elastic search api,

https://search-test-search-fqa4l6ubylznt7is4d5yxlmbxy.us-west-2.es.amazonaws.com/klove-ddb/recipe/_search

我以前没有对AWS服务进行api调用的经验.

I don't have any previous experience with making api calls to AWS services.

所以,我尝试了-

axios.post('https://search-test-search-fqa4l6ubylznt7is4d5yxlmbxy.us-west-2.es.amazonaws.com/klove-ddb/recipe/_search')
            .then(res => res.data)
            .then(res => console.log(res));

但是我收到{"Message":用户:匿名用户无权执行:es:ESHttpPost"}

But I was getting {"Message":"User: anonymous is not authorized to perform: es:ESHttpPost"}

我还签出了一些IAM角色,并将AWSESFullAccess策略添加到我的配置文件中.

I also checked out with some IAM roles and added AWSESFullAccess policies to my profile.

仍然无法解决任何问题.

Still I can't make anything work out.

请帮助我.

推荐答案

看到错误User: anonymous is not authorized to perform: es:ESHttpPost的原因是因为您在请求数据时没有让ElasticSearch知道您是谁-这就是为什么它说匿名"的原因

The reason your seeing the error User: anonymous is not authorized to perform: es:ESHttpPost is because you're making requesting data without letting ElasticSearch know who you are - this is why it says 'Anonymous'.

有两种身份验证方式,最简单的方法是使用 elasticsearch库.使用此库,您将为IAM角色/用户提供一组凭据(访问密钥,秘密密钥).它将使用它来创建签名的请求.签名的请求将使AWS知道是谁在实际发出请求,因此不会以匿名方式接收,而是您自己.

There are a couple ways of authentication, the easiest being using the elasticsearch library. With this library you'll give the library a set of credentials (access key, secret key) to the IAM role / user. It will use this to create signed requests. Signed requests will let AWS know who's actually making the request, so it won't be received as anonymous, but rather, yourself.

另一种可行的方法是将访问策略调整为基于IP:

Another way of getting this to work is to adjust your access policy to be IP-based:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "es:*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [
                        "AAA.BBB.CCC.DDD"
                    ]
                }
            },
            "Resource": "YOUR_ELASTICSEARCH_CLUSTER_ARN"
        }
    ]
}

对于任何使用您在此处提供的ip(范围)的人,此特定策略都是开放的.但是,这将使您免于必须签署请求的麻烦.

This particular policy will be wide open for anyone with the ip(range) that you provide here. It will spare you the hassle of having to go through signing your requests though.

帮助使用AWS ES设置elasticsearch-js的库是此库

以下是一个工作示例:

const AWS = require('aws-sdk')
const elasticsearch = require('elasticsearch')
const awsHttpClient = require('http-aws-es')

let client = elasticsearch.Client({
    host: '<YOUR_ES_CLUSTER_ID>.<YOUR_ES_REGION>.es.amazonaws.com',
    connectionClass: awsHttpClient,
    amazonES: {
        region: '<YOUR_ES_REGION>',
        credentials: new AWS.Credentials('<YOUR_ACCESS_KEY>', '<YOUR_SECRET_KEY>')
    }
});

client.search({
    index: 'twitter',
    type: 'tweets',
    body: {
        query: {
            match: {
                body: 'elasticsearch'
            }
        }
    }
})
.then(res => console.log(res));

这篇关于如何通过NodeJS调用elasticsearch api?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆