Skip to content

Commit 60f4011

Browse files
committed
add index create and drop ability
1 parent 84c7b8d commit 60f4011

File tree

4 files changed

+304
-9
lines changed

4 files changed

+304
-9
lines changed

readme.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,67 @@
2222

2323

2424
After publishing, the config file is placed here `config/es.php`
25-
where you can add more than one elasticsearch server.
25+
where you can add more than one elasticsearch node.
2626

2727

28+
#### Creating a new index
29+
30+
ES::index("my_index")->create();
31+
32+
# or
33+
34+
ES::create("my_index");
35+
36+
37+
>
38+
39+
# [optional] you can create index with custom options
40+
41+
ES::index("my_index")->create(function($index){
42+
43+
$index->shards(5)->replicas(1)->mappping([
44+
'my_type' => [
45+
'properties' => [
46+
'first_name' => [
47+
'type' => 'string',
48+
],
49+
'age' => [
50+
'type' => 'integer'
51+
]
52+
]
53+
]
54+
])
55+
56+
});
57+
58+
# or
59+
60+
ES::create("my_index", function($index){
61+
62+
$index->shards(5)->replicas(1)->mappping([
63+
'my_type' => [
64+
'properties' => [
65+
'first_name' => [
66+
'type' => 'string',
67+
],
68+
'age' => [
69+
'type' => 'integer'
70+
]
71+
]
72+
]
73+
])
74+
75+
});
76+
77+
78+
#### Dropping index
79+
80+
ES::index("my_index")->drop();
81+
82+
# or
83+
84+
ES::drop("my_index");
85+
2886
#### Running queries:
2987

3088
$documents = ES::connection("default")
@@ -40,6 +98,7 @@ the query builder will use the default connection, index, and type names setted
4098

4199
Index and type names setted in query will override values the configuration file
42100

101+
43102
#### Available methods:
44103

45104
##### Getting document by id

src/Connection.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ function connection($name)
6969

