Skip to content

Commit 2ac947a

Browse files
authored
Merge pull request #4 from ensi-platform/dev-facets
Facets
2 parents 6a8d2fa + d74bcb9 commit 2ac947a

31 files changed

+1641
-19
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class ProductSpecification extends CompositeSpecification
3737
AllowedAggregate::minmax('rating')
3838
]);
3939

40+
$this->allowedFacets([
41+
'package'
42+
]);
43+
4044
$this->whereNotNull('package');
4145

4246
$this->nested('offers', function (Specification $spec) {
@@ -50,6 +54,10 @@ class ProductSpecification extends CompositeSpecification
5054
$spec->allowedSorts([
5155
AllowedSort::field('price')->byMin()
5256
]);
57+
58+
$spec->allowedFacets([
59+
AllowedFacet::terms('seller_id')
60+
]);
5361
});
5462
}
5563
}
@@ -74,6 +82,15 @@ Here are examples of queries for this specification.
7482
}
7583
}
7684
```
85+
```json
86+
{
87+
"facet": ["seller_id", "package"],
88+
"filter": {
89+
"package": "bottle",
90+
"seller_id": [10, 20, 50, 90]
91+
}
92+
}
93+
```
7794

7895
The `nested` method adds specifications for nested documents. The names of filters, aggregates, and sorts are exported
7996
from them to the global scope without adding any prefixes. It is acceptable to have the same names for filters, but not
@@ -158,6 +175,25 @@ AllowedAggregate::minmax('name', 'field'); // Get min and max attribute values
158175

159176
Aggregates from nested specifications are added to the Elasticsearch query with all constraints and active filters.
160177

178+
You can use the `allowedFacets` method to define facets. Each facet requires an aggregate and one or
179+
more filters. You can use both the existing aggregate
180+
181+
```php
182+
AllowedFacet::fromAggregate('name', 'filter');
183+
```
184+
185+
and the aggregate created by the facet itself
186+
187+
```php
188+
AllowedFacet::terms('name', 'filter');
189+
AllowedFacet::minmax('name', ['filter1', 'filter2']);
190+
```
191+
192+
Filters are registered in the specification separately. Only their names are passed to facet creation methods.
193+
194+
During the calculation of the available values for each facet, all set filters are applied except those associated with
195+
this facet.
196+
161197
## Search for documents
162198

163199
```php
@@ -210,6 +246,32 @@ class ProductsController
210246
}
211247
```
212248

249+
## Determining the available facet values
250+
251+
```php
252+
use Ensi\LaravelElasticQuerySpecification\FacetQueryBuilder;
253+
use Ensi\LaravelElasticQuerySpecification\QueryBuilderRequest;
254+
255+
class ProductsFacetsQuery extends FacetQueryBuilder
256+
{
257+
public function __construct(QueryBuilderRequest $request)
258+
{
259+
parent::__construct(ProductsIndex::aggregate(), new ProductSpecification(), $request);
260+
}
261+
}
262+
```
263+
264+
```php
265+
class ProductsController
266+
{
267+
// ...
268+
public function facets(ProductsFacetsQuery $query)
269+
{
270+
return new ProductFacetsResource($query->get());
271+
}
272+
}
273+
```
274+
213275
## Contributing
214276

215277
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

config/elastic-query-specification.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
'filter' => 'filter',
99
'sort' => 'sort',
1010
'aggregate' => 'aggregate',
11+
'facet' => 'facet',
1112
],
1213
];

0 commit comments

Comments
 (0)