1
|
|
|
<?php |
2
|
|
|
namespace MAbadir\ElasticLaravel; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\Facade; |
5
|
|
|
use MAbadir\ElasticLaravel\Exceptions\NotValidEloquentException; |
6
|
|
|
|
7
|
|
|
class ElasticSearcher extends Facade |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Search the Elastic Search Index using the given parameters |
11
|
|
|
* |
12
|
|
|
* @param $params |
13
|
|
|
* |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
|
public static function search($params, $object = null) |
17
|
|
|
{ |
18
|
|
|
$query = self::buildQuery($params); |
19
|
|
|
|
20
|
|
|
if(isset($object)) |
21
|
|
|
{ |
22
|
|
|
$query = static::addType($object) + $query; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return static::performSearch($query); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Runs an advanced search against Elastic Search |
30
|
|
|
* @param $params |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public static function advanced($params, $object = null) |
35
|
|
|
{ |
36
|
|
|
$query = [ |
37
|
|
|
'body' => [ |
38
|
|
|
'query' => $params |
39
|
|
|
] |
40
|
|
|
]; |
41
|
|
|
if(isset($object)) |
42
|
|
|
{ |
43
|
|
|
$query = static::addType($object) + $query; |
44
|
|
|
} |
45
|
|
|
return static::performSearch($query); |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Calls the Search function on the ElasticSearch Client |
51
|
|
|
* @param $params |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
protected static function performSearch($params) |
56
|
|
|
{ |
57
|
|
|
return (new ElasticClient)->search($params); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Checks whether the passed is an instance |
62
|
|
|
* of Eloquent model or class name |
63
|
|
|
* |
64
|
|
|
* @param $object |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected static function addType($object) |
69
|
|
|
{ |
70
|
|
|
if(is_object($object)){ |
71
|
|
|
$type = static::checkValidEloquent($object); |
72
|
|
|
} |
73
|
|
|
else{ |
74
|
|
|
$type = (new $object)->getTable(); |
75
|
|
|
} |
76
|
|
|
return ["type"=>$type]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check whether the query is a simple text or an array |
81
|
|
|
* |
82
|
|
|
* @param $params |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
protected static function buildQuery($params) |
87
|
|
|
{ |
88
|
|
|
if (is_array($params)) { |
89
|
|
|
$query = self::handleArray($params); |
90
|
|
|
} else { |
91
|
|
|
$query = self::handleTerm($params); |
92
|
|
|
} |
93
|
|
|
return $query; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Handles search if parameter is an array |
98
|
|
|
* @param $params |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected static function handleArray($params) |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'body' => [ |
106
|
|
|
'query' => [ |
107
|
|
|
'match' => $params |
108
|
|
|
] |
109
|
|
|
] |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Handles search if parameter is a simple array |
115
|
|
|
* @param $params |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
protected static function handleTerm($params) |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
'body' => [ |
123
|
|
|
'query' => [ |
124
|
|
|
'match' => [ |
125
|
|
|
'_all' => $params |
126
|
|
|
] |
127
|
|
|
] |
128
|
|
|
] |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $object |
134
|
|
|
* |
135
|
|
|
* @return mixed |
136
|
|
|
* @throws \Exception |
137
|
|
|
*/ |
138
|
|
|
protected static function checkValidEloquent($object) |
139
|
|
|
{ |
140
|
|
|
if (is_subclass_of($object, '\Illuminate\Database\Eloquent\Model')) |
|
|
|
|
141
|
|
|
$type = $object->getTable(); |
142
|
|
|
else |
143
|
|
|
throw new NotValidEloquentException('Not a valid object, class should be extending Eloquent Model class.'); |
144
|
|
|
return $type; |
145
|
|
|
} |
146
|
|
|
} |