7070
if (array_key_exists($name, $this->config["connections"])) {
7171

72-
$config = $this->config["connections"][$name];
73-
7472
// Instantiate a new ClientBuilder
7573
$clientBuilder = ClientBuilder::create();
7674

src/Index.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
namespace Basemkhirat\Elasticsearch;
4+
5+
/**
6+
* Class Index
7+
* @package Basemkhirat\Elasticsearch\Query
8+
*/
9+
class Index
10+
{
11+
12+
/**
13+
* Native elasticsearch connection instance
14+
* @var Connection
15+
*/
16+
public $connection;
17+
18+
/**
19+
* Ignored HTTP errors
20+
* @var array
21+
*/
22+
public $ignores = [400, 404, 500];
23+
24+
/**
25+
* Index name
26+
* @var string
27+
*/
28+
public $name;
29+
30+
31+
/**
32+
* Index create callback
33+
* @var null
34+
*/
35+
public $callback;
36+
37+
38+
/**
39+
* Index shards
40+
* @var int
41+
*/
42+
public $shards = 5;
43+
44+
45+
/**
46+
* Index replicas
47+
* @var int
48+
*/
49+
public $replicas = 0;
50+
51+
/**
52+
* Index mapping
53+
* @var int
54+
*/
55+
public $mappings = [];
56+
57+
58+
/**
59+
* Index constructor.
60+
* @param $name
61+
* @param null $callback
62+
*/
63+
function __construct($name, $callback = NULL)
64+
{
65+
$this->name = $name;
66+
$this->callback = $callback;
67+
}
68+
69+
70+
/**
71+
* Set index shards
72+
* @param $shards
73+
* @return $this
74+
*/
75+
public function shards($shards)
76+
{
77+
$this->shards = $shards;
78+
79+
return $this;
80+
}
81+
82+
83+
/**
84+
* Set index replicas
85+
* @param $replicas
86+
* @return $this
87+
*/
88+
public function replicas($replicas)
89+
{
90+
91+
$this->replicas = $replicas;
92+
93+
return $this;
94+
}
95+
96+
/**
97+
* Create a new index
98+
* @return mixed
99+
*/
100+
public function create()
101+
{
102+
103+
$callback = $this->callback;
104+
105+
if (is_callable($callback)) {
106+
$callback($this);
107+
}
108+
109+
$params = [
110+
111+
'index' => $this->name,
112+
113+
'body' => [
114+
"settings" => [
115+
'number_of_shards' => $this->shards,
116+
'number_of_replicas' => $this->replicas
117+
]
118+
],
119+
120+
'client' => [
121+
'ignore' => $this->ignores
122+
]
123+
];
124+
125+
if (count($this->mappings)) {
126+
$params["body"]["mappings"] = $this->mappings;
127+
}
128+
129+
return $this->connection->indices()->create($params);
130+
131+
}
132+
133+
/**
134+
* Drop index
135+
* @return mixed
136+
*/
137+
public function drop()
138+
{
139+
140+
$params = [
141+
'index' => $this->name,
142+
'client' => ['ignore' => $this->ignores]
143+
];
144+
145+
return $this->connection->indices()->delete($params);
146+
147+
}
148+
149+
/**
150+
* Fields mappings
151+
* @param array $mappings
152+
* @return $this
153+
*/
154+
public function mapping($mappings = [])
155+
{
156+
157+
$this->mappings = $mappings;
158+
159+
return $this;
160+
161+
}
162+
163+
164+
}
165+
166+

src/Query.php

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ class Query
1919
*/
2020
public $connection;
2121

22+
/**
23+
* Ignored HTTP errors
24+
* @var array
25+
*/
26+
public $ignores = [400, 404, 500];
27+
2228
/**
2329
* Filter operators
2430
* @var array
@@ -554,7 +560,9 @@ public function query()
554560

555561
"from" => $this->getSkip(),
556562

557-
"size" => $this->getTake()
563+
"size" => $this->getTake(),
564+
565+
'client' => ['ignore' => $this->ignores]
558566

559567
];
560568

@@ -718,7 +726,8 @@ public function insert($data, $_id = NULL)
718726
"index" => $this->getIndex(),
719727
"type" => $this->getType(),
720728
"id" => $this->_id,
721-
"body" => $data
729+
"body" => $data,
730+
'client' => ['ignore' => $this->ignores]
722731
];
723732

724733
return (object)$this->connection->index($parameters);
@@ -772,7 +781,8 @@ public function update($data, $_id = NULL)
772781
"index" => $this->getIndex(),
773782
"type" => $this->getType(),
774783
"id" => $this->_id,
775-
"body" => ['doc' => $data]
784+
"body" => ['doc' => $data],
785+
'client' => ['ignore' => $this->ignores]
776786
];
777787

778788
return (object)$this->connection->update($parameters);
@@ -829,7 +839,8 @@ public function script($script, $params = [])
829839
"inline" => $script,
830840
"params" => $params
831841
]
832-
]
842+
],
843+
'client' => ['ignore' => $this->ignores]
833844
];
834845

835846
return (object)$this->connection->update($parameters);
@@ -852,14 +863,13 @@ public function delete($_id = NULL)
852863
"index" => $this->getIndex(),
853864
"type" => $this->getType(),
854865
"id" => $this->_id,
855-
'client' => ['ignore' => [400, 404]]
866+
'client' => ['ignore' => $this->ignores]
856867
];
857868

858869
return (object)$this->connection->delete($parameters);
859870

860871
}
861872

862-
863873
/**
864874
* Return the native connection to execute native query
865875
* @return object
@@ -870,4 +880,66 @@ public function raw()
870880
}
871881

872882

883+
/**
884+
* Create a new index
885+
* @param $name
886+
* @param bool $callback
887+
* @return mixed
888+
*/
889+
function createIndex($name, $callback = false){
890+
891+
$index = new Index($name, $callback);
892+
893+
$index->connection = $this->connection;
894+
895+
return $index->create();
896+
897+
}
898+
899+
900+
/**
901+
* Drop index
902+
* @param $name
903+
* @return mixed
904+
*/
905+
function dropIndex($name){
906+
907+
$index = new Index($name);
908+
909+
$index->connection = $this->connection;
910+
911+
return $index->drop();
912+
913+
}
914+
915+
916+
/**
917+
* create a new index [alias to createIndex method]
918+
* @param bool $callback
919+
* @return mixed
920+
*/
921+
function create($callback = false){
922+
923+
$index = new Index($this->index, $callback);
924+
925+
$index->connection = $this->connection;
926+
927+
return $index->create();
928+
929+
}
930+
931+
/**
932+
* Drop index [alias to dropIndex method]
933+
* @return mixed
934+
*/
935+
function drop(){
936+
937+
$index = new Index($this->index);
938+
939+
$index->connection = $this->connection;
940+
941+
return $index->drop();
942+
943+
}
944+
873945
}

0 commit comments

Comments
 (0)