forked from pdphilip/laravel-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilesOrders.php
More file actions
169 lines (147 loc) · 5.62 KB
/
Copy pathCompilesOrders.php
File metadata and controls
169 lines (147 loc) · 5.62 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
declare(strict_types=1);
namespace PDPhilip\Elasticsearch\Query\Grammar\Concerns;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Support\Str;
use PDPhilip\Elasticsearch\Exceptions\BuilderException;
use PDPhilip\Elasticsearch\Query\Builder;
/**
* Order, sort, and highlight compilation.
* The stuff that makes results actually useful.
*/
trait CompilesOrders
{
/**
* Compile orderBy clauses into ES sort format.
*
* @throws BuilderException
*/
protected function compileOrders(Builder|BaseBuilder $builder, $orders = []): array
{
$compiledOrders = [];
foreach ($orders as $order) {
$column = $order['column'];
if (Str::startsWith($column, $builder->from.'.')) {
$column = Str::replaceFirst($builder->from.'.', '', $column);
}
$column = $this->prependParentField($column, $builder);
$column = $this->getIndexableField($column, $builder);
$type = $order['type'] ?? 'basic';
switch ($type) {
case 'geoDistance':
$orderSettings = [
$column => $order['options']['coordinates'],
'order' => $order['direction'] < 0 ? 'desc' : 'asc',
];
if (! empty($order['options']['unit'])) {
$orderSettings['unit'] = $order['options']['unit'];
}
if (! empty($order['options']['mode'])) {
$orderSettings['mode'] = $order['options']['mode'];
}
if (! empty($order['options']['distance_type'])) {
$orderSettings['distance_type'] = $order['options']['distance_type'];
}
$column = '_geo_distance';
break;
default:
$orderSettings = [
'order' => $order['direction'] < 0 ? 'desc' : 'asc',
];
$allowedOptions = ['missing', 'mode', 'nested', 'unmapped_type'];
$options = isset($order['options']) ? array_intersect_key($order['options'], array_flip($allowedOptions)) : [];
$orderSettings = array_merge($options, $orderSettings);
}
$compiledOrders[] = [
$column => $orderSettings,
];
}
return $compiledOrders;
}
/**
* Merge additional sort options into compiled orders.
* Handles the case where orderBy and sort() are both used.
*
* @throws BuilderException
*/
protected function compileSorts(Builder $builder, $sorts, $compiledOrders): array
{
foreach ($sorts as $column => $sort) {
$found = false;
$column = $this->prependParentField($column, $builder);
$column = $this->getIndexableField($column, $builder);
if ($compiledOrders) {
foreach ($compiledOrders as $i => $compiledOrder) {
if (array_key_exists($column, $compiledOrder)) {
$compiledOrders[$i][$column] = array_merge($compiledOrder[$column], $sort);
$found = true;
break;
}
}
}
if (! $found) {
$compiledOrders[] = [$column => $sort];
}
}
return $compiledOrders;
}
/**
* Compile highlight settings.
* For when you want to show users why they got these results.
*/
protected function compileHighlight(Builder $builder, mixed $highlight = []): array
{
$allowedArgs = [
'boundary_chars',
'boundary_max_scan',
'boundary_scanner',
'boundary_scanner_locale',
'encoder',
'fragmenter',
'force_source', // @deprecated Removed in Elasticsearch 9.x. Only works with ES 8.x (deprecated since 8.11)
'fragment_offset',
'fragment_size',
'highlight_query',
'matched_fields',
'number_of_fragments',
];
$compiledHighlights = $this->getAllowedOptions($highlight['options'], $allowedArgs);
foreach ($highlight['column'] as $column => $value) {
if (is_array($value)) {
$compiledHighlights['fields'][] = [$column => $value];
} elseif ($value != '*') {
$compiledHighlights['fields'][] = [$value => (object) []];
} else {
$compiledHighlights['fields'][] = ['*' => (object) []];
}
}
$compiledHighlights['pre_tags'] = $highlight['preTag'];
$compiledHighlights['post_tags'] = $highlight['postTag'];
return $compiledHighlights;
}
/**
* Build inner_hits options for nested queries.
* When you need to know which nested doc matched.
*/
public function _buildInnerHits($innerQuery)
{
$innerHits = [];
$compiledOrders = [];
if ($innerQuery->orders) {
$compiledOrders = $this->compileOrders($innerQuery, $innerQuery->orders);
}
if ($innerQuery->sorts) {
$compiledOrders = $this->compileSorts($innerQuery, $innerQuery->sorts, $compiledOrders);
}
if ($compiledOrders) {
$innerHits['sort'] = $compiledOrders;
}
if ($innerQuery->offset) {
$innerHits['from'] = $innerQuery->offset;
}
if ($size = $innerQuery->getSetLimit()) {
$innerHits['size'] = $size;
}
return $innerHits;
}
}