npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vinyl-elasticsearch

v1.13.0

Published

Presents ElasticSearch as a destination stream of Vinyl objects.

Downloads

257

Readme

vinyl-elasticsearch

Read and write Vinyl documents from and to ElasticSearch.

Examples

Reading documents from ES

var gulp = require('gulp');

var elasticsearch = require('vinyl-elasticsearch');

gulp.task('search', function() {
  return elasticsearch.src(
    {
      index: 'someindex',
      size: 0,
      body: {
        // Begin query.
        query: {
          // Boolean query for matching and excluding items.
          bool: {
            must: [{match: {'somefield': 'somevalue'}}]
          }
        },
        // Aggregate on the results
        aggs: {
          actions: {
            terms: {
              field: 'additionalType',
              order: {'_term' : 'asc'},
              size: 10,
            }
          }
        }
      }
    },
    {
      host: 'https://localhost:9200',
      log: 'trace'
    }
  )
    .pipe(echo());
});

Saving documents to ES

var gulp = require('gulp');

var elasticsearch = require('vinyl-elasticsearch');

gulp.task('default', function (){
  return gulp.src('hello.json')
    .pipe(
      elasticsearch.dest(
        {
          index: 'someindex',
          type: 'somedoc'
        },
        {
          host: 'https://localhost:9200',
          log: 'trace'
        }
      )
    );
});

API

Options

The configuration for the connection to ES comes from the opt parameter, which is used when creating the client. Possible options are described at ElasticSearch Configuration.

In addtion to the ElasticSearch options, a few more have been added to help manage templates, use the AWS version of ElasticSearch, and to configure retries.

Managing Templates

If opt.manageTemplate is true, then templates will be maintained automatically on the ElasticSearch host. This involves checking for one or more templates with the names indicated in templateName, and if they don't exist (or the templateOverwrite value is set to true) then the appropriate template from the templateDir is uploaded. If any of these parameters are missing then errors will be propagated back to the caller.

For example:

{
  templateName: 'logstash,listenaction',
  templateOverwrite: true,
  templateDir: path.join(__dirname, '../fixtures/templates')
}

Using the AWS Version of ElasticSearch

In addition, if opt.amazonES is present then each message is signed. This makes it possible to use Amazon's ElasticSearch Service. Possible values for amazonES are:

amazonES: {
  region: 'someregion',
  accessKey: 'accesskeyid',
  secretKey: 'secretaccesskey'
}

Configuring Retries

Some ElasticSearch actions may fail for reasons that are temporary, such as servers being busy with nightly backups, or network failures. To help deal with this opts.retries can be set to indicate the number of retries, with the default being zero.

The retry strategy uses the retry module and the backoff strategy is described there.

dest(glob, opt)

The configuration for the connection to ES comes from the opt parameter, as described above.

The index, document type and id are usually derived from the file object being passed through, using:

  • the file.index value;
  • the file.type value, of if none is present, the file.base value;
  • the file.id value, or if none is present the file.path value.

For any of these values that is not present the corresponding value from opt will be used.

However, to make it possible to override values across the board, any settings in glob take priority.

The body of the document will be set to the data property of the Vinyl file if it's present. Otherwise the buffer in the contents property will be converted to a string and then parsed as JSON.

As of v1.7.0, writing to ES uses the bulk update API. The sequence is that each new item to be indexed is collected in memory and then the whole batch is pushed to ES in one go. The trigger to cause the push is either that the batch size has been reached (which defaults to 5000, but can be set with opt.batchSize) or that the batch timeout has elapsed (which defaults to 5000 millisecond, but can be set with opt.batchTimeout).

src(glob, opt)

The configuration for the connection to ES comes from the opt parameter, as described above.

The document returned will be the whole collection of search results, with the usual hits.hits incantation. This will probably be modified soon to allow individual results to be returned as distinct Vinyl files.