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
|
|
|
|
12
|
|
|
trait BuildsSearches |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Search an ArangoSearch view. |
16
|
|
|
*/ |
17
|
|
|
public function searchView( |
18
|
|
|
array|Expression|string $fields, |
19
|
|
|
string $searchText, |
20
|
|
|
string $analyzer = null |
21
|
|
|
): IlluminateQueryBuilder { |
22
|
|
|
if(!is_array($fields)) { |
|
|
|
|
23
|
|
|
$fields = Arr::wrap($fields); |
24
|
|
|
} |
25
|
|
|
$fields = $this->grammar->convertJsonFields($fields); |
26
|
|
|
$fields = $this->convertIdToKey($fields); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$searchText = $this->bindValue($searchText, 'search'); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$locale = App::getLocale(); |
31
|
|
|
$analyzerLocales = ['de','en','es','fi','fr','it','nl','no','pt','ru','sv','zh']; |
32
|
|
|
if ($analyzer === null && in_array($locale, $analyzerLocales)) { |
33
|
|
|
$analyzer = 'text_' . $locale; |
34
|
|
|
} else { |
35
|
|
|
$analyzer = 'text_en'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->search = [ |
|
|
|
|
39
|
|
|
'fields' => $fields, |
40
|
|
|
'searchText' => $searchText, |
41
|
|
|
'analyzer' => $analyzer |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
return $this; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add an "order by best matching" clause to the query. |
50
|
|
|
* To be used with the searchView function |
51
|
|
|
* |
52
|
|
|
* @param string $direction |
53
|
|
|
* |
54
|
|
|
* @return IlluminateQueryBuilder |
55
|
|
|
*/ |
56
|
|
|
public function orderByBestMatching($direction = 'DESC'): IlluminateQueryBuilder |
57
|
|
|
{ |
58
|
|
|
$alias = $this->getTableAlias($this->from); |
|
|
|
|
59
|
|
|
$column = new Expression('BM25(' . $alias . ')'); |
60
|
|
|
|
61
|
|
|
$this->orderBy($column, $direction); |
|
|
|
|
62
|
|
|
|
63
|
|
|
return $this; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add an "order by best matching" clause to the query. |
68
|
|
|
* To be used with the searchView function |
69
|
|
|
* |
70
|
|
|
* @param string $direction |
71
|
|
|
* |
72
|
|
|
* @return IlluminateQueryBuilder |
73
|
|
|
*/ |
74
|
|
|
public function orderByFrequency($direction = 'DESC'): IlluminateQueryBuilder |
75
|
|
|
{ |
76
|
|
|
$alias = $this->getTableAlias($this->from); |
77
|
|
|
$column = new Expression('TFIDF(' . $alias . ')'); |
78
|
|
|
|
79
|
|
|
$this->orderBy($column, $direction); |
80
|
|
|
|
81
|
|
|
return $this; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|