1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Leopard\Business\Dsl; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
5
|
|
|
use ONGR\ElasticsearchDSL\Sort\FieldSort; |
6
|
|
|
use Triadev\Leopard\Busines\Dsl\Query\Specialized; |
7
|
|
|
use Triadev\Leopard\Business\Dsl\Query\Compound; |
8
|
|
|
use Triadev\Leopard\Business\Dsl\Query\Fulltext; |
9
|
|
|
use Triadev\Leopard\Business\Dsl\Query\Geo; |
10
|
|
|
use Triadev\Leopard\Business\Dsl\Query\InnerHit; |
11
|
|
|
use Triadev\Leopard\Business\Dsl\Query\Joining; |
12
|
|
|
use Triadev\Leopard\Business\Dsl\Query\TermLevel; |
13
|
|
|
|
14
|
|
|
class Search extends AbstractQuery |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Search constructor. |
18
|
|
|
* @param \ONGR\ElasticsearchDSL\Search|null $search |
19
|
|
|
* @param Model|null $model |
20
|
|
|
*/ |
21
|
30 |
|
public function __construct( |
22
|
|
|
?\ONGR\ElasticsearchDSL\Search $search = null, |
23
|
|
|
?Model $model = null |
24
|
|
|
) { |
25
|
30 |
|
parent::__construct($search, $model); |
26
|
30 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Aggregation |
30
|
|
|
* |
31
|
|
|
* @param \Closure $aggregation |
32
|
|
|
* @return Search |
33
|
|
|
*/ |
34
|
1 |
|
public function aggregation(\Closure $aggregation) : Search |
35
|
|
|
{ |
36
|
1 |
|
$aggregation(new Aggregation($this->search)); |
37
|
1 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Term level |
42
|
|
|
* |
43
|
|
|
* @return TermLevel |
44
|
|
|
*/ |
45
|
22 |
|
public function termLevel() : TermLevel |
46
|
|
|
{ |
47
|
22 |
|
return new TermLevel($this->search, $this->model); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Fulltext |
52
|
|
|
* |
53
|
|
|
* @return Fulltext |
54
|
|
|
*/ |
55
|
2 |
|
public function fulltext() : Fulltext |
56
|
|
|
{ |
57
|
2 |
|
return new Fulltext($this->search, $this->model); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Geo |
62
|
|
|
* |
63
|
|
|
* @return Geo |
64
|
|
|
*/ |
65
|
1 |
|
public function geo() : Geo |
66
|
|
|
{ |
67
|
1 |
|
return new Geo($this->search, $this->model); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Compound |
72
|
|
|
* |
73
|
|
|
* @return Compound |
74
|
|
|
*/ |
75
|
7 |
|
public function compound() : Compound |
76
|
|
|
{ |
77
|
7 |
|
return new Compound($this->search, $this->model); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Joining |
82
|
|
|
* |
83
|
|
|
* @return Joining |
84
|
|
|
*/ |
85
|
1 |
|
public function joining() : Joining |
86
|
|
|
{ |
87
|
1 |
|
return new Joining($this->search, $this->model); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Specialized |
92
|
|
|
* |
93
|
|
|
* @return Specialized |
94
|
|
|
*/ |
95
|
1 |
|
public function specialized() : Specialized |
96
|
|
|
{ |
97
|
1 |
|
return new Specialized($this->search, $this->model); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Inner hit |
102
|
|
|
* |
103
|
|
|
* @return InnerHit |
104
|
|
|
*/ |
105
|
1 |
|
public function innerHit() : InnerHit |
106
|
|
|
{ |
107
|
1 |
|
return new InnerHit($this->search, $this->model); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Paginate |
112
|
|
|
* |
113
|
|
|
* @param int $page |
114
|
|
|
* @param int $limit |
115
|
|
|
* @return Search |
116
|
|
|
*/ |
117
|
1 |
|
public function paginate(int $page, int $limit = 25) : Search |
118
|
|
|
{ |
119
|
1 |
|
$this->search->setFrom($limit * ($page - 1))->setSize($limit); |
120
|
1 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Min score |
125
|
|
|
* |
126
|
|
|
* @param int $minScore |
127
|
|
|
* @return Search |
128
|
|
|
*/ |
129
|
1 |
|
public function minScore(int $minScore) : Search |
130
|
|
|
{ |
131
|
1 |
|
$this->search->setMinScore($minScore); |
132
|
1 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Sort |
137
|
|
|
* |
138
|
|
|
* @param string $field |
139
|
|
|
* @param string $order |
140
|
|
|
* @param array $params |
141
|
|
|
* @return Search |
142
|
|
|
*/ |
143
|
1 |
|
public function sort(string $field, string $order = FieldSort::DESC, array $params = []) : Search |
144
|
|
|
{ |
145
|
1 |
|
$this->search->addSort(new FieldSort( |
146
|
1 |
|
$field, |
147
|
1 |
|
$order, |
148
|
1 |
|
$params |
149
|
|
|
)); |
150
|
|
|
|
151
|
1 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|