Skip to content

Commit

Permalink
Merge pull request #20 from madewithlove/add-dotted-notation-setters
Browse files Browse the repository at this point in the history
Simplify by accessing the body the same way everywhere
  • Loading branch information
dieterve committed Sep 24, 2015
2 parents 1e4dd5d + 82992a2 commit 7075062
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 191 deletions.
37 changes: 14 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ class MoviesFrom2014Query extends AbstractQuery
{
$this->searchIn('movies', 'movies');

$body = array(
'query' => array(
'filtered' => array(
'filter' => array(
'term' => array(
'year' => 2014
)
)
)
)
);

// Full notation
$body = [
'query' => [
'filtered' => [
'filter' => [
'term' => ['year' => 2014]
]
]
]
];
$this->setBody($body);

// Short (dotted) notation
$this->set('query.filtered.filter.term.year', 2014);
}
}

Expand All @@ -58,17 +59,7 @@ class MoviesFrom2014Query extends AbstractQuery
{
$this->searchIn('movies', 'movies');

$body = array(
'query' => array(
'filtered' => array(
'filter' => array(
new YearFilter(2014)
)
)
)
);

$this->setBody($body);
$this->set('query.filtered.filter', [new YearFilter(2014)]);
}
}
```
Expand Down
45 changes: 20 additions & 25 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 All @@ -65,9 +60,9 @@ $suggestionsIndex = new SuggestionsIndex();
$searcher->indicesManager()->register($suggestionsIndex);

// Grouped registration
$indices = array(
$indices = [
$suggestionsIndex
);
];
$searcher->indicesManager()->registerIndices($indices);

// Other
Expand Down
46 changes: 18 additions & 28 deletions docs/query-building.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ var_dump($result->getResults());

## Body building

Inside your Query class you can build the body of the query however you like. We only require you to call `setBody` with
the body array. Here are some examples.
Inside your Query class you can build the body of the query however you like. A query is
[body aware](https://github.com/madewithlove/elasticsearcher/tree/master/src/Traits/BodyTrait.php)
for easy manipulation. Here are some examples.

Basic example:

Expand All @@ -45,19 +46,20 @@ class MoviesYouMightLikeQuery extends QueryAbstract
{
$this->searchIn('suggestions', 'movies');

$body = array(
'query' => array(
'filtered' => array(
'filter' => array(
'term' => array(
'status' => 'active'
)
)
)
)
);

$this->setBody($body);
// Long notation
$body = [
'query' => [
'filtered' => [
'filter' => [
'term' => ['status' => 'active']
]
]
]
];
$this->setBody($body);

// Short dotted notation
$this->set('query.filtered.filter.term.status', 'active');
}
}
```
Expand All @@ -73,19 +75,7 @@ class MoviesYouMightLikeQuery extends AbstractQuery
{
$this->searchIn('suggestions', 'movies');

$body = array(
'query' => array(
'filtered' => array(
'filter' => array(
'term' => array(
'status' => $this->getData('status')
)
)
)
)
);

$this->setBody($body);
$this->set('query.filtered.filter.term.status', $this->getData('status'));
}
}
```
Expand Down
3 changes: 3 additions & 0 deletions docs/re-useable-fragments.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ comes with a build-in set of fragments but you are encouraged to build your own.
For example: `BookingRangeFilter(date, date)`, `MovieIDFilter(int)`, .... As long as they extend
`ElasticSearcher\Abstracts\AbstractFragment` they can be used in queries or indices.

A fragment is [body aware](https://github.com/madewithlove/elasticsearcher/tree/master/src/Traits/BodyTrait.php)
for easy manipulation.

## Examples

```php
Expand Down
26 changes: 3 additions & 23 deletions src/Abstracts/AbstractFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

namespace ElasticSearcher\Abstracts;

use ElasticSearcher\Traits\BodyTrait;

/**
* Base class for fragments that can be used in the body of requests to Elasticsearch.
*/
abstract class AbstractFragment
{
/**
* Body of the fragment to be executed. Should be the array as if you would pass it
* directly to the ElasticSearcher SDK.
*
* @var array
*/
protected $body;
use BodyTrait;

/**
* Should this fragment be merged with its parent, or simply be replaced.
Expand All @@ -23,20 +19,4 @@ abstract class AbstractFragment
* @var bool
*/
public $mergeWithParent = false;

/**
* @param array $body
*/
public function setBody(array $body)
{
$this->body = $body;
}

/**
* @return array
*/
public function getBody()
{
return $this->body;
}
}
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);
}
}
18 changes: 3 additions & 15 deletions src/Abstracts/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use ElasticSearcher\ElasticSearcher;
use ElasticSearcher\Parsers\ArrayResultParser;
use ElasticSearcher\Parsers\FragmentParser;
use ElasticSearcher\Traits\BodyTrait;

/**
* Base class for queries.
*/
abstract class AbstractQuery
{
use BodyTrait;

/**
* @var ElasticSearcher
*/
Expand All @@ -30,13 +33,6 @@ abstract class AbstractQuery
*/
protected $types = [];

/**
* Body of the query to execute.
*
* @var array
*/
protected $body = [];

/**
* Data that can be used when building a query.
*
Expand Down Expand Up @@ -142,14 +138,6 @@ protected function searchInTypes(array $types)
$this->types = array_unique($this->types);
}

/**
* @param array $body
*/
protected function setBody(array $body)
{
$this->body = $body;
}

/**
* Build the query by adding all chunks together.
*
Expand Down
Loading

0 comments on commit 7075062

Please sign in to comment.