-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIndexer.php
137 lines (107 loc) · 3.47 KB
/
Indexer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Mgussekloo\FacetFilter;
use DB;
use Mgussekloo\FacetFilter\Facades\FacetFilter;
class Indexer
{
public $models;
public $facetClass;
public $facetRowClass;
public function __construct($models = null)
{
$this->models = $models;
$this->facetClass = config('facet-filter.classes.facet');
$this->facetRowClass = config('facet-filter.classes.facetrow');
}
public function buildRow($facet, $model, $value)
{
return [
'facet_slug' => $facet->getSlug(),
'subject_id' => $model->id,
'value' => $value,
];
}
public function buildValues($facet, $model)
{
$values = [];
if (isset($facet->fieldname)) {
$fields = explode('.', (string) $facet->fieldname);
if (count($fields) == 1) {
$values = collect([$model->{$fields[0]}]);
} else {
$last_key = array_key_last($fields);
$values = collect([$model]);
foreach ($fields as $key => $field) {
$values = $values->pluck($field);
if ($key !== $last_key) {
$values = $values->flatten(1);
}
}
}
return $values->toArray();
}
return $values;
}
public function insertRows($rows)
{
$chunks = array_chunk($rows, 1000);
foreach ($chunks as $chunk) {
$this->facetRowClass::insert(array_values($chunk));
}
}
public function resetRows($models = null): self
{
if (is_null($models) || $models->isEmpty()) {
return $this->resetIndex();
}
foreach ($models as $model) {
FacetFilter::getFacets($model::class, false, false)->each(function ($facet) use ($model) {
$this->facetRowClass::where('subject_id', $model->id)
->where('facet_slug', $facet->getSlug())
->delete();
});
}
FacetFilter::forgetCache();
return $this;
}
public function resetIndex()
{
$this->facetRowClass::truncate();
FacetFilter::forgetCache();
return $this;
}
public function buildIndex($models = null)
{
if (!is_null($models)) {
$this->models = $models;
}
if (! is_null($this->models) && $this->models->isNotEmpty()) {
$subjectType = $this->models->first()::class;
$facets = FacetFilter::getFacets($subjectType, false, false);
$now = now();
$rows = [];
foreach ($this->models as $model) {
foreach ($facets as $facet) {
$values = $this->buildValues($facet, $model);
if (!is_array($values)) {
$values = [$values];
}
foreach ($values as $value) {
if (is_null($value)) {
continue;
}
$uniqueKey = implode('.', [$facet->getSlug(), $model->id, $value]);
$row = $this->buildRow($facet, $model, $value);
$row = array_merge([
'created_at' => $now,
// 'updated_at' => null,
], $row);
$rows[$uniqueKey] = $row;
}
}
}
$this->insertRows($rows);
}
return $this;
}
}