Skip to content

Commit

Permalink
Indices: simplify and allign index building.
Browse files Browse the repository at this point in the history
Refactored so it uses the same methology of query building or fragment building.
This should simplify it and make it clearer. I also believe its more flexibel.
  • Loading branch information
dieterve committed Sep 11, 2015
1 parent f7325cd commit aff13fd
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 92 deletions.
41 changes: 18 additions & 23 deletions docs/index-management.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Index Management

The package includes an indices manager. You are required to register indices that you'll using, whether its for
CRUD on the indices or for quering. The index manager is accessed via:
CRUD on the indices or for querying. The index manager is accessed via:

```php
$manager = $searcher->indicesManager();
Expand All @@ -20,33 +20,28 @@ class SuggestionsIndex extends \ElasticSearcher\Abstracts\AbstractIndex
return 'suggestions';
}

public function getTypes()
public function setup()
{
return array(
'books' => array(
'properties' => array(
'id' => array(
'type' => 'integer'
),
'name' => array(
'type' => 'string'
)
)
),
'movies' => array(
'properties' => array(
'name' => array(
'type' => 'string'
)
)
)
);
$this->setTypes([
'books' => [
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
]
],
'movies' => [
'properties' => [
'name' => ['type' => 'string'],
]
]
]);
}
}
```

This the minimum required for defining an index. If you require more extensive configuration, override the `getBody`
method.
This the minimum required for defining an index. Inside the `setup()` method you can setup your index further,
for example adding settings, aggregations, .... An index is [body aware](https://github.com/madewithlove/elasticsearcher/tree/master/src/Traits/BodyTrait.php)
for easy manipulation.

### Using re-useable fragments

Expand Down
50 changes: 37 additions & 13 deletions src/Abstracts/AbstractIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
namespace ElasticSearcher\Abstracts;

use ElasticSearcher\Parsers\FragmentParser;
use ElasticSearcher\Traits\BodyTrait;

/**
* Base class for indexes.
*/
abstract class AbstractIndex
{
use BodyTrait;

/**
* @var FragmentParser
*/
Expand All @@ -22,48 +25,69 @@ abstract public function getName();
/**
* @return array
*/
abstract public function getTypes();
abstract public function setup();

/**
*/
public function __construct()
{
$this->fragmentParser = new FragmentParser();

$this->setup();
}

/**
* @return array
*/
public function getBody()
{
$body = [
'settings' => $this->getSettings(),
'mappings' => $this->getTypes(),
];

// Replace fragments with their raw body.
$body = $this->fragmentParser->parse($body);
return $this->fragmentParser->parse($this->body);
}

/**
* @param array $types
*
* @return array
*/
public function setTypes(array $types)
{
return $this->set('mappings', $types);
}

/**
* @return array
*/
public function getTypes()
{
return $this->get('mappings');
}

return $body;
/**
* @param array $settings
*
* @return array
*/
public function setSettings(array $settings)
{
return $this->set('settings', $settings);
}

/**
* @return array
*/
public function getSettings()
{
return null;
return $this->get('settings');
}

/**
* @param string $type
*
* @return mixed
* @return array
*/
public function getType($type)
{
$types = $this->getTypes();

return $types[$type];
return $this->get('mappings.'.$type);
}
}
19 changes: 16 additions & 3 deletions src/Traits/BodyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function setBody(array $body)
$this->body = $body;
}

/**
* @return array
*/
public function getBody()
{
return $this->body;
}

/**
* Set a value in the body using the dotted notation.
*
Expand All @@ -44,10 +52,15 @@ public function set($key, $value)
}

/**
* @return array
* Get a value in the body using the dotted notation.
*
* @param string $key
* @param mixed $default
*
* @return $this
*/
public function getBody()
public function get($key, $default = null)
{
return $this->body;
return Arr::get($this->body, $key, $default);
}
}
42 changes: 16 additions & 26 deletions tests/dummy/Indexes/AuthorsIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,22 @@ public function getName()
return 'authors';
}

public function getTypes()
public function setup()
{
return array(
'directors' => array(
'properties' => array(
'id' => array(
'type' => 'integer'
),
'first_name' => array(
'type' => 'string'
),
'last_name' => array(
'type' => 'string'
)
)
),
'producers' => array(
'properties' => array(
'id' => array(
'type' => 'integer'
),
'name' => array(
'type' => 'string'
)
)
)
);
$this->setTypes([
'directors' => [
'properties' => [
'id' => ['type' => 'integer'],
'first_name' => ['type' => 'string'],
'last_name' => ['type' => 'string']
]
],
'producers' => [
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string']
]
]
]);
}
}
26 changes: 10 additions & 16 deletions tests/dummy/Indexes/MoviesIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@ public function getName()
return 'movies';
}

public function getTypes()
public function setup()
{
return array(
'movies' => array(
'properties' => array(
'id' => array(
'type' => 'integer'
),
'name' => array(
'type' => 'string'
),
'year' => array(
'type' => 'integer',
)
)
)
);
$this->setTypes([
'movies' => [
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
'year' => ['type' => 'integer'],
]
]
]);
}
}
12 changes: 1 addition & 11 deletions tests/dummy/Queries/MovieWithIDXQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ public function setup()
{
$this->searchIn('movies', 'movies');

$body = array(
'query' => array(
'filtered' => array(
'filter' => array(
new IDFilter($this->getData('id'))
)
)
)
);

$this->setBody($body);
$this->set('query.filtered.filter', [new IDFilter($this->getData('id'))]);
}
}

0 comments on commit aff13fd

Please sign in to comment.