elasticsearch.md 47,7 КБ
Newer Older
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
1
# Elasticsearch integration **(STARTER ONLY)**
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
2

GitLab Bot's avatar
GitLab Bot включено в состав коммита
3
4
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/109 "Elasticsearch Merge Request") in GitLab [Starter](https://about.gitlab.com/pricing/) 8.4.
> - Support for [Amazon Elasticsearch](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-gsg.html) was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/1305) in GitLab [Starter](https://about.gitlab.com/pricing/) 9.0.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
5
6
7
8
9

This document describes how to set up Elasticsearch with GitLab. Once enabled,
you'll have the benefit of fast search response times and the advantage of two
special searches:

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
10
11
- [Advanced Global Search](../user/search/advanced_global_search.md)
- [Advanced Syntax Search](../user/search/advanced_search_syntax.md)
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
12
13

## Version Requirements
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
14

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
15
16
17
18
<!-- Please remember to update ee/lib/system_check/app/elasticsearch_check.rb if this changes -->

| GitLab version | Elasticsearch version |
| -------------- | --------------------- |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
19
| GitLab Enterprise Edition 8.4 - 8.17   | Elasticsearch 2.4 with [Delete By Query Plugin](https://www.elastic.co/guide/en/elasticsearch/plugins/2.4/plugins-delete-by-query.html) installed |
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
20
| GitLab Enterprise Edition 9.0 - 11.4   | Elasticsearch 5.1 - 5.5 |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
21
22
| GitLab Enterprise Edition 11.5 - 12.6  | Elasticsearch 5.6 - 6.x |
| GitLab Enterprise Edition 12.7+        | Elasticsearch 6.x - 7.x |
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
23
24
25

## Installing Elasticsearch

GitLab Bot's avatar
GitLab Bot включено в состав коммита
26
27
Elasticsearch is _not_ included in the Omnibus packages or when you install from source. You must
[install it separately](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/install-elasticsearch.html "Elasticsearch 7.x installation documentation"). Be sure to select your version.
Ronald van Zon's avatar
Ronald van Zon включено в состав коммита
28
29
30
31
32
Providing detailed information on installing Elasticsearch is out of the scope
of this document.

NOTE: **Note:**
Elasticsearch should be installed on a separate server, whether you install
GitLab Bot's avatar
GitLab Bot включено в состав коммита
33
34
it yourself or use a cloud hosted offering like Elastic's [Elasticsearch Service](https://www.elastic.co/elasticsearch/service) (available on AWS, GCP, or Azure) or the
[Amazon Elasticsearch](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-gsg.html) service. Running Elasticsearch on the same server as GitLab is not recommended
GitLab Bot's avatar
GitLab Bot включено в состав коммита
35
and will likely cause a degradation in GitLab instance performance.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
36

GitLab Bot's avatar
GitLab Bot включено в состав коммита
37
38
39
NOTE: **Note:**
**For a single node Elasticsearch cluster the functional cluster health status will be yellow** (will never be green) because the primary shard is allocated but replicas can not be as there is no other node to which Elasticsearch can assign a replica.

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
40
Once the data is added to the database or repository and [Elasticsearch is
GitLab Bot's avatar
GitLab Bot включено в состав коммита
41
enabled in the Admin Area](#enabling-elasticsearch) the search index will be
Ronald van Zon's avatar
Ronald van Zon включено в состав коммита
42
updated automatically.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
43

GitLab Bot's avatar
GitLab Bot включено в состав коммита
44
## Elasticsearch repository indexer
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
45

GitLab Bot's avatar
GitLab Bot включено в состав коммита
46
For indexing Git repository data, GitLab uses an [indexer written in Go](https://gitlab.com/gitlab-org/gitlab-elasticsearch-indexer).
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
47

GitLab Bot's avatar
GitLab Bot включено в состав коммита
48
49
The way you install the Go indexer depends on your version of GitLab:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
50
51
- For Omnibus GitLab 11.8 and above, see [Omnibus GitLab](#omnibus-gitlab).
- For installations from source or older versions of Omnibus GitLab, install the indexer [From Source](#from-source).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
52

GitLab Bot's avatar
GitLab Bot включено в состав коммита
53
### Omnibus GitLab
GitLab Bot's avatar
GitLab Bot включено в состав коммита
54

GitLab Bot's avatar
GitLab Bot включено в состав коммита
55
Since GitLab 11.8 the Go indexer is included in Omnibus GitLab.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
56
The former Ruby-based indexer was removed in [GitLab 12.3](https://gitlab.com/gitlab-org/gitlab/-/issues/6481).
Greg Myers's avatar
Greg Myers включено в состав коммита
57

GitLab Bot's avatar
GitLab Bot включено в состав коммита
58
### From source
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
59
60
61
62
63
64
65
66

First, we need to install some dependencies, then we'll build and install
the indexer itself.

This project relies on [ICU](http://site.icu-project.org/) for text encoding,
therefore we need to ensure the development packages for your platform are
installed before running `make`.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
67
#### Debian / Ubuntu
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
68

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
69
To install on Debian or Ubuntu, run:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
70

GitLab Bot's avatar
GitLab Bot включено в состав коммита
71
```shell
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
72
73
74
sudo apt install libicu-dev
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
75
#### CentOS / RHEL
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
76
77
78

To install on CentOS or RHEL, run:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
79
```shell
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
80
81
82
sudo yum install libicu-devel
```

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
83
84
85
86
##### Mac OSX

To install on macOS, run:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
87
```shell
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
88
89
90
91
brew install icu4c
export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig:$PKG_CONFIG_PATH"
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
92
### Building and installing
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
93
94
95

To build and install the indexer, run:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
96
```shell
Nick Thomas's avatar
Nick Thomas включено в состав коммита
97
98
99
100
101
indexer_path=/home/git/gitlab-elasticsearch-indexer

# Run the installation task for gitlab-elasticsearch-indexer:
sudo -u git -H bundle exec rake gitlab:indexer:install[$indexer_path] RAILS_ENV=production
cd $indexer_path && sudo make install
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
102
103
104
105
106
107
108
109
110
```

The `gitlab-elasticsearch-indexer` will be installed to `/usr/local/bin`.

You can change the installation path with the `PREFIX` env variable.
Please remember to pass the `-E` flag to `sudo` if you do so.

Example:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
111
```shell
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
112
113
114
PREFIX=/usr sudo -E make install
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
115
Once installed, enable it under your instance's Elasticsearch settings explained [below](#enabling-elasticsearch).
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
116
117
118
119

## System Requirements

Elasticsearch requires additional resources in excess of those documented in the
GitLab Bot's avatar
GitLab Bot включено в состав коммита
120
[GitLab system requirements](../install/requirements.md).
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
121

GitLab Bot's avatar
GitLab Bot включено в состав коммита
122
The amount of resources (memory, CPU, storage) will vary greatly, based on the amount of data being indexed into the Elasticsearch cluster. According to [Elasticsearch official guidelines](https://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html#_memory), each node should have:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
123

GitLab Bot's avatar
GitLab Bot включено в состав коммита
124
125
126
127
128
129
130
131
132
133
134
- [RAM](https://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html#_disks): **8 GiB as the bare minimum**
- [CPU](https://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html#_cpus): Modern processor with multiple cores
- [Storage](https://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html#_disks): Use SSD storage. As a guide you will need enough storage for 50% of the total size of your Git repositories.

A few notes on CPU and storage:

- CPU requirements for Elasticsearch tend to be light. There are specific scenarios where this isn't true, but GitLab.com isn't using Elasticsearch in an exceptionally CPU-heavy way. More cores will be more performant than faster CPUs. Extra concurrency from multiple cores will far outweigh a slightly faster clock speed in Elasticsearch.

- Storage requirements for Elasticsearch are important, especially for indexing-heavy clusters. When possible, use SSDs, Their speed is far superior to any spinning media for Elasticsearch. In testing, nodes that use SSD storage see boosts in both query and indexing performance.

Keep in mind, these are **minimum requirements** for Elasticsearch. Heavily-utilized Elasticsearch clusters will likely require considerably more resources.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
135
136
137

## Enabling Elasticsearch

Markus Koller's avatar
Markus Koller включено в состав коммита
138
139
140
141
In order to enable Elasticsearch, you need to have admin access. Navigate to
**Admin Area** (wrench icon), then **Settings > Integrations** and expand the **Elasticsearch** section.

Click **Save changes** for the changes to take effect.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
142
143
144

The following Elasticsearch settings are available:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
145
146
| Parameter                                             | Description |
| ----------------------------------------------------- | ----------- |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
147
| `Elasticsearch indexing`                              | Enables/disables Elasticsearch indexing. You may want to enable indexing but disable search in order to give the index time to be fully completed, for example. Also, keep in mind that this option doesn't have any impact on existing data, this only enables/disables background indexer which tracks data changes. So by enabling this you will not get your existing data indexed, use special Rake task for that as explained in [Adding GitLab's data to the Elasticsearch index](#adding-gitlabs-data-to-the-elasticsearch-index). |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
148
| `Elasticsearch pause indexing`                        | Enables/disables temporary indexing pause. This is useful for cluster migration/reindexing. All changes are still tracked, but they are not committed to the Elasticsearch index until unpaused. |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
149
150
| `Search with Elasticsearch enabled`                   | Enables/disables using Elasticsearch in search. |
| `URL`                                                 | The URL to use for connecting to Elasticsearch. Use a comma-separated list to support clustering (e.g., `http://host1, https://host2:9200`). If your Elasticsearch instance is password protected, pass the `username:password` in the URL (e.g., `http://<username>:<password>@<elastic_host>:9200/`). |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
151
| `Number of Elasticsearch shards`                      | Elasticsearch indexes are split into multiple shards for performance reasons. In general, larger indexes need to have more shards. Changes to this value do not take effect until the index is recreated. You can read more about tradeoffs in the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/scalability.html). |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
152
153
| `Number of Elasticsearch replicas`                    | Each Elasticsearch shard can have a number of replicas. These are a complete copy of the shard, and can provide increased query performance or resilience against hardware failure. Increasing this value will greatly increase total disk space required by the index. |
| `Limit namespaces and projects that can be indexed`   | Enabling this will allow you to select namespaces and projects to index. All other namespaces and projects will use database search instead. Please note that if you enable this option but do not select any namespaces or projects, none will be indexed. [Read more below](#limiting-namespaces-and-projects).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
154
| `Using AWS hosted Elasticsearch with IAM credentials` | Sign your Elasticsearch requests using [AWS IAM authorization](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html), [AWS EC2 Instance Profile Credentials](https://docs.aws.amazon.com/codedeploy/latest/userguide/getting-started-create-iam-instance-profile.html#getting-started-create-iam-instance-profile-cli), or [AWS ECS Tasks Credentials](https://docs.aws.amazon.com/AmazonECS/latest/userguide/task-iam-roles.html). The policies must be configured to allow `es:*` actions. |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
155
156
157
| `AWS Region`                                          | The AWS region your Elasticsearch service is located in. |
| `AWS Access Key`                                      | The AWS access key. |
| `AWS Secret Access Key`                               | The AWS secret access key. |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
158
| `Maximum file size indexed`                           | See [the explanation in instance limits.](../administration/instance_limits.md#maximum-file-size-indexed). |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
159
| `Maximum field length`                                | See [the explanation in instance limits.](../administration/instance_limits.md#maximum-field-length). |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
160
161
| `Maximum bulk request size (MiB)` | The Maximum Bulk Request size is used by GitLab's Golang-based indexer processes and indicates how much data it ought to collect (and store in memory) in a given indexing process before submitting the payload to Elasticsearch’s Bulk API. This setting should be used with the Bulk request concurrency setting (see below) and needs to accommodate the resource constraints of both the Elasticsearch host(s) and the host(s) running GitLab's Golang-based indexer either from the `gitlab-rake` command or the Sidekiq tasks. |
| `Bulk request concurrency` | The Bulk request concurrency indicates how many of GitLab's Golang-based indexer processes (or threads) can run in parallel to collect data to subsequently submit to Elasticsearch’s Bulk API. This increases indexing performance, but fills the Elasticsearch bulk requests queue faster. This setting should be used together with the Maximum bulk request size setting (see above) and needs to accommodate the resource constraints of both the Elasticsearch host(s) and the host(s) running GitLab's Golang-based indexer either from the `gitlab-rake` command or the Sidekiq tasks. |
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
162
163
164
165
166
167
168
169
170

### Limiting namespaces and projects

If you select `Limit namespaces and projects that can be indexed`, more options will become available
![limit namespaces and projects options](img/limit_namespaces_projects_options.png)

You can select namespaces and projects to index exclusively. Please note that if the namespace is a group it will include
any sub-groups and projects belonging to those sub-groups to be indexed as well.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
171
Elasticsearch only provides cross-group code/commit search (global) if all name-spaces are indexed. In this particular scenario where only a subset of namespaces are indexed, a global search will not provide a code or commit scope. This will be possible only in the scope of an indexed namespace. Currently there is no way to code/commit search in multiple indexed namespaces (when only a subset of namespaces has been indexed). For example if two groups are indexed, there is no way to run a single code search on both. You can only run a code search on the first group and then on the second.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
172

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
173
You can filter the selection dropdown by writing part of the namespace or project name you're interested in.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
174

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
175
176
![limit namespace filter](img/limit_namespace_filter.png)

GitLab Bot's avatar
GitLab Bot включено в состав коммита
177
NOTE: **Note:**
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
178
179
If no namespaces or projects are selected, no Elasticsearch indexing will take place.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
180
CAUTION: **Warning:**
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
181
If you have already indexed your instance, you will have to regenerate the index in order to delete all existing data
GitLab Bot's avatar
GitLab Bot включено в состав коммита
182
for filtering to work correctly. To do this run the Rake tasks `gitlab:elastic:recreate_index` and
Evan Read's avatar
Evan Read включено в состав коммита
183
`gitlab:elastic:clear_index_status`. Afterwards, removing a namespace or a project from the list will delete the data
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
184
185
186
187
188
189
from the Elasticsearch index as expected.

## Disabling Elasticsearch

To disable the Elasticsearch integration:

Markus Koller's avatar
Markus Koller включено в состав коммита
190
191
192
193
1. Navigate to the **Admin Area** (wrench icon), then **Settings > Integrations**.
1. Expand the **Elasticsearch** section and uncheck **Elasticsearch indexing**
   and **Search with Elasticsearch enabled**.
1. Click **Save changes** for the changes to take effect.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
194
1. (Optional) Delete the existing index:
Markus Koller's avatar
Markus Koller включено в состав коммита
195

GitLab Bot's avatar
GitLab Bot включено в состав коммита
196
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
197
198
199
200
201
202
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:delete_index

   # Installations from source
   bundle exec rake gitlab:elastic:delete_index RAILS_ENV=production
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
203
204
205

## Adding GitLab's data to the Elasticsearch index

Markus Koller's avatar
Markus Koller включено в состав коммита
206
207
While Elasticsearch indexing is enabled, new changes in your GitLab instance will be automatically indexed as they happen.
To backfill existing data, you can use one of the methods below to index it in background jobs.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
208

Markus Koller's avatar
Markus Koller включено в состав коммита
209
### Indexing through the administration UI
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
210

GitLab Bot's avatar
GitLab Bot включено в состав коммита
211
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/15390) in [GitLab Starter](https://about.gitlab.com/pricing/) 12.3.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
212

GitLab Bot's avatar
GitLab Bot включено в состав коммита
213
To index via the Admin Area:
Markus Koller's avatar
Markus Koller включено в состав коммита
214

GitLab Bot's avatar
GitLab Bot включено в состав коммита
215
1. [Configure your Elasticsearch host and port](#enabling-elasticsearch).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
216
1. Create empty indexes:
Markus Koller's avatar
Markus Koller включено в состав коммита
217

GitLab Bot's avatar
GitLab Bot включено в состав коммита
218
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
219
220
221
222
223
224
225
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:create_empty_index

   # Installations from source
   bundle exec rake gitlab:elastic:create_empty_index RAILS_ENV=production
   ```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
226
227
1. [Enable **Elasticsearch indexing**](#enabling-elasticsearch).
1. Click **Index all projects** in **Admin Area > Settings > Integrations > Elasticsearch**.
Markus Koller's avatar
Markus Koller включено в состав коммита
228
1. Click **Check progress** in the confirmation message to see the status of the background jobs.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
229
1. Personal snippets need to be indexed manually:
Markus Koller's avatar
Markus Koller включено в состав коммита
230

GitLab Bot's avatar
GitLab Bot включено в состав коммита
231
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
232
233
234
235
236
237
238
239
240
241
242
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:index_snippets

   # Installations from source
   bundle exec rake gitlab:elastic:index_snippets RAILS_ENV=production
   ```

1. After the indexing has completed, enable [**Search with Elasticsearch**](#enabling-elasticsearch).

### Indexing through Rake tasks

GitLab Bot's avatar
GitLab Bot включено в состав коммита
243
244
Indexing can be performed using Rake tasks.

Markus Koller's avatar
Markus Koller включено в состав коммита
245
246
#### Indexing small instances

GitLab Bot's avatar
GitLab Bot включено в состав коммита
247
CAUTION: **Warning:**
Markus Koller's avatar
Markus Koller включено в состав коммита
248
249
250
251
This will delete your existing indexes.

If the database size is less than 500 MiB, and the size of all hosted repos is less than 5 GiB:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
252
1. [Configure your Elasticsearch host and port](#enabling-elasticsearch).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
253
1. Index your data:
Markus Koller's avatar
Markus Koller включено в состав коммита
254

GitLab Bot's avatar
GitLab Bot включено в состав коммита
255
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
256
257
258
259
260
261
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:index

   # Installations from source
   bundle exec rake gitlab:elastic:index RAILS_ENV=production
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
262

Markus Koller's avatar
Markus Koller включено в состав коммита
263
1. After the indexing has completed, enable [**Search with Elasticsearch**](#enabling-elasticsearch).
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
264

Markus Koller's avatar
Markus Koller включено в состав коммита
265
#### Indexing large instances
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
266

GitLab Bot's avatar
GitLab Bot включено в состав коммита
267
CAUTION: **Warning:**
Markus Koller's avatar
Markus Koller включено в состав коммита
268
Performing asynchronous indexing will generate a lot of Sidekiq jobs.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
269
Make sure to prepare for this task by having a [Scalable and Highly Available Setup](README.md)
Markus Koller's avatar
Markus Koller включено в состав коммита
270
or creating [extra Sidekiq processes](../administration/operations/extra_sidekiq_processes.md)
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
271

GitLab Bot's avatar
GitLab Bot включено в состав коммита
272
1. [Configure your Elasticsearch host and port](#enabling-elasticsearch).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
273
1. Create empty indexes:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
274

GitLab Bot's avatar
GitLab Bot включено в состав коммита
275
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
276
277
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:create_empty_index
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
278

Markus Koller's avatar
Markus Koller включено в состав коммита
279
280
281
   # Installations from source
   bundle exec rake gitlab:elastic:create_empty_index RAILS_ENV=production
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
282

GitLab Bot's avatar
GitLab Bot включено в состав коммита
283
284
285
286
287
288
289
290
291
292
1. If this is a re-index of your GitLab instance, clear the index status:

   ```shell
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:clear_index_status

   # Installations from source
   bundle exec rake gitlab:elastic:clear_index_status RAILS_ENV=production
   ```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
293
1. [Enable **Elasticsearch indexing**](#enabling-elasticsearch).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
294
295
296
297
298
299
300
301
302
303
1. Indexing large Git repositories can take a while. To speed up the process, you can [tune for indexing speed](https://www.elastic.co/guide/en/elasticsearch/reference/current/tune-for-indexing-speed.html#tune-for-indexing-speed):

   - You can temporarily disable [`refresh`](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html), the operation responsible for making changes to an index available to search.

   - You can set the number of replicas to 0. This setting controls the number of copies each primary shard of an index will have. Thus, having 0 replicas effectively disables the replication of shards across nodes, which should increase the indexing performance. This is an important trade-off in terms of reliability and query performance. It is important to remember to set the replicas to a considered value after the initial indexing is complete.

   In our experience, you can expect a 20% decrease in indexing time. After completing indexing in a later step, you can return `refresh` and `number_of_replicas` to their desired settings.

   NOTE: **Note:**
   This step is optional but may help significantly speed up large indexing operations.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
304

GitLab Bot's avatar
GitLab Bot включено в состав коммита
305
   ```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
306
   curl --request PUT localhost:9200/gitlab-production/_settings --header 'Content-Type: application/json' --data '{
Markus Koller's avatar
Markus Koller включено в состав коммита
307
308
309
310
311
       "index" : {
           "refresh_interval" : "-1",
           "number_of_replicas" : 0
       } }'
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
312

Markus Koller's avatar
Markus Koller включено в состав коммита
313
1. Index projects and their associated data:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
314

GitLab Bot's avatar
GitLab Bot включено в состав коммита
315
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
316
317
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:index_projects
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
318

Markus Koller's avatar
Markus Koller включено в состав коммита
319
320
321
   # Installations from source
   bundle exec rake gitlab:elastic:index_projects RAILS_ENV=production
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
322

Markus Koller's avatar
Markus Koller включено в состав коммита
323
324
   This enqueues a Sidekiq job for each project that needs to be indexed.
   You can view the jobs in **Admin Area > Monitoring > Background Jobs > Queues Tab**
GitLab Bot's avatar
GitLab Bot включено в состав коммита
325
   and click `elastic_indexer`, or you can query indexing status using a Rake task:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
326

GitLab Bot's avatar
GitLab Bot включено в состав коммита
327
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
328
329
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:index_projects_status
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
330

Markus Koller's avatar
Markus Koller включено в состав коммита
331
332
   # Installations from source
   bundle exec rake gitlab:elastic:index_projects_status RAILS_ENV=production
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
333

Markus Koller's avatar
Markus Koller включено в состав коммита
334
335
   Indexing is 65.55% complete (6555/10000 projects)
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
336

Markus Koller's avatar
Markus Koller включено в состав коммита
337
338
   If you want to limit the index to a range of projects you can provide the
   `ID_FROM` and `ID_TO` parameters:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
339

GitLab Bot's avatar
GitLab Bot включено в состав коммита
340
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
341
342
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:index_projects ID_FROM=1001 ID_TO=2000
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
343

Markus Koller's avatar
Markus Koller включено в состав коммита
344
345
346
   # Installations from source
   bundle exec rake gitlab:elastic:index_projects ID_FROM=1001 ID_TO=2000 RAILS_ENV=production
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
347

Markus Koller's avatar
Markus Koller включено в состав коммита
348
349
   Where `ID_FROM` and `ID_TO` are project IDs. Both parameters are optional.
   The above example will index all projects from ID `1001` up to (and including) ID `2000`.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
350

Markus Koller's avatar
Markus Koller включено в состав коммита
351
352
353
354
355
   TIP: **Troubleshooting:**
   Sometimes the project indexing jobs queued by `gitlab:elastic:index_projects`
   can get interrupted. This may happen for many reasons, but it's always safe
   to run the indexing task again. It will skip repositories that have
   already been indexed.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
356

Markus Koller's avatar
Markus Koller включено в состав коммита
357
358
359
360
   As the indexer stores the last commit SHA of every indexed repository in the
   database, you can run the indexer with the special parameter `UPDATE_INDEX` and
   it will check every project repository again to make sure that every commit in
   a repository is indexed, which can be useful in case if your index is outdated:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
361

GitLab Bot's avatar
GitLab Bot включено в состав коммита
362
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
363
364
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:index_projects UPDATE_INDEX=true ID_TO=1000
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
365

Markus Koller's avatar
Markus Koller включено в состав коммита
366
367
368
   # Installations from source
   bundle exec rake gitlab:elastic:index_projects UPDATE_INDEX=true ID_TO=1000 RAILS_ENV=production
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
369

Markus Koller's avatar
Markus Koller включено в состав коммита
370
371
372
   You can also use the `gitlab:elastic:clear_index_status` Rake task to force the
   indexer to "forget" all progress, so it will retry the indexing process from the
   start.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
373

GitLab Bot's avatar
GitLab Bot включено в состав коммита
374
1. Personal snippets are not associated with a project and need to be indexed separately:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
375

GitLab Bot's avatar
GitLab Bot включено в состав коммита
376
   ```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
377
378
   # Omnibus installations
   sudo gitlab-rake gitlab:elastic:index_snippets
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
379

Markus Koller's avatar
Markus Koller включено в состав коммита
380
381
382
   # Installations from source
   bundle exec rake gitlab:elastic:index_snippets RAILS_ENV=production
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
383

Markus Koller's avatar
Markus Koller включено в состав коммита
384
1. Enable replication and refreshing again after indexing (only if you previously disabled it):
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
385

GitLab Bot's avatar
GitLab Bot включено в состав коммита
386
   ```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
387
   curl --request PUT localhost:9200/gitlab-production/_settings --header 'Content-Type: application/json' --data '{
Markus Koller's avatar
Markus Koller включено в состав коммита
388
389
390
391
392
       "index" : {
           "number_of_replicas" : 1,
           "refresh_interval" : "1s"
       } }'
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
393

Markus Koller's avatar
Markus Koller включено в состав коммита
394
   A force merge should be called after enabling the refreshing above.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
395

Markus Koller's avatar
Markus Koller включено в состав коммита
396
   For Elasticsearch 6.x, the index should be in read-only mode before proceeding with the force merge:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
397

GitLab Bot's avatar
GitLab Bot включено в состав коммита
398
   ```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
399
   curl --request PUT localhost:9200/gitlab-production/_settings --header 'Content-Type: application/json' --data '{
Markus Koller's avatar
Markus Koller включено в состав коммита
400
401
402
403
     "settings": {
       "index.blocks.write": true
     } }'
   ```
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
404

Markus Koller's avatar
Markus Koller включено в состав коммита
405
   Then, initiate the force merge:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
406

GitLab Bot's avatar
GitLab Bot включено в состав коммита
407
   ```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
408
   curl --request POST 'localhost:9200/gitlab-production/_forcemerge?max_num_segments=5'
Markus Koller's avatar
Markus Koller включено в состав коммита
409
   ```
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
410

Markus Koller's avatar
Markus Koller включено в состав коммита
411
   After this, if your index is in read-only mode, switch back to read-write:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
412

GitLab Bot's avatar
GitLab Bot включено в состав коммита
413
   ```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
414
   curl --request PUT localhost:9200/gitlab-production/_settings --header 'Content-Type: application/json' --data '{
Markus Koller's avatar
Markus Koller включено в состав коммита
415
416
417
418
     "settings": {
       "index.blocks.write": false
     } }'
   ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
419

Markus Koller's avatar
Markus Koller включено в состав коммита
420
1. After the indexing has completed, enable [**Search with Elasticsearch**](#enabling-elasticsearch).
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
421

Markus Koller's avatar
Markus Koller включено в состав коммита
422
### Indexing limitations
Mark Chao's avatar
Mark Chao включено в состав коммита
423

Markus Koller's avatar
Markus Koller включено в состав коммита
424
For repository and snippet files, GitLab will only index up to 1 MiB of content, in order to avoid indexing timeouts.
Mark Chao's avatar
Mark Chao включено в состав коммита
425

GitLab Bot's avatar
GitLab Bot включено в состав коммита
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
## Zero downtime reindexing

The idea behind this reindexing method is to leverage Elasticsearch index alias feature to atomically swap between two indices.
We will refer to each index as `primary` (online and used by GitLab for read/writes) and `secondary` (offline, for reindexing purpose).

Instead of connecting directly to the `primary` index, we'll setup an index alias such as we can change the underlying index at will.

NOTE: **Note:**
Any index attached to the production alias is deemed a `primary` and will end up being used by the GitLab Elasticsearch integration.

### Pause the indexing

Under **Admin Area > Integration > Elasticsearch**, check the **Pause Elasticsearch Indexing** setting and save.

With this, all updates that should happen on your Elasticsearch index will be buffered and caught up once unpaused.

### Setup

TIP: **Tip:**
If your index has been created with GitLab v13.0+ you can skip directly to [trigger the reindex](#trigger-the-reindex-via-the-elasticsearch-administration).

This process involves multiple shell commands and curl invocations, so a good initial setup will help down the road:

```shell
# You can find this value under Admin Area > Integration > Elasticsearch > URL
export CLUSTER_URL="http://localhost:9200"
export PRIMARY_INDEX="gitlab-production"
export SECONDARY_INDEX="gitlab-production-$(date +%s)"
```

### Reclaiming the `gitlab-production` index name

CAUTION: **Caution:**
It is highly recommended that you take a snapshot of your cluster to make sure there is a recovery path if anything goes wrong.

NOTE: **Note:**
Due to a technical limitation, there will be a slight downtime because of the fact that we need to reclaim the current `primary` index to be used as the alias.

To reclaim the `gitlab-production` index name, you need to first create a `secondary` index and then trigger the re-index from `primary`.

#### Creating a secondary index

To create a secondary index, run the following Rake task. The `SKIP_ALIAS`
environment variable will disable the automatic creation of the Elasticsearch
alias, which would conflict with the existing index under `$PRIMARY_INDEX`:

```shell
# Omnibus installation
sudo SKIP_ALIAS=1 gitlab-rake "gitlab:elastic:create_empty_index[$SECONDARY_INDEX]"

# Source installation
SKIP_ALIAS=1 bundle exec rake "gitlab:elastic:create_empty_index[$SECONDARY_INDEX]"
```

The index should be created successfully, with the latest index options and mappings.

#### Trigger the re-index from `primary`

To trigger the re-index from `primary` index:

1. Use the Elasticsearch [Reindex API](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/docs-reindex.html):

    ```shell
    curl --request POST \
          --header 'Content-Type: application/json' \
          --data "{ \"source\": { \"index\": \"$PRIMARY_INDEX\" }, \"dest\": { \"index\": \"$SECONDARY_INDEX\" } }" \
          "$CLUSTER_URL/_reindex?slices=auto&wait_for_completion=false"
    ```

    There will be an output like:

    ```plaintext
    {"task":"3qw_Tr0YQLq7PF16Xek8YA:1012"}
    ```

    Note the `task` value here as it will be useful to follow the reindex progress.

1. Wait for the reindex process to complete, by checking the `completed` value.
   Using the `task` value form the previous step:

    ```shell
    export TASK_ID=3qw_Tr0YQLq7PF16Xek8YA:1012
    curl "$CLUSTER_URL/_tasks/$TASK_ID?pretty"
    ```

    The output will be like:

    ```plaintext
    {"completed":false, …}
    ```

    Once the returned value is `true`, you may continue to the next step.

1. Make sure that the secondary index has data in it. You can use the Elasticsearch
   API to look for the index size and compare our two indices:

    ```shell
    curl $CLUSTER_URL/$PRIMARY_INDEX/_count => 123123
    curl $CLUSTER_URL/$SECONDARY_INDEX/_count => 123123
    ```

    TIP: **Tip:**
    Comparing the document count is more accurate than using the index size, as improvements to the storage might cause the new index to be smaller than the original one.

1. Once you are confident your `secondary` index is valid, you can process to the creation of the alias.

    ```shell
    # Delete the original index
    curl --request DELETE $CLUSTER_URL/$PRIMARY_INDEX

    # Create the alias and add the `secondary` index to it
    curl --request POST \
          --header 'Content-Type: application/json' \
          --data "{\"actions\":[{\"add\":{\"index\":\"$SECONDARY_INDEX\",\"alias\":\"$PRIMARY_INDEX\"}}]}}" \
          $CLUSTER_URL/_aliases
    ```

    The reindexing is now completed. Your GitLab instance is now ready to use the [automated in-cluster reindexing](#trigger-the-reindex-via-the-elasticsearch-administration) feature for future reindexing.

1. Unpause the indexing

    Under **Admin Area > Integration > Elasticsearch**, uncheck the **Pause Elasticsearch Indexing** setting and save.

### Trigger the reindex via the Elasticsearch administration

GitLab Bot's avatar
GitLab Bot включено в состав коммита
551
552
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/34069) in [GitLab Starter](https://about.gitlab.com/pricing/) 13.2.
> - A scheduled index deletion and the ability to cancel it was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/38914) in GitLab Starter 13.3.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
553
554
555
556
557
558

Under **Admin Area > Integration > Elasticsearch zero-downtime reindexing**, click on **Trigger cluster reindexing**.

NOTE: **Note:**
Reindexing can be a lengthy process depending on the size of your Elasticsearch cluster.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
559
560
561
CAUTION: **Caution:**
After the reindexing is completed, the original index will be scheduled to be deleted after 14 days. You can cancel this action by pressing the cancel button.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
562
563
While the reindexing is running, you will be able to follow its progress under that same section.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
564
565
566
567
568
569
570
571
572
573
574
575
## GitLab Elasticsearch Rake tasks

Rake tasks are available to:

- [Build and install](#building-and-installing) the indexer.
- Delete indexes when [disabling Elasticsearch](#disabling-elasticsearch).
- [Add GitLab data](#adding-gitlabs-data-to-the-elasticsearch-index) to an index.

The following are some available Rake tasks:

| Task                                                                                                                                                    | Description                                                                                                                                                                               |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
GitLab Bot's avatar
GitLab Bot включено в состав коммита
576
| [`sudo gitlab-rake gitlab:elastic:index`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)                            | Enables Elasticsearch Indexing and run `gitlab:elastic:create_empty_index`, `gitlab:elastic:clear_index_status`, `gitlab:elastic:index_projects`, and `gitlab:elastic:index_snippets`.                          |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
577
578
579
| [`sudo gitlab-rake gitlab:elastic:index_projects`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)                   | Iterates over all projects and queues Sidekiq jobs to index them in the background.                                                                                                       |
| [`sudo gitlab-rake gitlab:elastic:index_projects_status`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)            | Determines the overall status of the indexing. It is done by counting the total number of indexed projects, dividing by a count of the total number of projects, then multiplying by 100. |
| [`sudo gitlab-rake gitlab:elastic:clear_index_status`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)               | Deletes all instances of IndexStatus for all projects.                                                                                                                                    |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
580
581
582
| [`sudo gitlab-rake gitlab:elastic:create_empty_index[<TARGET_NAME>]`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake) | Generates an empty index and assigns an alias for it on the Elasticsearch side only if it doesn't already exist.                                                                                                      |
| [`sudo gitlab-rake gitlab:elastic:delete_index[<TARGET_NAME>]`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)       | Removes the GitLab index and alias (if exists) on the Elasticsearch instance.                                                                                                                                   |
| [`sudo gitlab-rake gitlab:elastic:recreate_index[<TARGET_NAME>]`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)     | Wrapper task for `gitlab:elastic:delete_index[<TARGET_NAME>]` and `gitlab:elastic:create_empty_index[<TARGET_NAME>]`.                                                                       |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
583
584
| [`sudo gitlab-rake gitlab:elastic:index_snippets`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)                   | Performs an Elasticsearch import that indexes the snippets data.                                                                                                                          |
| [`sudo gitlab-rake gitlab:elastic:projects_not_indexed`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)             | Displays which projects are not indexed.                                                                                                                                                  |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
585
| [`sudo gitlab-rake gitlab:elastic:reindex_cluster`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)             | Schedules a zero-downtime cluster reindexing task. This feature should be used with an index that was created after GitLab 13.0. |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
586
587

NOTE: **Note:**
GitLab Bot's avatar
GitLab Bot включено в состав коммита
588
The `TARGET_NAME` parameter is optional and will use the default index/alias name from the current `RAILS_ENV` if not set.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
589
590

### Environment variables
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
591

GitLab Bot's avatar
GitLab Bot включено в состав коммита
592
In addition to the Rake tasks, there are some environment variables that can be used to modify the process:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
593
594
595
596
597
598
599
600
601
602
603

| Environment Variable | Data Type | What it does                                                                 |
| -------------------- |:---------:| ---------------------------------------------------------------------------- |
| `UPDATE_INDEX`       | Boolean   | Tells the indexer to overwrite any existing index data (true/false).         |
| `ID_TO`              | Integer   | Tells the indexer to only index projects less than or equal to the value.    |
| `ID_FROM`            | Integer   | Tells the indexer to only index projects greater than or equal to the value. |

### Indexing a specific project

Because the `ID_TO` and `ID_FROM` environment variables use the `or equal to` comparison, you can index only one project by using both these variables with the same project ID number:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
604
```shell
Markus Koller's avatar
Markus Koller включено в состав коммита
605
root@git:~# sudo gitlab-rake gitlab:elastic:index_projects ID_TO=5 ID_FROM=5
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
606
607
608
609
Indexing project repositories...I, [2019-03-04T21:27:03.083410 #3384]  INFO -- : Indexing GitLab User / test (ID=33)...
I, [2019-03-04T21:27:05.215266 #3384]  INFO -- : Indexing GitLab User / test (ID=33) is done!
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
610
## Elasticsearch index scopes
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625

When performing a search, the GitLab index will use the following scopes:

| Scope Name       | What it searches       |
| ---------------- | ---------------------- |
| `commits`        | Commit data            |
| `projects`       | Project data (default) |
| `blobs`          | Code                   |
| `issues`         | Issue data             |
| `merge_requests` | Merge Request data     |
| `milestones`     | Milestone data         |
| `notes`          | Note data              |
| `snippets`       | Snippet data           |
| `wiki_blobs`     | Wiki contents          |

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
626
627
## Tuning

GitLab Bot's avatar
GitLab Bot включено в состав коммита
628
629
### Guidance on choosing optimal cluster configuration

GitLab Bot's avatar
GitLab Bot включено в состав коммита
630
For basic guidance on choosing a cluster configuration you may refer to [Elastic Cloud Calculator](https://cloud.elastic.co/pricing). You can find more information below.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
631
632
633
634
635

- Generally, you will want to use at least a 2-node cluster configuration with one replica, which will allow you to have resilience. If your storage usage is growing quickly, you may want to plan horizontal scaling (adding more nodes) beforehand.
- It's not recommended to use HDD storage with the search cluster, because it will take a hit on performance. It's better to use SSD storage (NVMe or SATA SSD drives for example).
- You can use the [GitLab Performance Tool](https://gitlab.com/gitlab-org/quality/performance) to benchmark search performance with different search cluster sizes and configurations.
- `Heap size` should be set to no more than 50% of your physical RAM. Additionally, it shouldn't be set to more than the threshold for zero-based compressed oops. The exact threshold varies, but 26 GB is safe on most systems, but can also be as large as 30 GB on some systems. See [Setting the heap size](https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html#heap-size) for more details.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
636
- Number of CPUs (CPU cores) per node usually corresponds to the `Number of Elasticsearch shards` setting described below.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
637
638
639
640
641
642
643
- A good guideline is to ensure you keep the number of shards per node below 20 per GB heap it has configured. A node with a 30GB heap should therefore have a maximum of 600 shards, but the further below this limit you can keep it the better. This will generally help the cluster stay in good health.
- Small shards result in small segments, which increases overhead. Aim to keep the average shard size between at least a few GB and a few tens of GB. Another consideration is the number of documents, you should aim for this simple formula for the number of shards: `number of expected documents / 5M +1`.
- `refresh_interval` is a per index setting. You may want to adjust that from default `1s` to a bigger value if you don't need data in realtime. This will change how soon you will see fresh results. If that's important for you, you should leave it as close as possible to the default value.
- You might want to raise [`indices.memory.index_buffer_size`](https://www.elastic.co/guide/en/elasticsearch/reference/current/indexing-buffer.html) to 30% or 40% if you have a lot of heavy indexing operations.

### Elasticsearch integration settings guidance

GitLab Bot's avatar
GitLab Bot включено в состав коммита
644
- The `Number of Elasticsearch shards` setting usually corresponds with the number of CPUs available in your cluster. For example, if you have a 3-node cluster with 4 cores each, this means you will benefit from having at least 3*4=12 shards in the cluster. Please note, it's only possible to change the shards number by using [Split index API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-split-index.html) or by reindexing to a different index with a changed number of shards.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
645
646
- The `Number of Elasticsearch replicas` setting should most of the time be equal to `1` (each shard will have 1 replica). Using `0` is not recommended, because losing one node will corrupt the index.

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
647
648
### Deleted documents

GitLab Bot's avatar
GitLab Bot включено в состав коммита
649
Whenever a change or deletion is made to an indexed GitLab object (a merge request description is changed, a file is deleted from the master branch in a repository, a project is deleted, etc), a document in the index is deleted. However, since these are "soft" deletes, the overall number of "deleted documents", and therefore wasted space, increases. Elasticsearch does intelligent merging of segments in order to remove these deleted documents. However, depending on the amount and type of activity in your GitLab installation, it's possible to see as much as 50% wasted space in the index.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
650

Evan Read's avatar
Evan Read включено в состав коммита
651
In general, we recommend simply letting Elasticsearch merge and reclaim space automatically, with the default settings. From [Lucene's Handling of Deleted Documents](https://www.elastic.co/blog/lucenes-handling-of-deleted-documents "Lucene's Handling of Deleted Documents"), _"Overall, besides perhaps decreasing the maximum segment size, it is best to leave Lucene's defaults as-is and not fret too much about when deletes are reclaimed."_
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
652

Evan Read's avatar
Evan Read включено в состав коммита
653
However, some larger installations may wish to tune the merge policy settings:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
654

GitLab Bot's avatar
GitLab Bot включено в состав коммита
655
- Consider reducing the `index.merge.policy.max_merged_segment` size from the default 5 GB to maybe 2 GB or 3 GB. Merging only happens when a segment has at least 50% deletions. Smaller segment sizes will allow merging to happen more frequently.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
656

GitLab Bot's avatar
GitLab Bot включено в состав коммита
657
  ```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
658
  curl --request PUT localhost:9200/gitlab-production/_settings ---header 'Content-Type: application/json' --data '{
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
659
660
661
662
663
664
    "index" : {
      "merge.policy.max_merged_segment": "2gb"
    }
  }'
  ```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
665
- You can also adjust `index.merge.policy.reclaim_deletes_weight`, which controls how aggressively deletions are targeted. But this can lead to costly merge decisions, so we recommend not changing this unless you understand the tradeoffs.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
666

GitLab Bot's avatar
GitLab Bot включено в состав коммита
667
  ```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
668
  curl --request PUT localhost:9200/gitlab-production/_settings ---header 'Content-Type: application/json' --data '{
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
669
670
671
672
673
674
    "index" : {
      "merge.policy.reclaim_deletes_weight": "3.0"
    }
  }'
  ```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
675
- Do not do a [force merge](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html "Force Merge") to remove deleted documents. A warning in the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html "Force Merge") states that this can lead to very large segments that may never get reclaimed, and can also cause significant performance or availability issues.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
676
677
678

## Troubleshooting

GitLab Bot's avatar
GitLab Bot включено в состав коммита
679
680
### Common issues

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
681
682
Here are some common pitfalls and how to overcome them:

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
683
684
- **How can I verify my GitLab instance is using Elasticsearch?**

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
685
  The easiest method is via the rails console (`sudo gitlab-rails console`) by running the following:
Evan Read's avatar
Evan Read включено в состав коммита
686

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
687
688
689
690
691
  ```ruby
  u = User.find_by_username('your-username')
  s = SearchService.new(u, {:search => 'search_term'})
  pp s.search_objects.class.name
  ```
Evan Read's avatar
Evan Read включено в состав коммита
692

GitLab Bot's avatar
GitLab Bot включено в состав коммита
693
694
695
  If you see `"ActiveRecord::Relation"`, you are **not** using Elasticsearch.
  
  If you see `"Kaminari::PaginatableArray"` you are using Elasticsearch.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
696

GitLab Bot's avatar
GitLab Bot включено в состав коммита
697
  NOTE: **Note:**
GitLab Bot's avatar
GitLab Bot включено в состав коммита
698
  The above instructions are used to verify that GitLab is using Elasticsearch only when indexing all namespaces. This is not to be used for scenarios that only index a [subset of namespaces](#limiting-namespaces-and-projects).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
699

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
700
701
- **I updated GitLab and now I can't find anything**

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
702
703
704
  We continuously make updates to our indexing strategies and aim to support
  newer versions of Elasticsearch. When indexing changes are made, it may
  be necessary for you to [reindex](#adding-gitlabs-data-to-the-elasticsearch-index) after updating GitLab.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
705
706
707

- **I indexed all the repositories but I can't find anything**

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
708
  Make sure you indexed all the database data [as stated above](#adding-gitlabs-data-to-the-elasticsearch-index).
Evan Read's avatar
Evan Read включено в состав коммита
709

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
710
  Beyond that, check via the [Elasticsearch Search API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html) to see if the data shows up on the Elasticsearch side.
Evan Read's avatar
Evan Read включено в состав коммита
711

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
712
  If it shows up via the [Elasticsearch Search API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html), check that it shows up via the rails console (`sudo gitlab-rails console`):
Evan Read's avatar
Evan Read включено в состав коммита
713

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
714
715
  ```ruby
  u = User.find_by_username('your-username')
GitLab Bot's avatar
GitLab Bot включено в состав коммита
716
  s = SearchService.new(u, {:search => 'search_term', :scope => 'blobs'})
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
717
718
  pp s.search_objects.to_a
  ```
Evan Read's avatar
Evan Read включено в состав коммита
719

GitLab Bot's avatar
GitLab Bot включено в состав коммита
720
  NOTE: **Note:**
GitLab Bot's avatar
GitLab Bot включено в состав коммита
721
  The above instructions are not to be used for scenarios that only index a [subset of namespaces](#limiting-namespaces-and-projects).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
722

Markus Koller's avatar
Markus Koller включено в состав коммита
723
  See [Elasticsearch Index Scopes](#elasticsearch-index-scopes) for more information on searching for specific types of data.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
724

Takuya Noguchi's avatar
Takuya Noguchi включено в состав коммита
725
- **I indexed all the repositories but then switched Elasticsearch servers and now I can't find anything**
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
726

GitLab Bot's avatar
GitLab Bot включено в состав коммита
727
  You will need to re-run all the Rake tasks to reindex the database, repositories, and wikis.
Evan Read's avatar
Evan Read включено в состав коммита
728

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
729
730
- **The indexing process is taking a very long time**

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
731
  The more data present in your GitLab instance, the longer the indexing process takes.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
732

Blair Lunceford's avatar
Blair Lunceford включено в состав коммита
733
734
735
736
- **There are some projects that weren't indexed, but we don't know which ones**

  You can run `sudo gitlab-rake gitlab:elastic:projects_not_indexed` to display projects that aren't indexed.

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
737
738
- **"Can't specify parent if no parent field has been configured"**

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
739
740
741
  If you enabled Elasticsearch before GitLab 8.12 and have not rebuilt indexes you will get
  exception in lots of different cases:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
742
  ```plaintext
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
743
744
745
746
747
748
749
750
751
752
753
754
755
756
  Elasticsearch::Transport::Transport::Errors::BadRequest([400] {
      "error": {
          "root_cause": [{
              "type": "illegal_argument_exception",
              "reason": "Can't specify parent if no parent field has been configured"
          }],
          "type": "illegal_argument_exception",
          "reason": "Can't specify parent if no parent field has been configured"
      },
      "status": 400
  }):
  ```

  This is because we changed the index mapping in GitLab 8.12 and the old indexes should be removed and built from scratch again,
GitLab Bot's avatar
GitLab Bot включено в состав коммита
757
  see details in the [update guide](../update/upgrading_from_source.md).
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
758
759
760

- Exception `Elasticsearch::Transport::Transport::Errors::BadRequest`

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
761
762
  If you have this exception (just like in the case above but the actual message is different) please check if you have the correct Elasticsearch version and you met the other [requirements](#system-requirements).
  There is also an easy way to check it automatically with `sudo gitlab-rake gitlab:check` command.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
763
764
765

- Exception `Elasticsearch::Transport::Transport::Errors::RequestEntityTooLarge`

GitLab Bot's avatar
GitLab Bot включено в состав коммита
766
  ```plaintext
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
767
768
  [413] {"Message":"Request size exceeded 10485760 bytes"}
  ```
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
769

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
770
771
772
773
  This exception is seen when your Elasticsearch cluster is configured to reject
  requests above a certain size (10MiB in this case). This corresponds to the
  `http.max_content_length` setting in `elasticsearch.yml`. Increase it to a
  larger size and restart your Elasticsearch cluster.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
774

GitLab Bot's avatar
GitLab Bot включено в состав коммита
775
  AWS has [fixed limits](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/aes-limits.html)
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
776
777
  for this setting ("Maximum Size of HTTP Request Payloads"), based on the size of
  the underlying instance.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
778

GitLab Bot's avatar
GitLab Bot включено в состав коммита
779
780
- **My single node Elasticsearch cluster status never goes from `yellow` to `green` even though everything seems to be running properly**

GitLab Bot's avatar
GitLab Bot включено в состав коммита
781
  **For a single node Elasticsearch cluster the functional cluster health status will be yellow** (will never be green) because the primary shard is allocated but replicas can not be as there is no other node to which Elasticsearch can assign a replica. This also applies if you are using the
GitLab Bot's avatar
GitLab Bot включено в состав коммита
782
783
[Amazon Elasticsearch](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/aes-handling-errors.html#aes-handling-errors-yellow-cluster-status) service.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
784
785
  CAUTION: **Warning:**
  Setting the number of replicas to `0` is not something that we recommend (this is not allowed in the GitLab Elasticsearch Integration menu). If you are planning to add more Elasticsearch nodes (for a total of more than 1 Elasticsearch) the number of replicas will need to be set to an integer value larger than `0`. Failure to do so will result in lack of redundancy (losing one node will corrupt the index).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
786
787
788

  If you have a **hard requirement to have a green status for your single node Elasticsearch cluster**, please make sure you understand the risks outlined in the previous paragraph and then simply run the following query to set the number of replicas to `0`(the cluster will no longer try to create any shard replicas):

GitLab Bot's avatar
GitLab Bot включено в состав коммита
789
  ```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
790
791
792
793
794
795
  curl --request PUT localhost:9200/gitlab-production/_settings --header 'Content-Type: application/json' --data '{
  "index" : {
     "number_of_replicas" : 0
    }
  }'
  ```
GitLab Bot's avatar
GitLab Bot включено в состав коммита
796

GitLab Bot's avatar
GitLab Bot включено в состав коммита
797
798
- **I'm getting a `health check timeout: no Elasticsearch node available` error in Sidekiq during the indexing process**

GitLab Bot's avatar
GitLab Bot включено в состав коммита
799
   ```plaintext
GitLab Bot's avatar
GitLab Bot включено в состав коммита
800
801
802
   Gitlab::Elastic::Indexer::Error: time="2020-01-23T09:13:00Z" level=fatal msg="health check timeout: no Elasticsearch node available"
   ```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
803
   You probably have not used either `http://` or `https://` as part of your value in the **"URL"** field of the Elasticsearch Integration Menu. Please make sure you are using either `http://` or `https://` in this field as the [Elasticsearch client for Go](https://github.com/olivere/elastic) that we are using [needs the prefix for the URL to be accepted as valid](https://github.com/olivere/elastic/commit/a80af35aa41856dc2c986204e2b64eab81ccac3a).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
804
   Once you have corrected the formatting of the URL, delete the index (via the [dedicated Rake task](#gitlab-elasticsearch-rake-tasks)) and [reindex the content of your instance](#adding-gitlabs-data-to-the-elasticsearch-index).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
805

GitLab Bot's avatar
GitLab Bot включено в состав коммита
806
### Low-level troubleshooting
GitLab Bot's avatar
GitLab Bot включено в состав коммита
807

GitLab Bot's avatar
GitLab Bot включено в состав коммита
808
There is a [more structured, lower-level troubleshooting document](../administration/troubleshooting/elasticsearch.md) for when you experience other issues, including poor performance.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
809

GitLab Bot's avatar
GitLab Bot включено в состав коммита
810
811
### Known Issues

GitLab Bot's avatar
GitLab Bot включено в состав коммита
812
- **[Elasticsearch `code_analyzer` doesn't account for all code cases](https://gitlab.com/gitlab-org/gitlab/-/issues/10693)**
GitLab Bot's avatar
GitLab Bot включено в состав коммита
813
814
815
816
817

   The `code_analyzer` pattern and filter configuration is being evaluated for improvement. We have noticed [several edge cases](https://gitlab.com/gitlab-org/gitlab/-/issues/10693#note_158382332) that are not returning expected search results due to our pattern and filter configuration.

   An improved strategy for the `code_analyzer` pattern and filters are being discussed in [issue 29443](https://gitlab.com/gitlab-org/gitlab/-/issues/29443).

GitLab Bot's avatar
GitLab Bot включено в состав коммита
818
819
820
821
822
823
### Reverting to basic search

Sometimes there may be issues with your Elasticsearch index data and as such
GitLab will allow you to revert to "basic search" when there are no search
results and assuming that basic search is supported in that scope. This "basic
search" will behave as though you don't have Elasticsearch enabled at all for
GitLab Bot's avatar
GitLab Bot включено в состав коммита
824
your instance and search using other data sources (ie. PostgreSQL data and Git
GitLab Bot's avatar
GitLab Bot включено в состав коммита
825
data).