|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PDPhilip\Elasticsearch\DSL; |
| 4 | + |
| 5 | + |
| 6 | +use ONGR\ElasticsearchDSL\Query\FullText\QueryStringQuery; |
| 7 | +use ONGR\ElasticsearchDSL\Query\MatchAllQuery; |
| 8 | +use ONGR\ElasticsearchDSL\Search; |
| 9 | +use ONGR\ElasticsearchDSL\Sort\FieldSort; |
| 10 | +use Exception; |
| 11 | + |
| 12 | +trait QueryBuilder |
| 13 | +{ |
| 14 | + |
| 15 | + protected static $bucketOperators = ['and', 'or']; |
| 16 | + |
| 17 | + protected static $equivalenceOperators = ['in', 'nin']; |
| 18 | + |
| 19 | + protected static $clauseOperators = ['ne', 'gt', 'gte', 'lt', 'lte', 'between', 'not_between', 'like', 'not_like', 'exists']; |
| 20 | + |
| 21 | + public static function buildParams($index, $wheres, $options = [], $columns = [], $_id = null) |
| 22 | + { |
| 23 | + if ($index){ |
| 24 | + $params = [ |
| 25 | + 'index' => $index |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + if ($_id) { |
| 30 | + $params['id'] = $_id; |
| 31 | + } |
| 32 | + |
| 33 | + $params['body'] = self::_buildQuery($wheres); |
| 34 | + if ($columns && $columns != '*') { |
| 35 | + $params['body']['_source'] = $columns; |
| 36 | + } |
| 37 | + |
| 38 | + $opts = self::_buildOptions($options); |
| 39 | + if ($opts) { |
| 40 | + foreach ($opts as $key => $value) { |
| 41 | + if (isset($params[$key])) { |
| 42 | + $params[$key] = array_merge($params[$key], $opts[$key]); |
| 43 | + } else { |
| 44 | + $params[$key] = $value; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return $params; |
| 50 | + } |
| 51 | + |
| 52 | + private static function _buildQueryString($wheres): string |
| 53 | + { |
| 54 | + if ($wheres) { |
| 55 | + foreach ($wheres as $key => $value) { |
| 56 | + return self::_parseParams($key, $value); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return ''; |
| 61 | + } |
| 62 | + |
| 63 | + private static function _andQueryString($values): string |
| 64 | + { |
| 65 | + $strings = []; |
| 66 | + foreach ($values as $key => $val) { |
| 67 | + $strings[] = self::_parseParams($key, $val); |
| 68 | + } |
| 69 | + |
| 70 | + return '('.implode(' AND ', $strings).')'; |
| 71 | + } |
| 72 | + |
| 73 | + private static function _orQueryString($values): string |
| 74 | + { |
| 75 | + $strings = []; |
| 76 | + foreach ($values as $key => $val) { |
| 77 | + $strings[] = self::_parseParams($key, $val); |
| 78 | + } |
| 79 | + |
| 80 | + return '('.implode(' OR ', $strings).')'; |
| 81 | + } |
| 82 | + |
| 83 | + private static function _inQueryString($key, $values): string |
| 84 | + { |
| 85 | + $strings = []; |
| 86 | + foreach ($values as $val) { |
| 87 | + $strings[] = self::_parseParams(null, $val); |
| 88 | + } |
| 89 | + |
| 90 | + return '('.$key.':('.implode(' OR ', $strings).'))'; |
| 91 | + } |
| 92 | + |
| 93 | + private static function _ninQueryString($key, $values): string |
| 94 | + { |
| 95 | + $strings = []; |
| 96 | + foreach ($values as $val) { |
| 97 | + $strings[] = self::_parseParams(null, $val); |
| 98 | + } |
| 99 | + |
| 100 | + return '(NOT '.$key.':('.implode(' OR ', $strings).'))'; |
| 101 | + } |
| 102 | + |
| 103 | + private static function _parseParams($key, $value): string |
| 104 | + { |
| 105 | + |
| 106 | + if ($key == 'and' || $key == 'or') { |
| 107 | + return self::{'_'.$key.'QueryString'}($value); |
| 108 | + } |
| 109 | + if (is_array($value)) { |
| 110 | + |
| 111 | + foreach ($value as $op => $opVal) { |
| 112 | + |
| 113 | + if (in_array($op, self::$bucketOperators)) { |
| 114 | + return self::{'_'.$op.'QueryString'}($opVal); |
| 115 | + } |
| 116 | + if (in_array($op, self::$equivalenceOperators)) { |
| 117 | + return self::{'_'.$op.'QueryString'}($key, $opVal); |
| 118 | + } |
| 119 | + if (in_array($op, self::$clauseOperators)) { |
| 120 | + switch ($op) { |
| 121 | + case 'ne': |
| 122 | + if (!$opVal) { |
| 123 | + // Is not equal to null => exists and has a value |
| 124 | + return '(_exists_:'.$key.')'; |
| 125 | + } |
| 126 | + |
| 127 | + return '(NOT '.$key.':'.self::_escape($opVal).')'; |
| 128 | + case 'lt': |
| 129 | + return '('.$key.':{* TO '.$opVal.'})'; |
| 130 | + case 'lte': |
| 131 | + return '('.$key.':[* TO '.$opVal.'])'; |
| 132 | + case 'gt': |
| 133 | + return '('.$key.':{'.$opVal.' TO *})'; |
| 134 | + case 'gte': |
| 135 | + return '('.$key.':['.$opVal.' TO *])'; |
| 136 | + case 'between': |
| 137 | + return '('.$key.':['.$opVal[0].' TO '.$opVal[1].'])'; |
| 138 | + case 'not_between': |
| 139 | + return '(NOT '.$key.':['.$opVal[0].' TO '.$opVal[1].'])'; |
| 140 | + case 'like': |
| 141 | + return '('.$key.':*'.self::_escape($opVal).'*)'; |
| 142 | + case 'not_like': |
| 143 | + return '(NOT '.$key.':*'.self::_escape($opVal).'*)'; |
| 144 | + case 'exists': |
| 145 | + if ($opVal) { |
| 146 | + return '(_exists_:'.$key.')'; |
| 147 | + } |
| 148 | + |
| 149 | + return '(NOT _exists_:'.$key.')'; |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + return self::_parseParams($op, $opVal); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if (!$key) { |
| 160 | + return self::_escape($value); |
| 161 | + } |
| 162 | + if ($value === true) { |
| 163 | + return '('.$key.':true)'; |
| 164 | + } |
| 165 | + if ($value === false) { |
| 166 | + return '('.$key.':false)'; |
| 167 | + } |
| 168 | + if ($value === null) { |
| 169 | + return '(NOT _exists_:'.$key.')'; |
| 170 | + } |
| 171 | + |
| 172 | + return '('.$key.':"'.self::_escape($value).'")'; |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + private static function _escape($string): string |
| 177 | + { |
| 178 | + //+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ / |
| 179 | + $stripped = preg_replace('/\W/', '\\\\$0', $string); |
| 180 | + |
| 181 | + //Put the spaces back; |
| 182 | + $stripped = str_replace('\ ', ' ', $stripped); |
| 183 | + //Edge cases |
| 184 | + $stripped = str_replace('\&\&', '\&&', $stripped); |
| 185 | + $stripped = str_replace('\|\|', '\||', $stripped); |
| 186 | + |
| 187 | + return $stripped; |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + private static function _buildQuery($wheres): array |
| 192 | + { |
| 193 | + $search = new Search(); |
| 194 | + if (!$wheres) { |
| 195 | + $search->addQuery(new MatchAllQuery()); |
| 196 | + $search->toArray(); |
| 197 | + |
| 198 | + return $search->toArray(); |
| 199 | + } |
| 200 | + $string = self::_buildQueryString($wheres); |
| 201 | + $queryStringQuery = new QueryStringQuery($string); |
| 202 | + $search->addQuery($queryStringQuery); |
| 203 | + |
| 204 | + return $search->toArray(); |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + private static function _buildOptions($options): array |
| 209 | + { |
| 210 | + |
| 211 | + $return = []; |
| 212 | + if ($options) { |
| 213 | + foreach ($options as $key => $value) { |
| 214 | + switch ($key) { |
| 215 | + case 'limit': |
| 216 | + $return['size'] = $value; |
| 217 | + break; |
| 218 | + case 'sort': |
| 219 | + if (!isset($return['body']['sort'])) { |
| 220 | + $return['body']['sort'] = []; |
| 221 | + } |
| 222 | + $sortBy = self::_parseSortOrder($value); |
| 223 | + $return['body']['sort'][] = $sortBy; |
| 224 | + break; |
| 225 | + case 'skip': |
| 226 | + $return['from'] = $value; |
| 227 | + break; |
| 228 | + case 'multiple': |
| 229 | + break; |
| 230 | + default: |
| 231 | + throw new Exception('Unexpected option: '.$key); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + return $return; |
| 237 | + } |
| 238 | + |
| 239 | + private static function _parseSortOrder($value): array |
| 240 | + { |
| 241 | + $field = array_key_first($value); |
| 242 | + $direction = $value[$field]; |
| 243 | + |
| 244 | + $dir = 'desc'; |
| 245 | + if ($direction == 1) { |
| 246 | + $dir = 'asc'; |
| 247 | + } |
| 248 | + $sort = new FieldSort($field, $dir); |
| 249 | + |
| 250 | + return $sort->toArray(); |
| 251 | + } |
| 252 | +} |
0 commit comments