Skip to content

Commit

Permalink
add index create and drop ability
Browse files Browse the repository at this point in the history
  • Loading branch information
basemkhirat committed Jan 17, 2017
1 parent 84c7b8d commit 60f4011
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 9 deletions.
61 changes: 60 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,67 @@


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


#### Creating a new index

ES::index("my_index")->create();

# or

ES::create("my_index");


>
# [optional] you can create index with custom options

ES::index("my_index")->create(function($index){
$index->shards(5)->replicas(1)->mappping([
'my_type' => [
'properties' => [
'first_name' => [
'type' => 'string',
],
'age' => [
'type' => 'integer'
]
]
]
])
});

# or

ES::create("my_index", function($index){
$index->shards(5)->replicas(1)->mappping([
'my_type' => [
'properties' => [
'first_name' => [
'type' => 'string',
],
'age' => [
'type' => 'integer'
]
]
]
])
});


#### Dropping index

ES::index("my_index")->drop();
# or

ES::drop("my_index");

#### Running queries:

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

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


#### Available methods:

##### Getting document by id
Expand Down
2 changes: 0 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ function connection($name)

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

$config = $this->config["connections"][$name];

// Instantiate a new ClientBuilder
$clientBuilder = ClientBuilder::create();

Expand Down
166 changes: 166 additions & 0 deletions src/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

namespace Basemkhirat\Elasticsearch;

/**
* Class Index
* @package Basemkhirat\Elasticsearch\Query
*/
class Index
{

/**
* Native elasticsearch connection instance
* @var Connection
*/
public $connection;

/**
* Ignored HTTP errors
* @var array
*/
public $ignores = [400, 404, 500];

/**
* Index name
* @var string
*/
public $name;


/**
* Index create callback
* @var null
*/
public $callback;


/**
* Index shards
* @var int
*/
public $shards = 5;


/**
* Index replicas
* @var int
*/
public $replicas = 0;

/**
* Index mapping
* @var int
*/
public $mappings = [];


/**
* Index constructor.
* @param $name
* @param null $callback
*/
function __construct($name, $callback = NULL)
{
$this->name = $name;
$this->callback = $callback;
}


/**
* Set index shards
* @param $shards
* @return $this
*/
public function shards($shards)
{
$this->shards = $shards;

return $this;
}


/**
* Set index replicas
* @param $replicas
* @return $this
*/
public function replicas($replicas)
{

$this->replicas = $replicas;

return $this;
}

/**
* Create a new index
* @return mixed
*/
public function create()
{

$callback = $this->callback;

if (is_callable($callback)) {
$callback($this);
}

$params = [

'index' => $this->name,

'body' => [
"settings" => [
'number_of_shards' => $this->shards,
'number_of_replicas' => $this->replicas
]
],

'client' => [
'ignore' => $this->ignores
]
];

if (count($this->mappings)) {
$params["body"]["mappings"] = $this->mappings;
}

return $this->connection->indices()->create($params);

}

/**
* Drop index
* @return mixed
*/
public function drop()
{

$params = [
'index' => $this->name,
'client' => ['ignore' => $this->ignores]
];

return $this->connection->indices()->delete($params);

}

/**
* Fields mappings
* @param array $mappings
* @return $this
*/
public function mapping($mappings = [])
{

$this->mappings = $mappings;

return $this;

}


}


84 changes: 78 additions & 6 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class Query
*/
public $connection;

/**
* Ignored HTTP errors
* @var array
*/
public $ignores = [400, 404, 500];

/**
* Filter operators
* @var array
Expand Down Expand Up @@ -554,7 +560,9 @@ public function query()

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

"size" => $this->getTake()
"size" => $this->getTake(),

'client' => ['ignore' => $this->ignores]

];

Expand Down Expand Up @@ -718,7 +726,8 @@ public function insert($data, $_id = NULL)
"index" => $this->getIndex(),
"type" => $this->getType(),
"id" => $this->_id,
"body" => $data
"body" => $data,
'client' => ['ignore' => $this->ignores]
];

return (object)$this->connection->index($parameters);
Expand Down Expand Up @@ -772,7 +781,8 @@ public function update($data, $_id = NULL)
"index" => $this->getIndex(),
"type" => $this->getType(),
"id" => $this->_id,
"body" => ['doc' => $data]
"body" => ['doc' => $data],
'client' => ['ignore' => $this->ignores]
];

return (object)$this->connection->update($parameters);
Expand Down Expand Up @@ -829,7 +839,8 @@ public function script($script, $params = [])
"inline" => $script,
"params" => $params
]
]
],
'client' => ['ignore' => $this->ignores]
];

return (object)$this->connection->update($parameters);
Expand All @@ -852,14 +863,13 @@ public function delete($_id = NULL)
"index" => $this->getIndex(),
"type" => $this->getType(),
"id" => $this->_id,
'client' => ['ignore' => [400, 404]]
'client' => ['ignore' => $this->ignores]
];

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

}


/**
* Return the native connection to execute native query
* @return object
Expand All @@ -870,4 +880,66 @@ public function raw()
}


/**
* Create a new index
* @param $name
* @param bool $callback
* @return mixed
*/
function createIndex($name, $callback = false){

$index = new Index($name, $callback);

$index->connection = $this->connection;

return $index->create();

}


/**
* Drop index
* @param $name
* @return mixed
*/
function dropIndex($name){

$index = new Index($name);

$index->connection = $this->connection;

return $index->drop();

}


/**
* create a new index [alias to createIndex method]
* @param bool $callback
* @return mixed
*/
function create($callback = false){

$index = new Index($this->index, $callback);

$index->connection = $this->connection;

return $index->create();

}

/**
* Drop index [alias to dropIndex method]
* @return mixed
*/
function drop(){

$index = new Index($this->index);

$index->connection = $this->connection;

return $index->drop();

}

}

0 comments on commit 60f4011

Please sign in to comment.