1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait QueryComponentFacetTrait|Firesphere\SolrSearch\Traits\QueryComponentFacetTrait Trait to set Faceting on fields |
4
|
|
|
* for the {@link \Firesphere\SolrSearch\Factories\QueryComponentFactory} |
5
|
|
|
* |
6
|
|
|
* @package Firesphere\SolrSearch\Traits |
7
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
8
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Firesphere\SolrSearch\Traits; |
12
|
|
|
|
13
|
|
|
use Firesphere\SolrSearch\Indexes\BaseIndex; |
14
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
15
|
|
|
use Minimalcode\Search\Criteria; |
16
|
|
|
use Solarium\Component\Facet\Field; |
17
|
|
|
use Solarium\QueryType\Select\Query\Query; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait QueryComponentFacetTrait deals with the facets. |
21
|
|
|
* |
22
|
|
|
* Faceting for any given query or index. |
23
|
|
|
* |
24
|
|
|
* @package Firesphere\SolrSearch\Traits |
25
|
|
|
*/ |
26
|
|
|
trait QueryComponentFacetTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var BaseIndex Index to query |
30
|
|
|
*/ |
31
|
|
|
protected $index; |
32
|
|
|
/** |
33
|
|
|
* @var BaseQuery Query to use |
34
|
|
|
*/ |
35
|
|
|
protected $query; |
36
|
|
|
/** |
37
|
|
|
* @var Query Solarium query |
38
|
|
|
*/ |
39
|
|
|
protected $clientQuery; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add facets from the index, to make sure Solr returns |
43
|
|
|
* the expected facets and their respective count on the |
44
|
9 |
|
* correct fields |
45
|
|
|
*/ |
46
|
9 |
|
protected function buildQueryFacets(): void |
47
|
|
|
{ |
48
|
9 |
|
$facets = $this->clientQuery->getFacetSet(); |
49
|
2 |
|
// Facets should be set from the index configuration |
50
|
2 |
|
foreach ($this->index->getFacetFields() as $config) { |
51
|
|
|
$shortClass = getShortFieldName($config['BaseClass']); |
52
|
2 |
|
$field = $shortClass . '_' . str_replace('.', '_', $config['Field']); |
53
|
2 |
|
/** @var Field $facet */ |
54
|
|
|
$facet = $facets->createFacetField('facet-' . $config['Title']); |
55
|
|
|
$facet->setField($field); |
56
|
9 |
|
} |
57
|
9 |
|
// Count however, comes from the query |
58
|
|
|
$facets->setMinCount($this->query->getFacetsMinCount()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
9 |
|
* Add AND facet filters based on the current request |
63
|
|
|
*/ |
64
|
9 |
|
protected function buildAndFacetFilterQuery() |
65
|
|
|
{ |
66
|
9 |
|
$filterFacets = $this->query->getAndFacetFilter(); |
67
|
9 |
|
/** @var null|Criteria $criteria */ |
68
|
2 |
|
$criteria = null; |
69
|
|
|
foreach ($this->index->getFacetFields() as $config) { |
70
|
1 |
|
if (isset($filterFacets[$config['Title']])) { |
71
|
2 |
|
// For the API generator, this needs to be old style list(); |
72
|
|
|
list($filter, $field) = $this->getFieldFacets($filterFacets, $config); |
73
|
|
|
$this->createFacetCriteria($criteria, $field, $filter); |
74
|
9 |
|
} |
75
|
1 |
|
} |
76
|
1 |
|
if ($criteria) { |
77
|
1 |
|
$this->clientQuery |
78
|
|
|
->createFilterQuery('andFacets') |
79
|
9 |
|
->setQuery($criteria->getQuery()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add OR facet filters based on the current request |
85
|
|
|
*/ |
86
|
|
|
protected function buildOrFacetFilterQuery() |
87
|
|
|
{ |
88
|
1 |
|
$filterFacets = $this->query->getOrFacetFilter(); |
89
|
|
|
$i = 0; |
90
|
1 |
|
/** @var null|Criteria $criteria */ |
91
|
1 |
|
foreach ($this->index->getFacetFields() as $config) { |
92
|
|
|
$criteria = null; |
93
|
1 |
|
if (isset($filterFacets[$config['Title']])) { |
94
|
1 |
|
// For the API generator, this needs to be old style list(); |
95
|
|
|
list($filter, $field) = $this->getFieldFacets($filterFacets, $config); |
96
|
1 |
|
$this->createFacetCriteria($criteria, $field, $filter); |
97
|
|
|
$this->clientQuery |
98
|
|
|
->createFilterQuery('orFacet-' . $i++) |
99
|
|
|
->setQuery($criteria->getQuery()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
1 |
|
* Get the field and it's respected values to filter on to generate Criteria from |
106
|
|
|
* |
107
|
1 |
|
* @param array $filterFacets |
108
|
1 |
|
* @param array $config |
109
|
|
|
* @return array |
110
|
1 |
|
*/ |
111
|
1 |
|
protected function getFieldFacets(array $filterFacets, $config): array |
112
|
|
|
{ |
113
|
1 |
|
$filter = $filterFacets[$config['Title']]; |
114
|
|
|
$filter = is_array($filter) ? $filter : [$filter]; |
115
|
|
|
// Fields are "short named" for convenience |
116
|
|
|
$shortClass = getShortFieldName($config['BaseClass']); |
117
|
|
|
$field = $shortClass . '_' . str_replace('.', '_', $config['Field']); |
118
|
|
|
|
119
|
|
|
return [$filter, $field]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Combine all facets as AND facet filters for the results |
124
|
|
|
* |
125
|
|
|
* @param null|Criteria $criteria |
126
|
|
|
* @param string $field |
127
|
|
|
* @param array $filter |
128
|
|
|
*/ |
129
|
|
|
protected function createFacetCriteria(&$criteria, string $field, array $filter) |
130
|
|
|
{ |
131
|
|
|
// If the criteria is empty, create a new one with a value from the filter array |
132
|
|
|
if (!$criteria) { |
133
|
|
|
$criteria = Criteria::where($field)->is(array_pop($filter)); |
134
|
|
|
} |
135
|
|
|
// Add the other items in the filter array, as an AND |
136
|
|
|
foreach ($filter as $filterValue) { |
137
|
|
|
$criteria->andWhere($field)->is($filterValue); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|