1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Traits; |
5
|
|
|
|
6
|
|
|
use Firesphere\SolrSearch\Indexes\BaseIndex; |
7
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
8
|
|
|
use Minimalcode\Search\Criteria; |
9
|
|
|
use Solarium\QueryType\Select\Query\Query; |
10
|
|
|
|
11
|
|
|
trait QueryComponentFacetTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var BaseIndex |
15
|
|
|
*/ |
16
|
|
|
protected $index; |
17
|
|
|
/** |
18
|
|
|
* @var BaseQuery |
19
|
|
|
*/ |
20
|
|
|
protected $query; |
21
|
|
|
/** |
22
|
|
|
* @var Query |
23
|
|
|
*/ |
24
|
|
|
protected $clientQuery; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Add facets from the index |
28
|
|
|
*/ |
29
|
5 |
|
protected function buildFacets(): void |
30
|
|
|
{ |
31
|
5 |
|
$facets = $this->clientQuery->getFacetSet(); |
32
|
|
|
// Facets should be set from the index configuration |
33
|
5 |
|
foreach ($this->index->getFacetFields() as $class => $config) { |
34
|
1 |
|
$shortClass = explode('\\', $class); |
35
|
1 |
|
$shortClass = end($shortClass); |
36
|
1 |
|
$field = $shortClass . '_' . str_replace('.', '_', $config['Field']); |
37
|
1 |
|
$facets->createFacetField('facet-' . $config['Title'])->setField($field); |
38
|
|
|
} |
39
|
|
|
// Count however, comes from the query |
40
|
5 |
|
$facets->setMinCount($this->query->getFacetsMinCount()); |
41
|
5 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add facet filters based on the current request |
45
|
|
|
*/ |
46
|
5 |
|
protected function buildFacetQuery() |
47
|
|
|
{ |
48
|
5 |
|
$filterFacets = $this->query->getFacetFilter(); |
49
|
5 |
|
foreach ($this->index->getFacetFields() as $class => $config) { |
50
|
1 |
|
if (array_key_exists($config['Title'], $filterFacets) && |
51
|
1 |
|
$filter = array_filter($filterFacets[$config['Title']], 'strlen') |
52
|
|
|
) { |
53
|
|
|
// @todo add unit tests for this bit. It's crucial but untested |
54
|
|
|
$filter = is_array($filter) ? $filter : [$filter]; |
55
|
|
|
$shortClass = explode('\\', $class); |
56
|
|
|
$shortClass = end($shortClass); |
57
|
|
|
$field = $shortClass . '_' . str_replace('.', '_', $config['Field']); |
58
|
|
|
$criteria = Criteria::where($field)->in($filter); |
59
|
|
|
$this->clientQuery |
60
|
|
|
->createFilterQuery('facet-' . $config['Title']) |
61
|
1 |
|
->setQuery($criteria->getQuery()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
5 |
|
} |
65
|
|
|
} |
66
|
|
|
|