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>|Expression|string $fields |
18
|
|
|
* @param string $searchText |
19
|
|
|
* @param string|null $analyzer |
20
|
|
|
* @return IlluminateQueryBuilder |
21
|
|
|
*/ |
22
|
4 |
|
public function searchView( |
23
|
|
|
array|Expression|string $fields, |
24
|
|
|
string $searchText, |
25
|
|
|
string $analyzer = null |
|
|
|
|
26
|
|
|
): IlluminateQueryBuilder { |
27
|
|
|
assert($this->grammar instanceof Grammar); |
28
|
|
|
|
29
|
4 |
|
if(!is_array($fields)) { |
|
|
|
|
30
|
1 |
|
$fields = Arr::wrap($fields); |
31
|
|
|
} |
32
|
4 |
|
$fields = $this->grammar->convertJsonFields($fields); |
33
|
4 |
|
$fields = $this->convertIdToKey($fields); |
|
|
|
|
34
|
|
|
|
35
|
4 |
|
$searchText = $this->bindValue($searchText, 'search'); |
|
|
|
|
36
|
|
|
|
37
|
4 |
|
$locale = App::getLocale(); |
38
|
4 |
|
$analyzerLocales = ['de','en','es','fi','fr','it','nl','no','pt','ru','sv','zh']; |
39
|
|
|
|
40
|
4 |
|
$analyzer = 'text_en'; |
41
|
4 |
|
if (in_array($locale, $analyzerLocales)) { |
42
|
4 |
|
$analyzer = 'text_' . $locale; |
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
$this->search = [ |
|
|
|
|
46
|
4 |
|
'fields' => $fields, |
47
|
4 |
|
'searchText' => $searchText, |
48
|
4 |
|
'analyzer' => $analyzer |
49
|
4 |
|
]; |
50
|
|
|
|
51
|
4 |
|
return $this; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Add an "order by best matching" clause to the query. |
57
|
|
|
* To be used with the searchView function |
58
|
|
|
* |
59
|
|
|
* @param string $direction |
60
|
|
|
* |
61
|
|
|
* @return IlluminateQueryBuilder |
62
|
|
|
*/ |
63
|
1 |
|
public function orderByBestMatching($direction = 'DESC'): IlluminateQueryBuilder |
64
|
|
|
{ |
65
|
1 |
|
$alias = $this->getTableAlias($this->from); |
|
|
|
|
66
|
1 |
|
$column = new Expression('BM25(' . $alias . ')'); |
67
|
|
|
|
68
|
1 |
|
$this->orderBy($column, $direction); |
|
|
|
|
69
|
|
|
|
70
|
1 |
|
return $this; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add an "order by best matching" clause to the query. |
75
|
|
|
* To be used with the searchView function |
76
|
|
|
* |
77
|
|
|
* @param string $direction |
78
|
|
|
* |
79
|
|
|
* @return IlluminateQueryBuilder |
80
|
|
|
*/ |
81
|
1 |
|
public function orderByFrequency($direction = 'DESC'): IlluminateQueryBuilder |
82
|
|
|
{ |
83
|
1 |
|
$alias = $this->getTableAlias($this->from); |
84
|
1 |
|
$column = new Expression('TFIDF(' . $alias . ')'); |
85
|
|
|
|
86
|
1 |
|
$this->orderBy($column, $direction); |
87
|
|
|
|
88
|
1 |
|
return $this; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.