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

elasticness

v0.1.3

Published

Simple elasticsearch client

Downloads

5

Readme

Elasticness

This is a simple elasticsearch client for node.js. It wraps the http requests to your elasticsearch server. Here's how you use it.

var ElasticClient = require('elastic-client');
var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'

, indexProperties: {
    /* ElasticSearch index options. Stuff like mappings and analyzers */
  }
});

Docs

This module exports a constructor that has a couple of static methods defined on it as well. Please refer to the elasticsearch documentation for method options.

Static Methods

Client.createIndex

Explicitly create an index - really shouldn't be used ever because elastic search will automatically create new indexes when saving documents

@param  {String}   host     Hostname
@param  {Number}   port     Port of the host
@param  {String}   name     Name of the index
@param  {Object}   options  Options for the elasticsearch index
@param  {Function} callback Obviously the callback(error)

Example:

var ElasticClient = require('elastic-client');

ElasticClient.createIndex(
  'http://es.j0.hn/'  // Host
, 9200                // Port
, 'my-index'          // Index Name
, {                   // Normal ES index options like mappings
    mappings: {
      /* ... */
    }
  }
, function(error, result){ /* ... */ }
)

Client.deleteIndex

Remove an index

@param  {String}   host     Hostname
@param  {Number}   port     Port of the host
@param  {String}   name     Name of the index
@param  {Object}   options  Options for the elasticsearch index
@param  {Function} callback Obviously the callback(error)

Example:

var ElasticClient = require('elastic-client');

ElasticClient.deleteIndex(
  'http://es.j0.hn/'  // Host
, 9200                // Port
, 'my-index'          // Index Name
, function(error, result){ /* ... */ }
)

Instance Methods

Client.prototype.ensureIndex

Ensure the index that this client is associated to has been created

@param  {Object}   options  [Optional] Options for the elasticsearch index
@param  {Function} callback [Optional] Obviously the callback(error)

Example:

var ElasticClient = require('elastic-client');

var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'
});

elastic.ensureIndex(function(error, result){
  /* ... */
});

Client.prototype.removeSelf

Removes the index that this client is associated to

@param  {Object}   options  [Optional] Options for the removal
@param  {Function} callback [Optional] Obviously the callback(error)

Example:

var ElasticClient = require('elastic-client');

var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'
});

elastic.removeSelf(function(error, result){
  /* ... */
});

Client.prototype.save

Add or update a document making it searchable

If an ID is supplied, an update is made to that resource. Otherwise, a POST is made. Either way, it doesnt really matter. ES will generate an id for you if you did not supply one. Please refer to ES documentation for the response sent to the client.

Also,

PUT /{index}/{type}/{id}
    /staging/products/123
See http://www.elasticsearch.org/guide/reference/api/index_/
for more details

@param  {String}   type     elastic search document type
@param  {Object}   doc      The document to add or update
@param  {Function} callback Obviously the callback(error, result)

Example:

var ElasticClient = require('elastic-client');

var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'
});

var doc = {
  id: 111111111111
, name: 'Some Product'
, price: 3400
};

elastic.save('product', doc, function(error, result){
  /* ... */
});

Client.prototype.get

Get a single document from elasticsearch

@param  {String}   type     Elasticsearch document type
@param  {Number}   id       ID of document - obviously can be String as well
@param  {Function} callback Obviously the callback(error, result)

Example:

var ElasticClient = require('elastic-client');

var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'
});

elastic.get('product', 123, function(error, result){
  /* ... */
});

Client.prototype.getMapping

Get the mapping for a type

@param  {String}   type     Elasticsearch document type
@param  {Function} callback Obviously the callback(error, result)

Example:

var ElasticClient = require('elastic-client');

var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'
});

myClient.getMapping('product', function(error, result){
  /* result.mapping.product */
});

Client.prototype.analyze

Runs test input on an analyzer. Returns the tokens the analyzer will match

@param  {String}   analyzer The analyzer to be run
@param  {String}   text     The test input
@param  {Function} callback Obviously the callback(error, result)

Example:

var ElasticClient = require('elastic-client');

var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'

, indexProperties: {
    settings: {
      analysis: {
        analyzer: {
          my_custom_analyzer: {
            type:      'custom'
          , tokenizer: 'standard'

          , filter: [
              'standard'
            , 'lowercase'
            , 'asciifolding'
            ]
          }
        }
      }
    }
  }
});

myClient.ensureIndex(function(error, result){
  var input = 'This is Token\'s Analyzer';

  myClient.analyze('my_custom_analyzer', input, function(error, result){
    /* result.tokens */
  });
});

Client.prototype.search

Perform a search on elasticserach For all properties for search, see: http://www.elasticsearch.org/guide/reference/api/search/

Most searches use the query object, which uses the query-dsl: http://www.elasticsearch.org/guide/reference/query-dsl/

{
  query:   { ... }
, sort:    { ... }
, fields:  { ... }
, ...
}

@param  {String}   type     Optional elastic search doc type
@param  {Object}   query    Elastic search query dsl object
@param  {Function} callback Obviously the callback(error, results)

Example:

var ElasticClient = require('elastic-client');

var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'
});

var $query = {
  query: {
    match: {
      name: data.product
    }
  }
};

elastic.search('product', $query, function(error, result){
  /* result.hits */
});

Example:

If you're searching on multiple fields, you might try the multi_match search property.

var $query = {
  query: {
    multi_match: {
      query: data.business.substring(0, 4)
    , fields: [
        'name'
      , 'name.partial'
      , 'businessName'
      , 'businessName.partial'
      ]
    }
  }
};

Client.prototype.del

Remove an item from elasticsearch

@param  {String}   type     Elasticsearch document type
@param  {Number}   id       ID of document - obviously can be String as well
@param  {Function} callback Obviously the callback(error, result)

Example:

var ElasticClient = require('elastic-client');

var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'
});

myClient.del('product', 37, function(error, result){
  /* ... */
});

Client.prototype.info

Get server info

@param  {Function} callback Obviously the callback(error, result)

Example:

var ElasticClient = require('elastic-client');

var elastic = new ElasticClient({
  host:   'http://localhost' 
, port:   9200
, index:  'elastic-staging'
});

myClient.info(function(error, result){
  /* ... */
});