-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84c7b8d
commit 60f4011
Showing
4 changed files
with
304 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters