1 | # elasticsearch.js 16.7.1
|
2 |
|
3 | ---
|
4 |
|
5 | #### We have released the [new JavaScript client](https://www.elastic.co/blog/new-elasticsearch-javascript-client-released)!
|
6 | *In the next months this client will be deprecated, so you should start migrating your codebase as soon as possible.<br/>
|
7 | We have built a [migration guide](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/breaking-changes.html) that will help you move to the new client quickly, and if you have questions or need help, please [open an issue](https://github.com/elastic/elasticsearch-js/issues/new/choose).*
|
8 |
|
9 | ---
|
10 |
|
11 | The official low-level Elasticsearch client for Node.js and the browser.
|
12 |
|
13 | [![Coverage Status](http://img.shields.io/coveralls/elastic/elasticsearch-js/master.svg?style=flat-square)](https://coveralls.io/r/elastic/elasticsearch-js?branch=master)
|
14 | [![Dependencies up to date](http://img.shields.io/david/elastic/elasticsearch-js.svg?style=flat-square)](https://david-dm.org/elastic/elasticsearch-js)
|
15 |
|
16 | ## Features
|
17 |
|
18 | - One-to-one mapping with REST API and the other official clients
|
19 | - Generalized, pluggable architecture. See [Extending Core Components](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/extending_core_components.html)
|
20 | - Configurable, automatic discovery of cluster nodes
|
21 | - Persistent, Keep-Alive connections
|
22 | - Load balancing (with pluggable selection strategy) across all available nodes.
|
23 |
|
24 | ## Use in Node.js
|
25 |
|
26 | ```
|
27 | npm install elasticsearch
|
28 | ```
|
29 |
|
30 | [![NPM Stats](https://nodei.co/npm/elasticsearch.png?downloads=true)](https://npmjs.org/package/elasticsearch)
|
31 |
|
32 | ## Use in the Browser
|
33 |
|
34 | Check out the [Browser Builds](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/browser-builds.html) doc page for help downloading and setting up the client for use in the browser.
|
35 |
|
36 | ## Docs
|
37 | - [Quick Start](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/quick-start.html)
|
38 | - [Browser Builds](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/browser-builds.html)
|
39 | - [API](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/api-reference.html)
|
40 | - [Configuration](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/configuration.html)
|
41 | - [Development/Contributing](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/contributing.html)
|
42 | - [Extending Core Components](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/extending_core_components.html)
|
43 | - [Logging](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/logging.html)
|
44 |
|
45 |
|
46 | ## Questions?
|
47 | You can probably find help in [#kibana](https://kiwiirc.com/client/irc.freenode.net/?#kibana) on freenode.
|
48 |
|
49 |
|
50 | ## Supported Elasticsearch Versions
|
51 |
|
52 | Elasticsearch.js provides support for, and is regularly tested against, Elasticsearch releases 0.90.12 and greater. We also test against the latest changes in several branches in the Elasticsearch repository. To tell the client which version of Elasticsearch you are using, and therefore the API it should provide, set the `apiVersion` config param. [More info](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/configuration.html#config-options)
|
53 |
|
54 | ## Examples
|
55 |
|
56 | Create a client instance
|
57 | ```js
|
58 | var elasticsearch = require('elasticsearch');
|
59 | var client = new elasticsearch.Client({
|
60 | host: 'localhost:9200',
|
61 | log: 'trace',
|
62 | apiVersion: '7.2', // use the same version of your Elasticsearch instance
|
63 | });
|
64 | ```
|
65 |
|
66 | Send a HEAD request to `/` and allow up to 1 second for it to complete.
|
67 | ```js
|
68 | client.ping({
|
69 | // ping usually has a 3000ms timeout
|
70 | requestTimeout: 1000
|
71 | }, function (error) {
|
72 | if (error) {
|
73 | console.trace('elasticsearch cluster is down!');
|
74 | } else {
|
75 | console.log('All is well');
|
76 | }
|
77 | });
|
78 | ```
|
79 |
|
80 | Skip the callback to get a promise back
|
81 | ```js
|
82 | try {
|
83 | const response = await client.search({
|
84 | q: 'pants'
|
85 | });
|
86 | console.log(response.hits.hits)
|
87 | } catch (error) {
|
88 | console.trace(error.message)
|
89 | }
|
90 | ```
|
91 |
|
92 | Find tweets that have "elasticsearch" in their body field
|
93 | ```js
|
94 | const response = await client.search({
|
95 | index: 'twitter',
|
96 | type: 'tweets',
|
97 | body: {
|
98 | query: {
|
99 | match: {
|
100 | body: 'elasticsearch'
|
101 | }
|
102 | }
|
103 | }
|
104 | })
|
105 |
|
106 | for (const tweet of response.hits.hits) {
|
107 | console.log('tweet:', tweet);
|
108 | }
|
109 | ```
|
110 |
|
111 | More examples and detailed information about each method are available [here](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/index.html)
|
112 |
|
113 | ## License
|
114 |
|
115 | This software is licensed under the Apache 2 license, quoted below.
|
116 |
|
117 | Copyright (c) 2014 Elasticsearch <http://www.elasticsearch.org>
|
118 |
|
119 | Licensed under the Apache License, Version 2.0 (the "License");
|
120 | you may not use this file except in compliance with the License.
|
121 | You may obtain a copy of the License at
|
122 |
|
123 | http://www.apache.org/licenses/LICENSE-2.0
|
124 |
|
125 | Unless required by applicable law or agreed to in writing, software
|
126 | distributed under the License is distributed on an "AS IS" BASIS,
|
127 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
128 | See the License for the specific language governing permissions and
|
129 | limitations under the License.
|