ClauseParamsBuilder.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. namespace OpenSearch\Util;
  21. use OpenSearch\Generated\Search\Constant;
  22. use OpenSearch\Generated\Search\Order;
  23. use OpenSearch\Generated\Search\searchFormat;
  24. class ClauseParamsBuilder {
  25. const CONFIG_KEY = 'config';
  26. const QUERY_KEY = 'query';
  27. const SORT_KEY = 'sort';
  28. const DISTINCT_KEY = 'distinct';
  29. const AGGREGATE_KEY = 'aggregate';
  30. const FILTER_KEY = 'filter';
  31. const KVPAIRS = 'kvpairs';
  32. const CLAUSE_SEPARATOR = '&&';
  33. const KV_SEPARATOR = '=';
  34. const CLAUSE_CONFIG_SEPARATOR = ',';
  35. const CLAUSE_CONFIG_KV_SEPARATOR = ':';
  36. const CLAUSE_SORT_SEPARATOR = ';';
  37. const CLAUSE_DISTINCT_KV_SEPARATOR = ':';
  38. const CLAUSE_DISTINCT_SEPARATOR = ';';
  39. const CLAUSE_DISTINCT_SUB_SEPARATOR = ',';
  40. const CLAUSE_AGGREGATE_KV_SEPARATOR = ':';
  41. const CLAUSE_AGGREGATE_SEPARATOR = ';';
  42. const CLAUSE_AGGREGATE_SUB_SEPARATOR = ',';
  43. const CONFIG_CLAUSE_START = 'CONFIG_CLAUSE_START';
  44. const CONFIG_CLAUSE_HIT = 'CONFIG_CLAUSE_HIT';
  45. const CONFIG_CLAUSE_RERANK_SIZE = 'CONFIG_CLAUSE_RERANK_SIZE';
  46. const CONFIG_CLAUSE_FORMAT = 'CONFIG_CLAUSE_FORMAT';
  47. const DISTINCT_CLAUSE_DIST_KEY = 'DISTINCT_CLAUSE_DIST_KEY';
  48. const DISTINCT_CLAUSE_DIST_COUNT = 'DISTINCT_CLAUSE_DIST_COUNT';
  49. const DISTINCT_CLAUSE_DIST_TIMES = 'DISTINCT_CLAUSE_DIST_TIMES';
  50. const DISTINCT_CLAUSE_RESERVED = 'DISTINCT_CLAUSE_RESERVED';
  51. const DISTINCT_CLAUSE_DIST_FILTER = 'DISTINCT_CLAUSE_DIST_FILTER';
  52. const DISTINCT_CLAUSE_UPDATE_TOTAL_HIT = 'DISTINCT_CLAUSE_UPDATE_TOTAL_HIT';
  53. const DISTINCT_CLAUSE_GRADE = 'DISTINCT_CLAUSE_GRADE';
  54. const AGGREGATE_CLAUSE_GROUP_KEY = 'AGGREGATE_CLAUSE_GROUP_KEY';
  55. const AGGREGATE_CLAUSE_AGG_FUN = 'AGGREGATE_CLAUSE_AGG_FUN';
  56. const AGGREGATE_CLAUSE_RANGE = 'AGGREGATE_CLAUSE_RANGE';
  57. const AGGREGATE_CLAUSE_MAX_GROUP = 'AGGREGATE_CLAUSE_MAX_GROUP';
  58. const AGGREGATE_CLAUSE_AGG_FILTER = 'AGGREGATE_CLAUSE_AGG_FILTER';
  59. const AGGREGATE_CLAUSE_AGG_SAMPLER_THRESHOLD = 'AGGREGATE_CLAUSE_AGG_SAMPLER_THRESHOLD';
  60. const AGGREGATE_CLAUSE_AGG_SAMPLER_STEP = 'AGGREGATE_CLAUSE_AGG_SAMPLER_STEP';
  61. private $params;
  62. private $clauses = array();
  63. public function __construct($params) {
  64. $this->params = $params;
  65. }
  66. private function buildConfigClause() {
  67. $config = array();
  68. if (isset($this->params->config->start)) {
  69. $config[] = Constant::get(self::CONFIG_CLAUSE_START) .
  70. self::CLAUSE_CONFIG_KV_SEPARATOR . $this->params->config->start;
  71. }
  72. if (isset($this->params->config->hits)) {
  73. $config[] = Constant::get(self::CONFIG_CLAUSE_HIT) .
  74. self::CLAUSE_CONFIG_KV_SEPARATOR . $this->params->config->hits;
  75. }
  76. if (isset($this->params->config->searchFormat)) {
  77. $format = $this->params->config->searchFormat;
  78. $config[] = Constant::get(self::CONFIG_CLAUSE_FORMAT) .
  79. self::CLAUSE_CONFIG_KV_SEPARATOR . strtolower(searchFormat::$__names[$format]);
  80. }
  81. if (isset($this->params->rank->reRankSize)) {
  82. $config[] = Constant::get(self::CONFIG_CLAUSE_RERANK_SIZE) .
  83. self::CLAUSE_CONFIG_KV_SEPARATOR . $this->params->rank->reRankSize;
  84. }
  85. if (isset($this->params->config->customConfig)) {
  86. foreach ($this->params->config->customConfig as $k => $v) {
  87. $config[] = $k . self::CLAUSE_CONFIG_KV_SEPARATOR . $v;
  88. }
  89. }
  90. $this->clauses[self::CONFIG_KEY] = implode(self::CLAUSE_CONFIG_SEPARATOR, $config);
  91. }
  92. private function buildQueryClause() {
  93. if ($this->params->query !== null) {
  94. $this->clauses[self::QUERY_KEY] = $this->params->query;
  95. }
  96. }
  97. private function buildSortClause() {
  98. $sorts = array();
  99. if (isset($this->params->sort->sortFields)) {
  100. foreach ($this->params->sort->sortFields as $sortField) {
  101. $order = $sortField->order;
  102. $orderString = Order::$__names[$order];
  103. $sorts[] = Constant::get('SORT_CLAUSE_' . $orderString) . $sortField->field;
  104. }
  105. $this->clauses[self::SORT_KEY] = implode(self::CLAUSE_SORT_SEPARATOR, $sorts);
  106. }
  107. }
  108. private function buildFilterClause() {
  109. if (isset($this->params->filter)) {
  110. $this->clauses[self::FILTER_KEY] = $this->params->filter;
  111. }
  112. }
  113. private function buildDistinctClause() {
  114. $distincts = array();
  115. if (isset($this->params->distincts)) {
  116. $keys = array(
  117. 'key' => self::DISTINCT_CLAUSE_DIST_KEY,
  118. 'distCount' => self::DISTINCT_CLAUSE_DIST_COUNT,
  119. 'distTimes' => self::DISTINCT_CLAUSE_DIST_TIMES,
  120. 'reserved' => self::DISTINCT_CLAUSE_RESERVED,
  121. 'distFilter' => self::DISTINCT_CLAUSE_DIST_FILTER,
  122. 'updateTotalHit' => self::DISTINCT_CLAUSE_UPDATE_TOTAL_HIT,
  123. 'grade' => self::DISTINCT_CLAUSE_GRADE
  124. );
  125. foreach ($this->params->distincts as $distinct) {
  126. if (!isset($distinct->key)) {
  127. continue;
  128. }
  129. $dist = array();
  130. foreach ($keys as $k => $v) {
  131. if ($distinct->$k) {
  132. $dist[] = Constant::get($v) . self::CLAUSE_AGGREGATE_KV_SEPARATOR . $distinct->$k;
  133. }
  134. }
  135. $distincts[] = implode(self::CLAUSE_DISTINCT_SUB_SEPARATOR, $dist);
  136. }
  137. $this->clauses[self::DISTINCT_KEY] = implode(self::CLAUSE_DISTINCT_SEPARATOR, $distincts);
  138. }
  139. }
  140. private function buildAggregateClause() {
  141. $aggregates = array();
  142. if (isset($this->params->aggregates)) {
  143. $keys = array(
  144. 'groupKey' => self::AGGREGATE_CLAUSE_GROUP_KEY,
  145. 'aggFun' => self::AGGREGATE_CLAUSE_AGG_FUN,
  146. 'range' => self::AGGREGATE_CLAUSE_RANGE,
  147. 'maxGroup' => self::AGGREGATE_CLAUSE_MAX_GROUP,
  148. 'aggFilter' => self::AGGREGATE_CLAUSE_AGG_FILTER,
  149. 'aggSamplerThresHold' => self::AGGREGATE_CLAUSE_AGG_SAMPLER_THRESHOLD,
  150. 'aggSamplerStep' => self::AGGREGATE_CLAUSE_AGG_SAMPLER_STEP
  151. );
  152. foreach ($this->params->aggregates as $aggregate) {
  153. if (!isset($aggregate->groupKey) || !isset($aggregate->aggFun)) {
  154. continue;
  155. }
  156. $agg = array();
  157. foreach ($keys as $k => $v) {
  158. if (isset($aggregate->$k)) {
  159. $agg[] = Constant::get($v) . self::CLAUSE_AGGREGATE_KV_SEPARATOR . $aggregate->$k;
  160. }
  161. }
  162. $aggregates[] = implode(self::CLAUSE_AGGREGATE_SUB_SEPARATOR, $agg);
  163. }
  164. $this->clauses[self::AGGREGATE_KEY] = implode(self::CLAUSE_AGGREGATE_SEPARATOR, $aggregates);
  165. }
  166. }
  167. private function buildKVPairsClause() {
  168. if (isset($this->params->config->kvpairs)) {
  169. $this->clauses[self::KVPAIRS] = $this->params->config->kvpairs;
  170. }
  171. }
  172. public function getClausesString() {
  173. $this->buildConfigClause();
  174. $this->buildQueryClause();
  175. $this->buildSortClause();
  176. $this->buildFilterClause();
  177. $this->buildDistinctClause();
  178. $this->buildAggregateClause();
  179. $this->buildKVPairsClause();
  180. $clauses = array();
  181. foreach ($this->clauses as $clauseKey => $value) {
  182. $clauses[] = $clauseKey . self::KV_SEPARATOR . $value;
  183. }
  184. return implode(self::CLAUSE_SEPARATOR, $clauses);
  185. }
  186. }