Elasticsearch compilation target for RulerZ
Elasticsearch compilation target for RulerZ., (*1)
Usage
elastic/elasticsearch-php is one
of the targets supported by RulerZ. It allows the engine to query an Elasticsearch
server., (*2)
This cookbook will show you how to retrieve objects using the official client
for Elasticsearch and RulerZ., (*3)
Here is a summary of what you will have to do:, (*4)
This subject won't be directly treated here. You can either follow the official
documentation
or use a bundle/module/whatever the framework you're using promotes., (*5)
Once elastic/elasticelasticsearch-php is installed and configured we can the RulerZ engine:, (*6)
$rulerz = new RulerZ(
$compiler, [
new \RulerZ\Elasticsearch\Target\Elasticsearch(), // this line is Elasticsearch-specific
// other compilation targets...
]
);
The only Elasticsearch-related configuration is the Elasticsearch
target being added to the
list of the known compilation targets., (*7)
Filter your target
Now that both elastic/elasticsearch-php and RulerZ are ready, you can use them
to retrieve data., (*8)
The Elasticsearch
instance that we previously injected into the RulerZ engine
only knows how to use Elasticsearch\Client
objects so the first step is
creating one:, (*9)
$client = new Elasticsearch\Client();
And as usual, we call RulerZ with our target (the Search
object) and our
rule.
RulerZ will build the right executor for the given target and use it to filter
the data, or in our case to retrieve data from Elasticsearch., (*10)
$rule = 'gender = :gender and points > :points';
$parameters = [
'points' => 30,
'gender' => 'M',
];
$executionContext = [
'index' => 'index_name',
'type' => 'type_name',
];
var_dump(
iterator_to_array($rulerz->filter($client, $rule, $parameters, $executionContext))
);
N.B: you'll notice an unusual variable named $executionContext
. It
contains a few parameters needed by the Elasticsearch
in order to make
the request and are mandatory., (*11)
That's it!, (*12)
License
This library is under the MIT license., (*13)