4. Index¶
Index Information
1<?php
2
3$elastic = new ElasticSearch();
4
5$result = $elastic->query()->getIndices(); // It returns all indices information
6
7// You can get specific index information.
8
9$result = $elastic->query(['pointsort,location'])->getIndices();
10
11return $result;
Create Index
1<?php
2
3 $elastic = new ElasticSearch();
4 $index = $elastic->index()
5 ->name('my-index')
6 ->create();
7
8 return $index;
Delete Index
1<?php
2
3$index = $elastic->index()
4 ->name('my-index')
5 ->delete();
Settings
1<?php
2
3$index = $elastic->index()
4 ->name('my-index')
5 ->settings([
6 'number_of_shards' => 3,
7 'number_of_replicas' => 2
8 ])
9 ->create();
Mappings
1<?php
2
3$index = $elastic->index()
4 ->name('my-index')
5 ->mappings(function(){ // Closure $mappings
6 return $this->suggest([
7 'type' => 'completion',
8 ])
9 ->property('title', [
10 'type' => 'text'
11 ])
12 ->getMap();
13 })
14 ->create();
15
16 return $index;
Check if an index exists
1<?php
2
3$exists = $elastic->index()
4 ->exists([
5 'index' => 'my-index'
6 ]);
7
8 return $exists;
Get index settings
1<?php
2
3$settings = $elastic->index()
4 ->getSettings([
5 'index' => 'my-index'
6 ]);
7
8return $settings;
Get index mappings
1<?php
2
3$map = $elastic->index()
4 ->map([
5 'index' => 'my-index'
6 ]);
7
8return $map;
Update index mappings
1<?php
2
3 $index = $elastic->index()
4 ->name('my-index')
5 ->mappings(function(){
6 return $this
7 ->body([
8 'properties' => [
9 'location.keyword' => [
10 'type' => 'geo_shape',
11 ]
12 ]
13 ])
14 ->getMap();
15 })
16 ->updateMap();
17
18 return $index;