How to use @elastic/elasticsearch - 10 common examples

To help you get started, we’ve selected a few @elastic/elasticsearch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nasa / cumulus / packages / api / lambdas / bulk-operation.js View on Github external
if (payload.ids) {
    return applyWorkflowToGranules(payload.ids, workflowName, queueName);
  }

  log.info('No granule ids detected. Searching for granules in Elasticsearch.');

  if (!process.env.METRICS_ES_HOST
    || !process.env.METRICS_ES_USER
    || !process.env.METRICS_ES_PASS) {
    throw new Error('No ELK metrics stack configured.');
  }

  const query = payload.query;
  const index = payload.index;
  const client = new elasticsearch.Client({
    node: process.env.METRICS_ES_HOST,
    auth: {
      username: process.env.METRICS_ES_USER,
      password: process.env.METRICS_ES_PASS
    }
  });

  // TO DO
  // Update to take the search repsonse, get graules, and kick off workflows
  const searchResponse = await client.search({ index, body: query });
  return searchResponse;
  // Request against elastic search with pagenation
  // page through response, for each item in each page, applyWorkflow
}
github microsoft / pai / src / rest-server / src / models / v2 / job-attempt.js View on Github external
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// module dependencies
const _ = require('lodash');
const {Client} = require('@elastic/elasticsearch');
const base32 = require('base32');
const {isNil} = require('lodash');

const {convertToJobAttempt} = require('@pai/utils/frameworkConverter');
const launcherConfig = require('@pai/config/launcher');
const createError = require('@pai/utils/error');
const k8sModel = require('@pai/models/kubernetes');

let elasticSearchClient;
if (!_.isNil(process.env.ELASTICSEARCH_URI)) {
  elasticSearchClient = new Client({node: process.env.ELASTICSEARCH_URI});
}

const convertName = (name) => {
  // convert framework name to fit framework controller spec
  return name.toLowerCase().replace(/[^a-z0-9]/g, '');
};

const encodeName = (name) => {
  if (name.startsWith('unknown') || !name.includes('~')) {
    // framework is not generated by PAI
    return convertName(name.replace(/^unknown/g, ''));
  } else {
    // base32 encode
    return base32.encode(name);
  }
};
github kuzzleio / kuzzle / lib / util / esWrapper.js View on Github external
formatESError(error) {
    if (error instanceof KuzzleError) {
      return error;
    }

    if (error instanceof es.errors.NoLivingConnectionsError) {
      errorsManager.throw('not_connected');
    }
    const message = _.get(error, 'meta.body.error.reason', error.message);

    // Try to match a known elasticsearch error
    for (const mapping of errorMessagesMapping) {
      const matches = message.match(mapping.regex);

      if (matches) {
        return errorsManager.get(
          mapping.subcode,
          ...mapping.getPlaceholders(matches));
      }
    }

    // Try to match using error codes
github vendure-ecommerce / vendure / packages / elasticsearch-plugin / src / plugin.ts View on Github external
static init(options: ElasticsearchOptions): Type {
        const { host, port } = options;
        this.options = mergeWithDefaults(options);
        this.client = new Client({
            node: `${host}:${port}`,
        });
        return ElasticsearchPlugin;
    }
github OpenCTI-Platform / opencti / opencti-platform / opencti-graphql / src / database / elasticSearch.js View on Github external
'valid_until_day',
  'valid_until_month',
  'observable_date'
];
const numberFields = ['object_status', 'phase_order', 'level'];
const virtualTypes = ['Identity', 'Email', 'File', 'Stix-Domain-Entity', 'Stix-Domain', 'Stix-Observable'];

export const REL_INDEX_PREFIX = 'rel_';
export const INDEX_STIX_OBSERVABLE = 'stix_observables';
export const INDEX_STIX_ENTITIES = 'stix_domain_entities';
export const INDEX_STIX_RELATIONS = 'stix_relations';
export const INDEX_WORK_JOBS = 'work_jobs_index';
export const PLATFORM_INDICES = [INDEX_STIX_ENTITIES, INDEX_STIX_RELATIONS, INDEX_STIX_OBSERVABLE, INDEX_WORK_JOBS];

