PHP code example of daalvand / laravel-elasticsearch
1. Go to this page and download the library: Download daalvand/laravel-elasticsearch library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$myQuery = MyModel::newElasticsearchQuery()
->aggregation(
// The key of the aggregation (used in the Elasticsearch response)
'my_filter_aggregation',
// The type of the aggregation
'filter',
// A callback providing options to the aggregation, in this case adding filter criteria to a query builder
function ($query) {
$query->where('lost', '!=', true);
$query->where('concierge', true);
},
// A callback specifying a sub-aggregation
function ($builder) {
// A simpler aggregation, counting terms in the 'status' field
$builder->aggregation('my_terms_aggregation', 'terms', ['field' => 'status']);
}
);
$results = $myQuery->get();
$aggregations = $myQuery->getQuery()->getAggregationResults();