UrlParamsBuilder.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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\DeepPaging;
  23. use OpenSearch\Generated\Search\SearchType;
  24. class UrlParamsBuilder {
  25. const QUERY = 'query';
  26. const FORMAT = 'format';
  27. const FIRST_RANK_NAME = 'first_rank_name';
  28. const SECOND_RANK_NAME = 'second_rank_name';
  29. const SUMMARY = 'summary';
  30. const FETCH_FIELDS = 'fetch_fields';
  31. const QP = 'qp';
  32. const DISABLE = 'disable';
  33. const ROUTE_VALUE = 'route_value';
  34. const SCROLL_EXPIRE = 'scroll';
  35. const SCROLL_ID = 'scroll_id';
  36. const SEARCH_TYPE = 'search_type';
  37. const FETCH_FIELDS_SEPARATOR = ';';
  38. const QP_SEPARATOR = ',';
  39. const DISABLE_FUNCTIONS_SEPARATOR = ';';
  40. const SUMMARY_SEPARATOR = ';';
  41. const SUMMARY_SUB_SEPARATOR = ',';
  42. const SUMMARY_KV_SEPARATOR = ':';
  43. const SEARCH_TYPE_SCAN = 'scan';
  44. const ABTEST = "abtest";
  45. private static $summaryKeys = array(
  46. 'summary_field' => 'SUMMARY_PARAM_SUMMARY_FIELD',
  47. 'summary_len' => 'SUMMARY_PARAM_SUMMARY_LEN',
  48. 'summary_ellipsis' => 'SUMMARY_PARAM_SUMMARY_ELLIPSIS',
  49. 'summary_snippet' => 'SUMMARY_PARAM_SUMMARY_SNIPPET',
  50. 'summary_element' => 'SUMMARY_PARAM_SUMMARY_ELEMENT',
  51. 'summary_element_prefix' => 'SUMMARY_PARAM_SUMMARY_ELEMENT_PREFIX',
  52. 'summary_element_postfix' => 'SUMMARY_PARAM_SUMMARY_ELEMENT_POSTFIX'
  53. );
  54. private $params = array();
  55. public function __construct($searchParams) {
  56. $this->init($searchParams);
  57. }
  58. public function init($searchParams) {
  59. $this->initQuery($searchParams);
  60. $this->initScroll($searchParams);
  61. $this->initRank($searchParams);
  62. $this->initFetchFields($searchParams);
  63. $this->initSummary($searchParams);
  64. $this->initQueryProcessor($searchParams);
  65. $this->initDisableFunctions($searchParams);
  66. $this->initRouteValue($searchParams);
  67. $this->initCustomParams($searchParams);
  68. $this->initAbtest($searchParams);
  69. $this->initUserId($searchParams);
  70. $this->initRawQuery($searchParams);
  71. }
  72. public function initScroll($searchParams) {
  73. if (isset($searchParams->deepPaging) && $searchParams->deepPaging instanceof DeepPaging) {
  74. if ($searchParams->deepPaging->scrollId) {
  75. $this->params[self::SCROLL_ID] = $searchParams->deepPaging->scrollId;
  76. } else {
  77. $this->params[self::SEARCH_TYPE] = self::SEARCH_TYPE_SCAN;
  78. }
  79. $this->params[self::SCROLL_EXPIRE] = $searchParams->deepPaging->scrollExpire;
  80. }
  81. }
  82. public function initQuery($searchParams) {
  83. $builder = new ClauseParamsBuilder($searchParams);
  84. $this->params[self::QUERY] = $builder->getClausesString();
  85. }
  86. public function initRank($searchParams) {
  87. if (isset($searchParams->rank->firstRankName)) {
  88. $this->params[self::FIRST_RANK_NAME] = $searchParams->rank->firstRankName;
  89. }
  90. if (isset($searchParams->rank->secondRankName)) {
  91. $this->params[self::SECOND_RANK_NAME] = $searchParams->rank->secondRankName;
  92. }
  93. }
  94. public function initFetchFields($searchParams) {
  95. if (isset($searchParams->config->fetchFields)) {
  96. $this->params[self::FETCH_FIELDS] = implode(self::FETCH_FIELDS_SEPARATOR, $searchParams->config->fetchFields);
  97. }
  98. }
  99. public function initSummary($searchParams) {
  100. if (isset($searchParams->summaries)) {
  101. $summaries = array();
  102. foreach ($searchParams->summaries as $summary) {
  103. if (!isset($summary->summary_field)) {
  104. continue;
  105. }
  106. $sum = array();
  107. foreach (self::$summaryKeys as $k => $v) {
  108. if (isset($summary->$k)) {
  109. $sum[] = Constant::get($v) . self::SUMMARY_KV_SEPARATOR . $summary->$k;
  110. }
  111. }
  112. $summaries[] = implode(self::SUMMARY_SUB_SEPARATOR, $sum);
  113. }
  114. $this->params[self::SUMMARY] = implode(self::SUMMARY_SEPARATOR, $summaries);
  115. }
  116. }
  117. public function initQueryProcessor($searchParams) {
  118. if (isset($searchParams->queryProcessorNames)) {
  119. $this->params[self::QP] = implode(self::QP_SEPARATOR, $searchParams->queryProcessorNames);
  120. }
  121. }
  122. public function initDisableFunctions($searchParams) {
  123. if (isset($searchParams->disableFunctions)) {
  124. $this->params[self::DISABLE] = implode(self::DISABLE_FUNCTIONS_SEPARATOR, $searchParams->disableFunctions);
  125. }
  126. }
  127. public function initRouteValue($searchParams) {
  128. if (isset($searchParams->config->routeValue)) {
  129. $this->params[self::ROUTE_VALUE] = $searchParams->config->routeValue;
  130. }
  131. }
  132. public function initCustomParams($searchParams) {
  133. if (isset($searchParams->customParam)) {
  134. $this->params = array_merge($this->params, $searchParams->customParam);
  135. }
  136. }
  137. public function initAbtest($searchParams) {
  138. if (isset($searchParams->abtest) && isset($searchParams->abtest->sceneTag) && isset($searchParams->abtest->flowDivider)) {
  139. $this->params[self::ABTEST] = sprintf("%s=%s,%s=%s", Constant::get('ABTEST_PARAM_SCENE_TAG'), $searchParams->abtest->sceneTag, Constant::get('ABTEST_PARAM_FLOW_DIVIDER'), $searchParams->abtest->flowDivider);
  140. }
  141. }
  142. public function initUserId($searchParams) {
  143. if (isset($searchParams->userId)) {
  144. $this->params[Constant::get('USER_ID')] = $searchParams->userId;
  145. }
  146. }
  147. public function initRawQuery($searchParams) {
  148. if (isset($searchParams->rawQuery)) {
  149. $this->params[Constant::get('RAW_QUERY')] = $searchParams->rawQuery;
  150. }
  151. }
  152. public function getHttpParams() {
  153. return $this->params;
  154. }
  155. }