| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 | = elasticsearch-js== OverviewOfficial low-level client for Elasticsearch. Its goal is to provide commonground for all Elasticsearch-related code in JavaScript; because of this it triesto be opinion-free and very extendable.The full documentation is available at http://elasticsearch.github.io/elasticsearch-js=== Getting the Node.js moduleTo install the module into an existing Node.js project use npm:[source,sh]------------------------------------npm install elasticsearch------------------------------------=== Getting the browser clientFor a browser-based projects, builds for modern browsers are available http://elasticsearch.github.io/elasticsearch-js#browser-builds[here]. Download one of the archives and extract it, inside you'll find three files, pick the one that best matches your environment: * elasticsearch.jquery.js - for projects that already use jQuery * elasticsearch.angular.js - for Angular projects * elasticsearch.js - generic build for all other projectsEach of the library specific builds tie into the AJAX and Promise creation facilities provided by their respective libraries. This is an example of how Elasticsearch.js can be extended to provide a more opinionated approach when appropriate.=== Setting up the clientNow you are ready to get busy! First thing you'll need to do is create an instance of `elasticsearch.Client`. Here are several examples of configuration parameters you can use when creating that instance. For a full list of configuration options see http://elasticsearch.github.io/elasticsearch-js/index.html#configuration[the configuration docs].[source,javascript]------------------------------------var elasticsearch = require('elasticsearch');// Connect to localhost:9200 and use the default settingsvar client = new elasticsearch.Client();// Connect the client to two nodes, requests will be// load-balanced between them using round-robinvar client = elasticsearch.Client({  hosts: [    'elasticsearch1:9200',    'elasticsearch2:9200'  ]});// Connect to the this host's cluster, sniff// for the rest of the cluster right away, and// again every 5 minutesvar client = elasticsearch.Client({  host: 'elasticsearch1:9200',  sniffOnStart: true,  sniffInterval: 300000});// Connect to this host using https, basic auth,// a path prefix, and static query string valuesvar client = new elasticsearch.Client({  host: 'https://user:password@elasticsearch1/search?app=blog'});------------------------------------=== Setting up the client in the browserThe params accepted by the `Client` constructor are the same in the browser versions of the client, but how you access the Client constructor is different based on the build you are using. Below is an example of instantiating a client in each build.[source,javascript]------------------------------------// elasticsearch.js adds the elasticsearch namespace to the windowvar client = elasticsearch.Client({ ... });// elasticsearch.jquery.js adds the es namespace to the jQuery objectvar client = jQuery.es.Client({ ... });// elasticsearch.angular.js creates an elasticsearch// module, which provides an esFactoryvar app = angular.module('app', ['elasticsearch']);app.service('es', function (esFactory) {  return esFactory({ ... });});------------------------------------=== Using the client instance to make API calls.Once you create the client, making API calls is simple.[source,javascript]------------------------------------// get the current status of the entire cluster.// Note: params are always optional, you can just send a callbackclient.cluster.health(function (err, resp) {  if (err) {    console.error(err.message);  } else {    console.dir(resp);  }});// index a documentclient.index({  index: 'blog',  type: 'post',  id: 1,  body: {    title: 'JavaScript Everywhere!',    content: 'It all started when...',    date: '2013-12-17'  }}, function (err, resp) {  // ...});// search for documents (and also promises!!)client.search({  index: 'users',  size: 50,  body: {    query: {      match: {        profile: 'elasticsearch'      }    }  }}).then(function (resp) {  var hits = resp.body.hits;});------------------------------------== Copyright and LicenseThis software is Copyright (c) 2013 by Elasticsearch BV.This is free software, licensed under The Apache License Version 2.0.
 |