|
| 1 | +[[getting-started-js]] |
| 2 | +== Getting started |
| 3 | + |
| 4 | +This page guides you through the installation process of the Node.js client, |
| 5 | +shows you how to instantiate the client, and how to perform basic Elasticsearch |
| 6 | +operations with it. |
| 7 | + |
| 8 | +[discrete] |
| 9 | +=== Requirements |
| 10 | + |
| 11 | +* https://nodejs.org/[Node.js] version 14.x or newer |
| 12 | +* https://docs.npmjs.com/downloading-and-installing-node-js-and-npm[`npm`], usually bundled with Node.js |
| 13 | + |
| 14 | +[discrete] |
| 15 | +=== Installation |
| 16 | + |
| 17 | +To install the latest version of the client, run the following command: |
| 18 | + |
| 19 | +[source,shell] |
| 20 | +-------------------------- |
| 21 | +npm install @elastic/elasticsearch |
| 22 | +-------------------------- |
| 23 | + |
| 24 | +Refer to the <<installation>> page to learn more. |
| 25 | + |
| 26 | + |
| 27 | +[discrete] |
| 28 | +=== Connecting |
| 29 | + |
| 30 | +You can connect to the Elastic Cloud using an API key and the Elasticsearch |
| 31 | +endpoint. |
| 32 | + |
| 33 | +[source,js] |
| 34 | +---- |
| 35 | +const { Client } = require('@elastic/elasticsearch') |
| 36 | +const client = new Client({ |
| 37 | + node: 'https://...', // Elasticsearch endpoint |
| 38 | + auth: { |
| 39 | + apiKey: { // API key ID and secret |
| 40 | + id: 'foo', |
| 41 | + api_key: 'bar', |
| 42 | + } |
| 43 | + } |
| 44 | +}) |
| 45 | +---- |
| 46 | + |
| 47 | +Your Elasticsearch endpoint can be found on the **My deployment** page of your |
| 48 | +deployment: |
| 49 | + |
| 50 | +image::images/es-endpoint.jpg[alt="Finding Elasticsearch endpoint",align="center"] |
| 51 | + |
| 52 | +You can generate an API key on the **Management** page under Security. |
| 53 | + |
| 54 | +image::images/create-api-key.png[alt="Create API key",align="center"] |
| 55 | + |
| 56 | +For other connection options, refer to the <<client-connecting>> section. |
| 57 | + |
| 58 | + |
| 59 | +[discrete] |
| 60 | +=== Operations |
| 61 | + |
| 62 | +Time to use Elasticsearch! This section walks you through the basic, and most |
| 63 | +important, operations of Elasticsearch. |
| 64 | + |
| 65 | + |
| 66 | +[discrete] |
| 67 | +==== Creating an index |
| 68 | + |
| 69 | +This is how you create the `my_index` index: |
| 70 | + |
| 71 | +[source,js] |
| 72 | +---- |
| 73 | +await client.indices.create({ index: 'my_index' }) |
| 74 | +---- |
| 75 | + |
| 76 | + |
| 77 | +[discrete] |
| 78 | +==== Indexing documents |
| 79 | + |
| 80 | +This is a simple way of indexing a document: |
| 81 | + |
| 82 | +[source,js] |
| 83 | +---- |
| 84 | +await client.index({ |
| 85 | + index: 'my_index', |
| 86 | + id: 'my_document_id', |
| 87 | + document: { |
| 88 | + foo: 'foo', |
| 89 | + bar: 'bar', |
| 90 | + }, |
| 91 | +}) |
| 92 | +---- |
| 93 | + |
| 94 | + |
| 95 | +[discrete] |
| 96 | +==== Getting documents |
| 97 | + |
| 98 | +You can get documents by using the following code: |
| 99 | + |
| 100 | +[source,js] |
| 101 | +---- |
| 102 | +await client.get({ |
| 103 | + index: 'my_index', |
| 104 | + id: 'my_document_id', |
| 105 | +}) |
| 106 | +---- |
| 107 | + |
| 108 | + |
| 109 | +[discrete] |
| 110 | +==== Searching documents |
| 111 | + |
| 112 | +This is how you can create a single match query with the client: |
| 113 | + |
| 114 | +[source,js] |
| 115 | +---- |
| 116 | +await client.search({ |
| 117 | + query: { |
| 118 | + match: { |
| 119 | + foo: 'foo' |
| 120 | + } |
| 121 | + } |
| 122 | +}) |
| 123 | +---- |
| 124 | + |
| 125 | + |
| 126 | +[discrete] |
| 127 | +==== Updating documents |
| 128 | + |
| 129 | +This is how you can update a document, for example to add a new field: |
| 130 | + |
| 131 | +[source,js] |
| 132 | +---- |
| 133 | +await client.update({ |
| 134 | + index: 'my_index', |
| 135 | + id: 'my_document_id', |
| 136 | + doc: { |
| 137 | + foo: 'bar', |
| 138 | + new_field: 'new value' |
| 139 | + } |
| 140 | +}) |
| 141 | +---- |
| 142 | + |
| 143 | + |
| 144 | +[discrete] |
| 145 | +==== Deleting documents |
| 146 | + |
| 147 | +[source,js] |
| 148 | +---- |
| 149 | +await client.delete({ |
| 150 | + index: 'my_index', |
| 151 | + id: 'my_document_id', |
| 152 | +}) |
| 153 | +---- |
| 154 | + |
| 155 | + |
| 156 | +[discrete] |
| 157 | +==== Deleting an index |
| 158 | + |
| 159 | +[source,js] |
| 160 | +---- |
| 161 | +await client.indices.delete({ index: 'my_index' }) |
| 162 | +---- |
| 163 | + |
| 164 | + |
| 165 | +[discrete] |
| 166 | +== Further reading |
| 167 | + |
| 168 | +* Use <<client-helpers>> for a more comfortable experience with the APIs. |
| 169 | +* For an elaborate example of how to ingest data into Elastic Cloud, |
| 170 | +refer to {cloud}/ec-getting-started-node-js.html[this page]. |
0 commit comments