ElasticsearchQuery.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace App\Search\Builders\Traits;
  3. use App\Search\Builders\SubBuilder;
  4. use Closure;
  5. /**
  6. * Trait ElasticsearchQuery
  7. * @package App\Search\Traits
  8. * Auth Zhong
  9. * Date 2019-04-13
  10. */
  11. trait ElasticsearchQuery
  12. {
  13. public $wheres = [];
  14. /**
  15. * @param string|Closure $field
  16. * @param mixed $value
  17. * @return $this
  18. */
  19. public function where($field, $value)
  20. {
  21. if ($field instanceof Closure) {
  22. $shouldBuilder=new SubBuilder();
  23. call_user_func($field, $shouldBuilder);
  24. $this->wheres['must']['query'][]=['bool'=>['must'=>$shouldBuilder->getQueryData()]];
  25. return $this;
  26. }
  27. $this->wheres['must']['where'][]=[
  28. 'field'=>$field,
  29. 'value'=>$value
  30. ];
  31. return $this;
  32. }
  33. /**
  34. * @param string|Closure $field
  35. * @param string|int $value
  36. * @return $this
  37. */
  38. public function whereNot($field, $value)
  39. {
  40. if ($field instanceof Closure) {
  41. $shouldBuilder=new SubBuilder();
  42. call_user_func($field, $shouldBuilder);
  43. $this->wheres['must_not']['query'][]=['bool'=>['must_not'=>$shouldBuilder->getQueryData()]];
  44. return $this;
  45. }
  46. $this->wheres['must_not']['where'][]=[
  47. 'field'=>$field,
  48. 'value'=>$value
  49. ];
  50. return $this;
  51. }
  52. /**
  53. * @param string|Closure $field
  54. * @param string|int $value
  55. * @return $this
  56. */
  57. public function whereOr($field, $value = null)
  58. {
  59. if ($field instanceof Closure) {
  60. $shouldBuilder=new SubBuilder();
  61. call_user_func($field, $shouldBuilder);
  62. $this->wheres['should']['query'][]=['bool'=>['should'=>$shouldBuilder->getQueryData()]];
  63. return $this;
  64. }
  65. $this->wheres['should']['where'][]=[
  66. 'field'=>$field,
  67. 'value'=>$value
  68. ];
  69. return $this;
  70. }
  71. /**
  72. * @param string $field
  73. * @param array $value
  74. * @param bool $isKeyword
  75. * @return $this
  76. */
  77. public function whereIn(string $field, array $value, $isKeyword = true)
  78. {
  79. if ($isKeyword) {
  80. $this->wheres['in']['terms'][]=[
  81. 'field'=>$field,
  82. 'value'=>$value
  83. ];
  84. } else {
  85. $this->wheres['in']['phrase'][]=[
  86. 'field'=>$field,
  87. 'value'=>$value
  88. ];
  89. }
  90. return $this;
  91. }
  92. /**
  93. * @param string $field
  94. * @param array $value
  95. * @param bool $isKeyword
  96. * @return $this
  97. */
  98. public function whereOrIn(string $field, array $value, $isKeyword = true)
  99. {
  100. if ($isKeyword) {
  101. $this->wheres['should_in']['terms'][]=[
  102. 'field'=>$field,
  103. 'value'=>$value
  104. ];
  105. } else {
  106. $this->wheres['should_in']['phrase'][]=[
  107. 'field'=>$field,
  108. 'value'=>$value
  109. ];
  110. }
  111. return $this;
  112. }
  113. /**
  114. * @param string $field
  115. * @param array $value
  116. * @param bool $isKeyword
  117. * @return $this
  118. */
  119. public function whereNotIn(string $field, array $value, $isKeyword = true)
  120. {
  121. if ($isKeyword) {
  122. $this->wheres['not_in']['terms'][]=[
  123. 'field'=>$field,
  124. 'value'=>$value
  125. ];
  126. } else {
  127. $this->wheres['not_in']['phrase'][]=[
  128. 'field'=>$field,
  129. 'value'=>$value
  130. ];
  131. }
  132. return $this;
  133. }
  134. /**
  135. * @param string $field
  136. * @param null $min
  137. * @param null $max
  138. * @param bool $equal
  139. * @return $this
  140. */
  141. public function whereRange(string $field, $min = null, $max = null, $equal = true)
  142. {
  143. $equal=$equal?'e':'';
  144. $range=[];
  145. if (!is_null($min)) {
  146. $range['gt'.$equal]=$min;
  147. }
  148. if (!is_null($max)) {
  149. $range['lt'.$equal]=$max;
  150. }
  151. $this->wheres['must']['range'][]=[
  152. 'field'=>$field,
  153. 'value'=>$range
  154. ];
  155. return $this;
  156. }
  157. /**
  158. * @param string $field
  159. * @param null $min
  160. * @param null $max
  161. * @param bool $equal
  162. * @return $this
  163. */
  164. public function whereOrRange(string $field, $min = null, $max = null, $equal = true)
  165. {
  166. $equal=$equal?'e':'';
  167. $range=[];
  168. if (!is_null($min)) {
  169. $range['gt'.$equal]=$min;
  170. }
  171. if (!is_null($max)) {
  172. $range['lt'.$equal]=$max;
  173. }
  174. $this->wheres['should']['range'][]=[
  175. 'field'=>$field,
  176. 'value'=>$range
  177. ];
  178. return $this;
  179. }
  180. /**
  181. * @param string $field
  182. * @param null $min
  183. * @param null $max
  184. * @param bool $equal
  185. * @return $this
  186. */
  187. public function whereNotRange(string $field, $min = null, $max = null, $equal = true)
  188. {
  189. $equal=$equal?'e':'';
  190. $range=[];
  191. if (!is_null($min)) {
  192. $range['gt'.$equal]=$min;
  193. }
  194. if (!is_null($max)) {
  195. $range['lt'.$equal]=$max;
  196. }
  197. $this->wheres['must_not']['range'][]=[
  198. 'field'=>$field,
  199. 'value'=>$range
  200. ];
  201. return $this;
  202. }
  203. public function getQueryData()
  204. {
  205. $query = [];
  206. $must_data = [];
  207. $must_not_data = [];
  208. $should_data = [];
  209. if (empty($this->wheres)) {
  210. return $query;
  211. }
  212. //处理and逻辑
  213. if (isset($this->wheres['must'])) {
  214. if (isset($this->wheres['must']['where'])) {
  215. foreach ($this->wheres['must']['where'] as $value) {
  216. $must_data[]=[
  217. "match_phrase"=>[$value['field'] => $value['value']]
  218. ];
  219. }
  220. }
  221. if (isset($this->wheres['must']['range'])) {
  222. foreach ($this->wheres['must']['range'] as $value) {
  223. $must_data[]=[
  224. "range"=>[$value['field'] => $value['value']]
  225. ];
  226. }
  227. }
  228. }
  229. if (isset($this->wheres['in'])) {
  230. if (isset($this->wheres['in']['terms'])) {
  231. foreach ($this->wheres['in']['terms'] as $value) {
  232. $must_data[]=[
  233. "terms"=>[$value['field'] => $value['value']]
  234. ];
  235. }
  236. }
  237. if (isset($this->wheres['in']['phrase'])) {
  238. $sub_data = [];
  239. foreach ($this->wheres['in']['phrase'] as $value) {
  240. foreach ($value['value'] as $v) {
  241. $sub_data[]=[
  242. "match_phrase"=>[$value['field'] => $v]
  243. ];
  244. }
  245. }
  246. $must_data[] = ['bool' => ['should'=>$sub_data]];
  247. }
  248. }
  249. if (isset($this->wheres['query']['must'])) {
  250. $must_data=array_merge($must_data, $this->wheres['query']['must']);
  251. }
  252. //处理not逻辑
  253. if (isset($this->wheres['must_not'])) {
  254. if (isset($this->wheres['must_not']['where'])) {
  255. foreach ($this->wheres['must_not']['where'] as $value) {
  256. $must_not_data[]=[
  257. "match_phrase"=>[$value['field'] => $value['value']]
  258. ];
  259. }
  260. }
  261. if (isset($this->wheres['must_not']['range'])) {
  262. foreach ($this->wheres['must_not']['range'] as $value) {
  263. $must_not_data[]=[
  264. "range"=>[$value['field'] => $value['value']]
  265. ];
  266. }
  267. }
  268. }
  269. if (isset($this->wheres['not_in'])) {
  270. if (isset($this->wheres['not_in']['terms'])) {
  271. foreach ($this->wheres['not_in']['terms'] as $value) {
  272. $must_not_data[]=[
  273. "terms"=>[$value['field'] => $value['value']]
  274. ];
  275. }
  276. }
  277. if (isset($this->wheres['not_in']['phrase'])) {
  278. foreach ($this->wheres['not_in']['phrase'] as $value) {
  279. foreach ($value['value'] as $v) {
  280. $must_not_data[]=[
  281. "match_phrase"=>[$value['field'] => $v]
  282. ];
  283. }
  284. }
  285. }
  286. }
  287. if (isset($this->wheres['query']['must_not'])) {
  288. $must_not_data=array_merge($must_not_data, $this->wheres['query']['must_not']);
  289. }
  290. //处理or逻辑
  291. if (isset($this->wheres['should'])) {
  292. if (isset($this->wheres['should']['where'])) {
  293. foreach ($this->wheres['should']['where'] as $value) {
  294. $should_data[]=[
  295. "match_phrase"=>[$value['field'] => $value['value']]
  296. ];
  297. }
  298. }
  299. if (isset($this->wheres['should']['range'])) {
  300. foreach ($this->wheres['should']['range'] as $value) {
  301. $should_data[]=[
  302. "range"=>[$value['field'] => $value['value']]
  303. ];
  304. }
  305. }
  306. }
  307. if (isset($this->wheres['should_in'])) {
  308. if (isset($this->wheres['should_in']['terms'])) {
  309. foreach ($this->wheres['should_in']['terms'] as $value) {
  310. $should_data[]=[
  311. "terms"=>[$value['field'] => $value['value']]
  312. ];
  313. }
  314. }
  315. if (isset($this->wheres['should_in']['phrase'])) {
  316. foreach ($this->wheres['should_in']['phrase'] as $value) {
  317. foreach ($value['value'] as $v) {
  318. $should_data[]=[
  319. "match_phrase"=>[$value['field'] => $v]
  320. ];
  321. }
  322. }
  323. }
  324. }
  325. if (isset($this->wheres['query']['should'])) {
  326. $should_data=array_merge($should_data, $this->wheres['query']['should']);
  327. }
  328. if ($must_data) {
  329. $query[]=['bool'=>['must'=>$must_data]];
  330. }
  331. if ($must_not_data) {
  332. $query[]=['bool'=>['must_not'=>$must_not_data]];
  333. }
  334. if ($should_data) {
  335. $query[]=['bool'=>['should'=>$should_data]];
  336. }
  337. return $query;
  338. }
  339. }