|
1 | 1 | # Index templates |
2 | 2 |
|
3 | | -## Adding an index template named `access-logs` |
| 3 | +## Adding/updating an index template |
| 4 | + |
| 5 | +This adds a new index template or updates an existing one. |
| 6 | + |
4 | 7 | ``` |
5 | | -PUT /_template/access-logs |
| 8 | +PUT /_index_template/access-logs |
6 | 9 | { |
7 | 10 | "index_patterns": ["access-logs-*"], |
| 11 | + "template": { |
| 12 | + "settings": { |
| 13 | + "number_of_shards": 2, |
| 14 | + "index.mapping.coerce": false |
| 15 | + }, |
| 16 | + "mappings": { |
| 17 | + "properties": { |
| 18 | + "@timestamp": { "type": "date" }, |
| 19 | + "url.original": { "type": "wildcard" }, |
| 20 | + "url.path": { "type": "wildcard" }, |
| 21 | + "url.scheme": { "type": "keyword" }, |
| 22 | + "url.domain": { "type": "keyword" }, |
| 23 | + "client.geo.continent_name": { "type": "keyword" }, |
| 24 | + "client.geo.country_name": { "type": "keyword" }, |
| 25 | + "client.geo.region_name": { "type": "keyword" }, |
| 26 | + "client.geo.city_name": { "type": "keyword" }, |
| 27 | + "user_agent.original": { "type": "keyword" }, |
| 28 | + "user_agent.name": { "type": "keyword" }, |
| 29 | + "user_agent.version": { "type": "keyword" }, |
| 30 | + "user_agent.device.name": { "type": "keyword" }, |
| 31 | + "user_agent.os.name": { "type": "keyword" }, |
| 32 | + "user_agent.os.version": { "type": "keyword" } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +## Indexing a document into a new index |
| 40 | + |
| 41 | +The index name matches the index pattern defined within the above index template. |
| 42 | +The index template's settings and mappings will be therefore be applied to the new index. |
| 43 | + |
| 44 | +``` |
| 45 | +POST /access-logs-2023-01/_doc |
| 46 | +{ |
| 47 | + "@timestamp": "2023-01-01T00:00:00Z", |
| 48 | + "url.original": "https://example.com/products", |
| 49 | + "url.path": "/products", |
| 50 | + "url.scheme": "https", |
| 51 | + "url.domain": "example.com", |
| 52 | + "client.geo.continent_name": "Europe", |
| 53 | + "client.geo.country_name": "Denmark", |
| 54 | + "client.geo.region_name": "Capital City Region", |
| 55 | + "client.geo.city_name": "Copenhagen", |
| 56 | + "user_agent.original": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1", |
| 57 | + "user_agent.name": "Safari", |
| 58 | + "user_agent.version": "12.0", |
| 59 | + "user_agent.device.name": "iPhone", |
| 60 | + "user_agent.os.name": "iOS", |
| 61 | + "user_agent.os.version": "12.1.0" |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Manually creating an index |
| 66 | + |
| 67 | +The index template's settings/mappings will also be applied to this index. |
| 68 | + |
| 69 | +``` |
| 70 | +PUT /access-logs-2023-02 |
| 71 | +{ |
8 | 72 | "settings": { |
9 | | - "number_of_shards": 2, |
10 | | - "index.mapping.coerce": false |
11 | | - }, |
| 73 | + "number_of_shards": 1 |
| 74 | + }, |
12 | 75 | "mappings": { |
13 | 76 | "properties": { |
14 | | - "@timestamp": { |
15 | | - "type": "date" |
16 | | - }, |
17 | | - "url.original": { |
18 | | - "type": "keyword" |
19 | | - }, |
20 | | - "http.request.referrer": { |
| 77 | + "url.query": { |
21 | 78 | "type": "keyword" |
22 | | - }, |
23 | | - "http.response.status_code": { |
24 | | - "type": "long" |
25 | 79 | } |
26 | 80 | } |
27 | 81 | } |
28 | 82 | } |
29 | 83 | ``` |
30 | 84 |
|
31 | | -## Adding an index matching the index template's pattern |
| 85 | +## Inspecting the new indices |
| 86 | + |
| 87 | +``` |
| 88 | +GET /access-logs-2023-01 |
| 89 | +GET /access-logs-2023-02 |
| 90 | +``` |
| 91 | + |
| 92 | +## Retrieving an index template |
| 93 | + |
32 | 94 | ``` |
33 | | -PUT /access-logs-2020-01-01 |
| 95 | +GET /_index_template/access-logs |
34 | 96 | ``` |
35 | 97 |
|
36 | | -## Verify that the mapping is applied |
| 98 | +## Deleting an index template |
| 99 | + |
37 | 100 | ``` |
38 | | -GET /access-logs-2020-01-01 |
| 101 | +DELETE /_index_template/access-logs |
39 | 102 | ``` |
0 commit comments