|
| 1 | +[[getting-started-ruby]] |
| 2 | +== Getting started |
| 3 | + |
| 4 | +This page guides you through the installation process of the Ruby client, shows |
| 5 | +you how to instantiate the client, and how to perform basic Elasticsearch |
| 6 | +operations with it. |
| 7 | + |
| 8 | +[discrete] |
| 9 | +=== Requirements |
| 10 | + |
| 11 | +A currently maintained version of Ruby (3.0+) or JRuby (9.3+). |
| 12 | + |
| 13 | +[discrete] |
| 14 | +=== Installation |
| 15 | + |
| 16 | +To install the latest version of the client, run the following command: |
| 17 | + |
| 18 | +[source,shell] |
| 19 | +-------------------------- |
| 20 | +gem install elasticsearch |
| 21 | +-------------------------- |
| 22 | + |
| 23 | +Refer to the <<ruby-install>> page to learn more. |
| 24 | + |
| 25 | + |
| 26 | +[discrete] |
| 27 | +=== Connecting |
| 28 | + |
| 29 | +You can connect to the Elastic Cloud using an API key and the Elasticsearch |
| 30 | +endpoint. |
| 31 | + |
| 32 | +[source,rb] |
| 33 | +---- |
| 34 | +client = Elasticsearch::Client.new( |
| 35 | + cloud_id: '<CloudID>', |
| 36 | + api_key: '<ApiKey>' |
| 37 | +) |
| 38 | +---- |
| 39 | + |
| 40 | +Your Elasticsearch endpoint can be found on the **My deployment** page of your |
| 41 | +deployment: |
| 42 | + |
| 43 | +image::images/es_endpoint.jpg[alt="Finding Elasticsearch endpoint",align="center"] |
| 44 | + |
| 45 | +You can generate an API key on the **Management** page under Security. |
| 46 | + |
| 47 | +image::images/create_api_key.png[alt="Create API key",align="center"] |
| 48 | + |
| 49 | +For other connection options, refer to the <<connecting>> section. |
| 50 | + |
| 51 | + |
| 52 | +[discrete] |
| 53 | +=== Operations |
| 54 | + |
| 55 | +Time to use Elasticsearch! This section walks you through the basic, and most |
| 56 | +important, operations of Elasticsearch. For more operations and more advanced |
| 57 | +examples, refer to the <<examples>> page. |
| 58 | + |
| 59 | + |
| 60 | +[discrete] |
| 61 | +==== Creating an index |
| 62 | + |
| 63 | +This is how you create the `my_index` index: |
| 64 | + |
| 65 | +[source,rb] |
| 66 | +---- |
| 67 | +client.indices.create(index: 'my_index') |
| 68 | +---- |
| 69 | + |
| 70 | + |
| 71 | +[discrete] |
| 72 | +==== Indexing documents |
| 73 | + |
| 74 | +This is a simple way of indexing a document: |
| 75 | + |
| 76 | +[source,rb] |
| 77 | +---- |
| 78 | +document = { name: 'elasticsearch-ruby' } |
| 79 | +response = client.index(index: 'my_index', body: document) |
| 80 | +# You can get the indexed document id with: |
| 81 | +response['_id'] |
| 82 | +=> "PlgIDYkBWS9Ngdx5IMy-" |
| 83 | +id = response['_id'] |
| 84 | +---- |
| 85 | + |
| 86 | + |
| 87 | +[discrete] |
| 88 | +==== Getting documents |
| 89 | + |
| 90 | +You can get documents by using the following code: |
| 91 | + |
| 92 | +[source,rb] |
| 93 | +---- |
| 94 | +client.get(index: 'my_index', id: id) |
| 95 | +---- |
| 96 | + |
| 97 | + |
| 98 | +[discrete] |
| 99 | +==== Searching documents |
| 100 | + |
| 101 | +This is how you can create a single match query with the Ruby client: |
| 102 | + |
| 103 | +[source,rb] |
| 104 | +---- |
| 105 | +client.search(index: 'my_index', body: { query: { match_all: {} } }) |
| 106 | +---- |
| 107 | + |
| 108 | + |
| 109 | +[discrete] |
| 110 | +==== Updating documents |
| 111 | + |
| 112 | +This is how you can update a document, for example to add a new field: |
| 113 | + |
| 114 | +[source,rb] |
| 115 | +---- |
| 116 | +client.update(index: 'my_index', id: id, body: { doc: { language: 'Ruby' } }) |
| 117 | +---- |
| 118 | + |
| 119 | + |
| 120 | +[discrete] |
| 121 | +==== Deleting documents |
| 122 | + |
| 123 | +[source,rb] |
| 124 | +---- |
| 125 | +client.delete(index: 'my_index', id: id) |
| 126 | +---- |
| 127 | + |
| 128 | + |
| 129 | +[discrete] |
| 130 | +==== Deleting an index |
| 131 | + |
| 132 | +[source,rb] |
| 133 | +---- |
| 134 | +client.indices.delete(index: 'my_index') |
| 135 | +---- |
| 136 | + |
| 137 | + |
| 138 | +[discrete] |
| 139 | +== Further reading |
| 140 | + |
| 141 | +* Use <<Helpers>> for a more confortable experience with the APIs. |
0 commit comments