1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
8
|
|
|
use Illuminate\Database\Query\Expression; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
use Illuminate\Support\Facades\App; |
11
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Grammar; |
12
|
|
|
|
13
|
|
|
trait BuildsSearches |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Search an ArangoSearch view. |
17
|
|
|
* @param array<mixed>|string $fields |
18
|
|
|
* @param string $searchText |
19
|
|
|
* @param string|null $analyzer |
20
|
|
|
* @return IlluminateQueryBuilder |
21
|
|
|
*/ |
22
|
6 |
|
public function searchView( |
23
|
|
|
$fields, |
24
|
|
|
$searchText, |
25
|
|
|
$analyzer = null, |
26
|
|
|
): IlluminateQueryBuilder { |
27
|
|
|
assert($this->grammar instanceof Grammar); |
28
|
|
|
|
29
|
6 |
|
if (!is_array($fields)) { |
30
|
2 |
|
$fields = Arr::wrap($fields); |
31
|
|
|
} |
32
|
6 |
|
$fields = $this->grammar->convertJsonFields($fields); |
33
|
6 |
|
$fields = $this->convertIdToKey($fields); |
|
|
|
|
34
|
|
|
|
35
|
6 |
|
$searchText = $this->bindValue($searchText, 'search'); |
|
|
|
|
36
|
|
|
|
37
|
6 |
|
$locale = App::getLocale(); |
38
|
6 |
|
$analyzerLocales = ['de','en','es','fi','fr','it','nl','no','pt','ru','sv','zh']; |
39
|
|
|
|
40
|
|
|
|
41
|
6 |
|
if (!$analyzer && in_array($locale, $analyzerLocales)) { |
42
|
5 |
|
$analyzer = 'text_' . $locale; |
43
|
|
|
} |
44
|
6 |
|
if (!$analyzer) { |
45
|
|
|
$analyzer = 'text_en'; |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
$this->search = [ |
|
|
|
|
49
|
6 |
|
'fields' => $fields, |
50
|
6 |
|
'searchText' => $searchText, |
51
|
6 |
|
'analyzer' => $analyzer, |
52
|
6 |
|
]; |
53
|
|
|
|
54
|
6 |
|
return $this; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Search an ArangoSearch view ith a raw search expression. |
59
|
|
|
* |
60
|
|
|
* @param Expression|string $rawSearch |
61
|
|
|
* @param array<mixed> $bindings |
62
|
|
|
* @return IlluminateQueryBuilder |
63
|
|
|
*/ |
64
|
1 |
|
public function rawSearchView( |
65
|
|
|
$rawSearch, |
66
|
|
|
$bindings = [], |
67
|
|
|
): IlluminateQueryBuilder { |
68
|
|
|
assert($this->grammar instanceof Grammar); |
69
|
|
|
|
70
|
1 |
|
if (is_string($rawSearch)) { |
71
|
1 |
|
$rawSearch = new Expression($rawSearch); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
$this->search = [ |
|
|
|
|
75
|
1 |
|
'expression' => $rawSearch, |
76
|
1 |
|
]; |
77
|
|
|
|
78
|
1 |
|
$this->bindings['search'] = $bindings; |
|
|
|
|
79
|
|
|
|
80
|
1 |
|
return $this; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add an "order by best matching" clause to the query. |
86
|
|
|
* To be used with the searchView function |
87
|
|
|
* |
88
|
|
|
* @param string $direction |
89
|
|
|
* |
90
|
|
|
* @return IlluminateQueryBuilder |
91
|
|
|
*/ |
92
|
3 |
|
public function orderByBestMatching($direction = 'DESC'): IlluminateQueryBuilder |
93
|
|
|
{ |
94
|
3 |
|
$alias = $this->getTableAlias($this->from); |
|
|
|
|
95
|
3 |
|
$column = new Expression('BM25(' . $alias . ')'); |
|
|
|
|
96
|
|
|
|
97
|
3 |
|
$this->orderBy($column, $direction); |
|
|
|
|
98
|
|
|
|
99
|
3 |
|
return $this; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add an "order by best matching" clause to the query. |
104
|
|
|
* To be used with the searchView function |
105
|
|
|
* |
106
|
|
|
* @param string $direction |
107
|
|
|
* |
108
|
|
|
* @return IlluminateQueryBuilder |
109
|
|
|
*/ |
110
|
1 |
|
public function orderByFrequency($direction = 'DESC'): IlluminateQueryBuilder |
111
|
|
|
{ |
112
|
1 |
|
$alias = $this->getTableAlias($this->from); |
113
|
1 |
|
$column = new Expression('TFIDF(' . $alias . ')'); |
|
|
|
|
114
|
|
|
|
115
|
1 |
|
$this->orderBy($column, $direction); |
116
|
|
|
|
117
|
1 |
|
return $this; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|