1[[index_management]]
2== Index Management Operations
3
4Index management operations allow you to manage the indices in your Elasticsearch cluster, such as creating, deleting and
5updating indices and their mappings/settings.
6
7=== Create an index
8
9The index operations are all contained under a distinct namespace, separated from other methods that are on the root
10client object.  As an example, let's create a new index:
11
12[source,php]
13----
14$client = ClientBuilder::create()->build();
15$params = [
16    'index' => 'my_index'
17];
18
19// Create the index
20$response = $client->indices()->create($params);
21----
22{zwsp} +
23
24You can specify any parameters that would normally be included in a new index creation API.  All parameters that
25would normally go in the request body are located in the 'body' parameter:
26
27[source,php]
28----
29$client = ClientBuilder::create()->build();
30$params = [
31    'index' => 'my_index',
32    'body' => [
33        'settings' => [
34            'number_of_shards' => 3,
35            'number_of_replicas' => 2
36        ],
37        'mappings' => [
38            '_source' => [
39                'enabled' => true
40            ],
41            'properties' => [
42                'first_name' => [
43                    'type' => 'keyword'
44                ],
45                'age' => [
46                    'type' => 'integer'
47                ]
48            ]
49        ]
50    ]
51];
52
53
54// Create the index with mappings and settings now
55$response = $client->indices()->create($params);
56----
57{zwsp} +
58
59=== Create an index (advanced example)
60
61This is a more complicated example of creating an index, showing how to define analyzers, tokenizers, filters and
62index settings. Although essentially the same as the previous example, the more complicated example can be helpful
63for "real world" usage of the client, since this particular syntax is easy to mess up.
64
65[source,php]
66----
67$params = [
68    'index' => 'reuters',
69    'body' => [
70        'settings' => [ <1>
71            'number_of_shards' => 1,
72            'number_of_replicas' => 0,
73            'analysis' => [ <2>
74                'filter' => [
75                    'shingle' => [
76                        'type' => 'shingle'
77                    ]
78                ],
79                'char_filter' => [
80                    'pre_negs' => [
81                        'type' => 'pattern_replace',
82                        'pattern' => '(\\w+)\\s+((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\b',
83                        'replacement' => '~$1 $2'
84                    ],
85                    'post_negs' => [
86                        'type' => 'pattern_replace',
87                        'pattern' => '\\b((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\s+(\\w+)',
88                        'replacement' => '$1 ~$2'
89                    ]
90                ],
91                'analyzer' => [
92                    'reuters' => [
93                        'type' => 'custom',
94                        'tokenizer' => 'standard',
95                        'filter' => ['lowercase', 'stop', 'kstem']
96                    ]
97                ]
98            ]
99        ],
100        'mappings' => [ <3>
101            'properties' => [
102                'title' => [
103                    'type' => 'text',
104                    'analyzer' => 'reuters',
105                    'copy_to' => 'combined'
106                ],
107                'body' => [
108                    'type' => 'text',
109                    'analyzer' => 'reuters',
110                    'copy_to' => 'combined'
111                ],
112                'combined' => [
113                    'type' => 'text',
114                    'analyzer' => 'reuters'
115                ],
116                'topics' => [
117                    'type' => 'keyword'
118                ],
119                'places' => [
120                    'type' => 'keyword'
121                ]
122            ]
123        ]
124    ]
125];
126$client->indices()->create($params);
127----
128<1> The top level `settings` contains config about the index (# of shards, etc) as well as analyzers
129<2> `analysis` is nested inside of `settings`, and contains tokenizers, filters, char filters and analyzers
130<3> `mappings` is another element nested inside of `settings`, and contains the mappings for various types
131
132
133=== Delete an index
134
135Deleting an index is very simple:
136
137[source,php]
138----
139$params = ['index' => 'my_index'];
140$response = $client->indices()->delete($params);
141----
142{zwsp} +
143
144=== Put Settings API
145The Put Settings API allows you to modify any index setting that is dynamic:
146
147[source,php]
148----
149$params = [
150    'index' => 'my_index',
151    'body' => [
152        'settings' => [
153            'number_of_replicas' => 0,
154            'refresh_interval' => -1
155        ]
156    ]
157];
158
159$response = $client->indices()->putSettings($params);
160----
161{zwsp} +
162
163=== Get Settings API
164
165Get Settings API will show you the currently configured settings for one or more indexes:
166
167[source,php]
168----
169// Get settings for one index
170$params = ['index' => 'my_index'];
171$response = $client->indices()->getSettings($params);
172
173// Get settings for several indices
174$params = [
175    'index' => [ 'my_index', 'my_index2' ]
176];
177$response = $client->indices()->getSettings($params);
178----
179{zwsp} +
180
181=== Put Mappings API
182
183The Put Mappings API allows you to modify or add to an existing index's mapping.
184
185[source,php]
186----
187// Set the index and type
188$params = [
189    'index' => 'my_index',
190    'body' => [
191        '_source' => [
192            'enabled' => true
193        ],
194        'properties' => [
195            'first_name' => [
196                'type' => 'text',
197                'analyzer' => 'standard'
198            ],
199            'age' => [
200                'type' => 'integer'
201            ]
202        ]
203    ]
204];
205
206// Update the index mapping
207$client->indices()->putMapping($params);
208----
209{zwsp} +
210
211=== Get Mappings API
212
213The Get Mappings API will return the mapping details about your indexes.  Depending on the mappings that you wish to retrieve, you can specify one of more indexes:
214
215[source,php]
216----
217// Get mappings for all indexes
218$response = $client->indices()->getMapping();
219
220// Get mappings in 'my_index'
221$params = ['index' => 'my_index'];
222$response = $client->indices()->getMapping($params);
223
224// Get mappings for two indexes
225$params = [
226    'index' => [ 'my_index', 'my_index2' ]
227];
228$response = $client->indices()->getMapping($params);
229----
230{zwsp} +
231
232=== Other APIs in the Indices Namespace
233There are a number of other APIs in the indices namespace that allow you to manage your elasticsearch indexes (add/remove templates, flush segments, close indexes, etc).
234
235If you use an IDE with autocompletion, you should be able to easily explore the indices namespace by typing:
236
237[source,php]
238----
239$client->indices()->
240----
241And perusing the list of available methods.  Alternatively, browsing the `\Elasticsearch\Namespaces\Indices.php` file will show you the full list of available method calls (as well as parameter lists in the comments for each method).
242