使用AWS的elasticsearch.js

Mr.*_* B. 5 javascript amazon-web-services node.js elasticsearch

我正在使用AWS Elasticsearch服务,并希望通过elasticsearch.js进行连接,但需要端口.

看起来AWS只提供REST API(例如通过卷曲),在端口80上运行.我的群集已启动,我可以通过浏览器访问,但不能通过elasticsearch.js访问.

这个例子对我不起作用:

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'localhost:9200', // not working: '', 80, default: 443
  log: 'trace'
});

client.ping({
  requestTimeout: 1000
}, function (error) {
  if (error) {
    console.trace('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
  }
});
Run Code Online (Sandbox Code Playgroud)

我找到了http-aws-es,但它也没有用.

任何的想法?提前致谢!

Yuc*_*uci 7

NPM 包elasticsearch已被弃用,并由@elastic/elasticsearch取代

因此,您可以考虑使用包@acuris/aws-es-connectionhttp-aws-es ,它是新的 elasticsearch 客户端的 AWS ES 连接,而不是使用 ,它应该与已弃用的包一起使用。它在我的项目中对我来说效果很好。您可以在其自述文件中找到它的用法,但这里有一段示例代码:elasticsearch@elastic/elasticsearch

import {
  createAWSConnection,
  awsGetCredentials,
} from '@acuris/aws-es-connection';
import { Client } from '@elastic/elasticsearch';

export const getESClient = async () => {
  const esEndpoint = process.env.AWS_ES_ENDPOINT;
  if (!esEndpoint) {
    throw new Error(
      'AWS_ES_ENDPOINT ENV not set.'
    );
  }

  const awsCredentials = await awsGetCredentials();
  const AWSConnection = createAWSConnection(awsCredentials);
  const client = new Client({
    ...AWSConnection,
    node: esEndpoint,
  });
  return client;
};

export const createNewIndex = async (index: string) => {
  try {
    const client = await getESClient();
    const exists = await client.indices.exists({ index });
    if (!exists || !exists.statusCode || exists.statusCode !== 404) {
      console.log(`Index ${index} might alrady exist.`, exists);
      return false;
    }
    const created = await client.indices.create({
      index,
      body: {
        mappings: {
          properties: {
            product_id: {
              type: 'keyword',
            },
            product_description: {
              type: 'text',
            },
          },
        },
      },
    });
    console.log(`Index created for ${index}`, created);
  } catch (error) {
    console.log(`Error creating index ${index}`, error);
    return false;
  }
  return true;
};
Run Code Online (Sandbox Code Playgroud)


puj*_*oey 6

对于elasticsearch.client,您可以将http-aws-es用于connectionClass,将amazonES用于键.

var client = new elasticsearch.Client({
    hosts: config.elasticsearch.host,
    connectionClass: require('http-aws-es'),
    amazonES: {
        region: config.aws.region,
        accessKey: config.aws.key,
        secretKey: config.aws.secret
    }
});
Run Code Online (Sandbox Code Playgroud)