index.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. = elasticsearch-js
  2. == Overview
  3. Official low-level client for Elasticsearch. Its goal is to provide common
  4. ground for all Elasticsearch-related code in JavaScript; because of this it tries
  5. to be opinion-free and very extendable.
  6. The full documentation is available at http://elasticsearch.github.io/elasticsearch-js
  7. === Getting the Node.js module
  8. To install the module into an existing Node.js project use npm:
  9. [source,sh]
  10. ------------------------------------
  11. npm install elasticsearch
  12. ------------------------------------
  13. === Getting the browser client
  14. For 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:
  15. * elasticsearch.jquery.js - for projects that already use jQuery
  16. * elasticsearch.angular.js - for Angular projects
  17. * elasticsearch.js - generic build for all other projects
  18. Each 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.
  19. === Setting up the client
  20. Now 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].
  21. [source,javascript]
  22. ------------------------------------
  23. var elasticsearch = require('elasticsearch');
  24. // Connect to localhost:9200 and use the default settings
  25. var client = new elasticsearch.Client();
  26. // Connect the client to two nodes, requests will be
  27. // load-balanced between them using round-robin
  28. var client = elasticsearch.Client({
  29. hosts: [
  30. 'elasticsearch1:9200',
  31. 'elasticsearch2:9200'
  32. ]
  33. });
  34. // Connect to the this host's cluster, sniff
  35. // for the rest of the cluster right away, and
  36. // again every 5 minutes
  37. var client = elasticsearch.Client({
  38. host: 'elasticsearch1:9200',
  39. sniffOnStart: true,
  40. sniffInterval: 300000
  41. });
  42. // Connect to this host using https, basic auth,
  43. // a path prefix, and static query string values
  44. var client = new elasticsearch.Client({
  45. host: 'https://user:password@elasticsearch1/search?app=blog'
  46. });
  47. ------------------------------------
  48. === Setting up the client in the browser
  49. The 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.
  50. [source,javascript]
  51. ------------------------------------
  52. // elasticsearch.js adds the elasticsearch namespace to the window
  53. var client = elasticsearch.Client({ ... });
  54. // elasticsearch.jquery.js adds the es namespace to the jQuery object
  55. var client = jQuery.es.Client({ ... });
  56. // elasticsearch.angular.js creates an elasticsearch
  57. // module, which provides an esFactory
  58. var app = angular.module('app', ['elasticsearch']);
  59. app.service('es', function (esFactory) {
  60. return esFactory({ ... });
  61. });
  62. ------------------------------------
  63. === Using the client instance to make API calls.
  64. Once you create the client, making API calls is simple.
  65. [source,javascript]
  66. ------------------------------------
  67. // get the current status of the entire cluster.
  68. // Note: params are always optional, you can just send a callback
  69. client.cluster.health(function (err, resp) {
  70. if (err) {
  71. console.error(err.message);
  72. } else {
  73. console.dir(resp);
  74. }
  75. });
  76. // index a document
  77. client.index({
  78. index: 'blog',
  79. type: 'post',
  80. id: 1,
  81. body: {
  82. title: 'JavaScript Everywhere!',
  83. content: 'It all started when...',
  84. date: '2013-12-17'
  85. }
  86. }, function (err, resp) {
  87. // ...
  88. });
  89. // search for documents (and also promises!!)
  90. client.search({
  91. index: 'users',
  92. size: 50,
  93. body: {
  94. query: {
  95. match: {
  96. profile: 'elasticsearch'
  97. }
  98. }
  99. }
  100. }).then(function (resp) {
  101. var hits = resp.body.hits;
  102. });
  103. ------------------------------------
  104. == Copyright and License
  105. This software is Copyright (c) 2013-2015 by Elasticsearch BV.
  106. This is free software, licensed under The Apache License Version 2.0.