export const forceNoCache = () => conf.get('elasticsearch:noQueryCache') || false;
export const el = new Client({ node: conf.get('elasticsearch:url') });

export const elIsAlive = async () => {
  try {
    await el.info().then(info => {
      if (info.meta.connection.status !== 'alive') {
        logger.error(`[ELASTICSEARCH] Seems down`);
        throw new Error('elastic seems down');
      }
      return true;
    });
  } catch (e) {
    logger.error(`[ELASTICSEARCH] Seems down`);
    throw new Error('elastic seems down');
  }
};
export const elVersion = () => {
github kuzzleio / kuzzle / bin / .upgrades / connectors / es.js View on Github external
choices: ['source', 'target', 'source and target'],
      default: 'target',
    },
    {
      type: 'input',
      message: ({ current }) => `Enter the URL for the ${current === 'source' ? 'target': 'source'} instance:`,
      name: 'url',
      when: ({ current }) => current !== 'source and target',
      default: '',
      validate: url => validator.isURL(url) || 'A valid URL must be provided'
    }
  ]);

  const
    current = new Client(currentConfiguration),
    next = answers.url ? new Client({ node: answers.url }) : current;

  return answers.current === 'source'
    ? new ConnectorContext(context, current, next)
    : new ConnectorContext(context, next, current);
}
github fastify / fastify-elasticsearch / index.js View on Github external
async function fastifyElasticsearch (fastify, options) {
  const { namespace, healthcheck } = options
  delete options.namespace
  delete options.healthcheck

  const client = options.client || new Client(options)

  if (healthcheck !== false) {
    await client.ping()
  }

  if (namespace) {
    if (!fastify.elastic) {
      fastify.decorate('elastic', {})
    }

    if (fastify.elastic[namespace]) {
      throw new Error(`Elasticsearch namespace already used: ${namespace}`)
    }

    fastify.elastic[namespace] = client
github Emurgo / tangata-manu / src / entities / elastic-storage / elastic-storage-processor.js View on Github external
constructor(
    logger: Logger,
    elasticConfig: ElasticConfigType,
    networkConfig: NetworkConfig,
  ) {
    this.logger = logger
    this.elasticConfig = elasticConfig
    this.client = new Client({ node: elasticConfig.node })
    this.networkStartTime = networkConfig.startTime()
  }
github mediathekview / mediathekviewweb / server / src / instance-provider.ts View on Github external
return singleton(ElasticsearchClient, async () => {
      const logger = InstanceProvider.logger(ELASTICSEARCH_LOG);
      const logAdapter = getElasticsearchLogAdapter(logger);

      const elasticsearchClient = new ElasticsearchClient({
        node: config.elasticsearch.url
      });

      InstanceProvider.disposer.addDisposeTasks(async () => elasticsearchClient.close());

      await InstanceProvider.connect('elasticsearch', async () => await elasticsearchClient.ping() as Promise, logger);

      return elasticsearchClient;
    });
  }
github cube-js / cube.js / packages / cubejs-elasticsearch-driver / driver / ElasticSearchDriver.js View on Github external
constructor(config) {
    super();
    this.config = {
      url: process.env.CUBEJS_DB_URL,
      openDistro:
        (process.env.CUBEJS_DB_ELASTIC_OPENDISTRO || 'false').toLowerCase() === 'true' ||
        process.env.CUBEJS_DB_TYPE === 'odelasticsearch',
      ...config
    };
    this.client = new Client({ node: this.config.url });
    this.sqlClient = this.config.openDistro ? new Client({ node: `${this.config.url}/_opendistro` }) : this.client;
  }

@elastic/elasticsearch

The official Elasticsearch client for Node.js

Apache-2.0
Latest version published 29 days ago

Package Health Score

98 / 100
Full package analysis

Popular @elastic/elasticsearch functions