QueryBuilder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\elasticsearch;
  8. use yii\base\BaseObject;
  9. use yii\base\InvalidArgumentException;
  10. use yii\base\NotSupportedException;
  11. use yii\helpers\Json;
  12. /**
  13. * QueryBuilder builds an Elasticsearch query based on the specification given as a [[Query]] object.
  14. *
  15. * @author Carsten Brandt <mail@cebe.cc>
  16. * @since 2.0
  17. */
  18. class QueryBuilder extends BaseObject
  19. {
  20. /**
  21. * @var Connection the database connection.
  22. */
  23. public $db;
  24. /**
  25. * Constructor.
  26. * @param Connection $connection the database connection.
  27. * @param array $config name-value pairs that will be used to initialize the object properties
  28. */
  29. public function __construct($connection, $config = [])
  30. {
  31. $this->db = $connection;
  32. parent::__construct($config);
  33. }
  34. /**
  35. * Generates query from a [[Query]] object.
  36. * @param Query $query the [[Query]] object from which the query will be generated
  37. * @return array the generated SQL statement (the first array element) and the corresponding
  38. * parameters to be bound to the SQL statement (the second array element).
  39. */
  40. public function build($query)
  41. {
  42. $parts = [];
  43. if ($query->storedFields !== null) {
  44. $parts['stored_fields'] = $query->storedFields;
  45. }
  46. if ($query->scriptFields !== null) {
  47. $parts['script_fields'] = $query->scriptFields;
  48. }
  49. if ($query->source !== null) {
  50. $parts['_source'] = $query->source;
  51. }
  52. if ($query->limit !== null && $query->limit >= 0) {
  53. $parts['size'] = $query->limit;
  54. }
  55. if ($query->offset > 0) {
  56. $parts['from'] = (int)$query->offset;
  57. }
  58. if (isset($query->minScore)) {
  59. $parts['min_score'] = (float)$query->minScore;
  60. }
  61. if (isset($query->explain)) {
  62. $parts['explain'] = $query->explain;
  63. }
  64. // combine query with where
  65. $conditionals = [];
  66. $whereQuery = $this->buildQueryFromWhere($query->where);
  67. if ($whereQuery) {
  68. $conditionals[] = $whereQuery;
  69. }
  70. if ($query->query) {
  71. $conditionals[] = $query->query;
  72. }
  73. if (count($conditionals) === 2) {
  74. $parts['query'] = ['bool' => ['must' => $conditionals]];
  75. } elseif (count($conditionals) === 1) {
  76. $parts['query'] = reset($conditionals);
  77. }
  78. if (!empty($query->highlight)) {
  79. $parts['highlight'] = $query->highlight;
  80. }
  81. if (!empty($query->aggregations)) {
  82. $parts['aggregations'] = $query->aggregations;
  83. }
  84. if (!empty($query->stats)) {
  85. $parts['stats'] = $query->stats;
  86. }
  87. if (!empty($query->suggest)) {
  88. $parts['suggest'] = $query->suggest;
  89. }
  90. if (!empty($query->postFilter)) {
  91. $parts['post_filter'] = $query->postFilter;
  92. }
  93. if (!empty($query->collapse)) {
  94. $parts['collapse'] = $query->collapse;
  95. }
  96. $sort = $this->buildOrderBy($query->orderBy);
  97. if (!empty($sort)) {
  98. $parts['sort'] = $sort;
  99. }
  100. $options = $query->options;
  101. if ($query->timeout !== null) {
  102. $options['timeout'] = $query->timeout;
  103. }
  104. return [
  105. 'queryParts' => $parts,
  106. 'index' => $query->index,
  107. 'type' => $query->type,
  108. 'options' => $options,
  109. ];
  110. }
  111. /**
  112. * adds order by condition to the query
  113. */
  114. public function buildOrderBy($columns)
  115. {
  116. if (empty($columns)) {
  117. return [];
  118. }
  119. $orders = [];
  120. foreach ($columns as $name => $direction) {
  121. if (is_string($direction)) {
  122. $column = $direction;
  123. $direction = SORT_ASC;
  124. } else {
  125. $column = $name;
  126. }
  127. if ($this->db->dslVersion < 7) {
  128. if ($column == '_id') {
  129. $column = '_uid';
  130. }
  131. }
  132. // allow Elasticsearch extended syntax as described in https://www.elastic.co/guide/en/elasticsearch/guide/master/_sorting.html
  133. if (is_array($direction)) {
  134. $orders[] = [$column => $direction];
  135. } else {
  136. $orders[] = [$column => ($direction === SORT_DESC ? 'desc' : 'asc')];
  137. }
  138. }
  139. return $orders;
  140. }
  141. public function buildQueryFromWhere($condition) {
  142. $where = $this->buildCondition($condition);
  143. if ($where) {
  144. $query = [
  145. 'constant_score' => [
  146. 'filter' => $where,
  147. ],
  148. ];
  149. return $query;
  150. } else {
  151. return null;
  152. }
  153. }
  154. /**
  155. * Parses the condition specification and generates the corresponding SQL expression.
  156. *
  157. * @param string|array $condition the condition specification. Please refer to [[Query::where()]] on how to specify a condition.
  158. * @throws \yii\base\InvalidArgumentException if unknown operator is used in query
  159. * @throws \yii\base\NotSupportedException if string conditions are used in where
  160. * @return string the generated SQL expression
  161. */
  162. public function buildCondition($condition)
  163. {
  164. static $builders = [
  165. 'not' => 'buildNotCondition',
  166. 'and' => 'buildBoolCondition',
  167. 'or' => 'buildBoolCondition',
  168. 'between' => 'buildBetweenCondition',
  169. 'not between' => 'buildBetweenCondition',
  170. 'in' => 'buildInCondition',
  171. 'not in' => 'buildInCondition',
  172. 'like' => 'buildLikeCondition',
  173. 'not like' => 'buildLikeCondition',
  174. 'or like' => 'buildLikeCondition',
  175. 'or not like' => 'buildLikeCondition',
  176. 'lt' => 'buildHalfBoundedRangeCondition',
  177. '<' => 'buildHalfBoundedRangeCondition',
  178. 'lte' => 'buildHalfBoundedRangeCondition',
  179. '<=' => 'buildHalfBoundedRangeCondition',
  180. 'gt' => 'buildHalfBoundedRangeCondition',
  181. '>' => 'buildHalfBoundedRangeCondition',
  182. 'gte' => 'buildHalfBoundedRangeCondition',
  183. '>=' => 'buildHalfBoundedRangeCondition',
  184. ];
  185. if (empty($condition)) {
  186. return [];
  187. }
  188. if (!is_array($condition)) {
  189. throw new NotSupportedException('String conditions in where() are not supported by Elasticsearch.');
  190. }
  191. if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ...
  192. $operator = strtolower($condition[0]);
  193. if (isset($builders[$operator])) {
  194. $method = $builders[$operator];
  195. array_shift($condition);
  196. return $this->$method($operator, $condition);
  197. } else {
  198. throw new InvalidArgumentException('Found unknown operator in query: ' . $operator);
  199. }
  200. } else { // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
  201. return $this->buildHashCondition($condition);
  202. }
  203. }
  204. private function buildHashCondition($condition)
  205. {
  206. $parts = $emptyFields = [];
  207. foreach ($condition as $attribute => $value) {
  208. if ($attribute == '_id') {
  209. if ($value === null) { // there is no null pk
  210. $parts[] = ['bool' => ['must_not' => [['match_all' => new \stdClass()]]]]; // this condition is equal to WHERE false
  211. } else {
  212. $parts[] = ['ids' => ['values' => is_array($value) ? $value : [$value]]];
  213. }
  214. } else {
  215. if (is_array($value)) { // IN condition
  216. $parts[] = ['terms' => [$attribute => $value]];
  217. } else {
  218. if ($value === null) {
  219. $emptyFields[] = [ 'exists' => [ 'field' => $attribute ] ];
  220. } else {
  221. $parts[] = ['term' => [$attribute => $value]];
  222. }
  223. }
  224. }
  225. }
  226. $query = [ 'must' => $parts ];
  227. if ($emptyFields) {
  228. $query['must_not'] = $emptyFields;
  229. }
  230. return [ 'bool' => $query ];
  231. }
  232. private function buildNotCondition($operator, $operands)
  233. {
  234. if (count($operands) != 1) {
  235. throw new InvalidArgumentException("Operator '$operator' requires exactly one operand.");
  236. }
  237. $operand = reset($operands);
  238. if (is_array($operand)) {
  239. $operand = $this->buildCondition($operand);
  240. }
  241. return [
  242. 'bool' => [
  243. 'must_not' => $operand,
  244. ],
  245. ];
  246. }
  247. private function buildBoolCondition($operator, $operands)
  248. {
  249. $parts = [];
  250. if ($operator === 'and') {
  251. $clause = 'must';
  252. } else if ($operator === 'or') {
  253. $clause = 'should';
  254. } else {
  255. throw new InvalidArgumentException("Operator should be 'or' or 'and'");
  256. }
  257. foreach ($operands as $operand) {
  258. if (is_array($operand)) {
  259. $operand = $this->buildCondition($operand);
  260. }
  261. if (!empty($operand)) {
  262. $parts[] = $operand;
  263. }
  264. }
  265. if ($parts) {
  266. return [
  267. 'bool' => [
  268. $clause => $parts,
  269. ]
  270. ];
  271. } else {
  272. return null;
  273. }
  274. }
  275. private function buildBetweenCondition($operator, $operands)
  276. {
  277. if (!isset($operands[0], $operands[1], $operands[2])) {
  278. throw new InvalidArgumentException("Operator '$operator' requires three operands.");
  279. }
  280. list($column, $value1, $value2) = $operands;
  281. if ($column === '_id') {
  282. throw new NotSupportedException('Between condition is not supported for the _id field.');
  283. }
  284. $filter = ['range' => [$column => ['gte' => $value1, 'lte' => $value2]]];
  285. if ($operator === 'not between') {
  286. $filter = ['bool' => ['must_not'=>$filter]];
  287. }
  288. return $filter;
  289. }
  290. private function buildInCondition($operator, $operands)
  291. {
  292. if (!isset($operands[0], $operands[1]) || !is_array($operands)) {
  293. throw new InvalidArgumentException("Operator '$operator' requires array of two operands: column and values");
  294. }
  295. list($column, $values) = $operands;
  296. $values = (array)$values;
  297. if (empty($values) || $column === []) {
  298. return $operator === 'in' ? ['bool' => ['must_not' => [['match_all' => new \stdClass()]]]] : []; // this condition is equal to WHERE false
  299. }
  300. if (is_array($column)) {
  301. if (count($column) > 1) {
  302. return $this->buildCompositeInCondition($operator, $column, $values);
  303. }
  304. $column = reset($column);
  305. }
  306. $canBeNull = false;
  307. foreach ($values as $i => $value) {
  308. if (is_array($value)) {
  309. $values[$i] = $value = isset($value[$column]) ? $value[$column] : null;
  310. }
  311. if ($value === null) {
  312. $canBeNull = true;
  313. unset($values[$i]);
  314. }
  315. }
  316. if ($column === '_id') {
  317. if (empty($values) && $canBeNull) { // there is no null pk
  318. $filter = ['bool' => ['must_not' => [['match_all' => new \stdClass()]]]]; // this condition is equal to WHERE false
  319. } else {
  320. $filter = ['ids' => ['values' => array_values($values)]];
  321. if ($canBeNull) {
  322. $filter = [
  323. 'bool' => [
  324. 'should' => [
  325. $filter,
  326. 'bool' => ['must_not' => ['exists' => ['field'=>$column]]],
  327. ],
  328. ],
  329. ];
  330. }
  331. }
  332. } else {
  333. if (empty($values) && $canBeNull) {
  334. $filter = [
  335. 'bool' => [
  336. 'must_not' => [
  337. 'exists' => [ 'field' => $column ],
  338. ]
  339. ]
  340. ];
  341. } else {
  342. $filter = [ 'terms' => [$column => array_values($values)] ];
  343. if ($canBeNull) {
  344. $filter = [
  345. 'bool' => [
  346. 'should' => [
  347. $filter,
  348. 'bool' => ['must_not' => ['exists' => ['field'=>$column]]],
  349. ],
  350. ],
  351. ];
  352. }
  353. }
  354. }
  355. if ($operator === 'not in') {
  356. $filter = [
  357. 'bool' => [
  358. 'must_not' => $filter,
  359. ],
  360. ];
  361. }
  362. return $filter;
  363. }
  364. /**
  365. * Builds a half-bounded range condition
  366. * (for "gt", ">", "gte", ">=", "lt", "<", "lte", "<=" operators)
  367. * @param string $operator
  368. * @param array $operands
  369. * @return array Filter expression
  370. */
  371. private function buildHalfBoundedRangeCondition($operator, $operands)
  372. {
  373. if (!isset($operands[0], $operands[1])) {
  374. throw new InvalidArgumentException("Operator '$operator' requires two operands.");
  375. }
  376. list($column, $value) = $operands;
  377. if ($this->db->dslVersion < 7) {
  378. if ($column === '_id') {
  379. $column = '_uid';
  380. }
  381. }
  382. $range_operator = null;
  383. if (in_array($operator, ['gte', '>='])) {
  384. $range_operator = 'gte';
  385. } elseif (in_array($operator, ['lte', '<='])) {
  386. $range_operator = 'lte';
  387. } elseif (in_array($operator, ['gt', '>'])) {
  388. $range_operator = 'gt';
  389. } elseif (in_array($operator, ['lt', '<'])) {
  390. $range_operator = 'lt';
  391. }
  392. if ($range_operator === null) {
  393. throw new InvalidArgumentException("Operator '$operator' is not implemented.");
  394. }
  395. $filter = [
  396. 'range' => [
  397. $column => [
  398. $range_operator => $value
  399. ]
  400. ]
  401. ];
  402. return $filter;
  403. }
  404. protected function buildCompositeInCondition($operator, $columns, $values)
  405. {
  406. throw new NotSupportedException('composite in is not supported by Elasticsearch.');
  407. }
  408. private function buildLikeCondition($operator, $operands)
  409. {
  410. throw new NotSupportedException('like conditions are not supported by Elasticsearch.');
  411. }
  412. }