SearchParamsBuilder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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\Aggregate;
  22. use OpenSearch\Generated\Search\Distinct;
  23. use OpenSearch\Generated\Search\Config;
  24. use OpenSearch\Generated\Search\Constant;
  25. use OpenSearch\Generated\Search\Order;
  26. use OpenSearch\Generated\Search\Rank;
  27. use OpenSearch\Generated\Search\SearchFormat;
  28. use OpenSearch\Generated\Search\SearchParams;
  29. use OpenSearch\Generated\Search\Sort;
  30. use OpenSearch\Generated\Search\SortField;
  31. use OpenSearch\Generated\Search\Summary;
  32. use OpenSearch\Generated\Search\DeepPaging;
  33. use OpenSearch\Generated\Search\Abtest;
  34. /**
  35. * 搜索配置项。
  36. */
  37. class SearchParamsBuilder {
  38. const SORT_INCREASE = 1;
  39. const SORT_DECREASE = 0;
  40. private $searchParams;
  41. public function __construct($opts = array()) {
  42. $config = new Config();
  43. $this->searchParams = new SearchParams(array('config' => $config));
  44. if (isset($opts['start'])) {
  45. $this->setStart($opts['start']);
  46. }
  47. if (isset($opts['hits'])) {
  48. $this->setHits($opts['hits']);
  49. }
  50. if (isset($opts['format'])) {
  51. $this->setFormat($opts['format']);
  52. }
  53. if (isset($opts['appName'])) {
  54. $this->setAppName($opts['appName']);
  55. }
  56. if (isset($opts['query'])) {
  57. $this->setQuery($opts['query']);
  58. }
  59. if (isset($opts['kvpairs'])) {
  60. $this->setKvPairs($opts['kvpairs']);
  61. }
  62. if (isset($opts['fetchFields'])) {
  63. $this->setFetchFields($opts['fetchFields']);
  64. }
  65. if (isset($opts['routeValue'])) {
  66. $this->setRouteValue($opts['routeValue']);
  67. }
  68. if (isset($opts['customConfig']) && is_array($opts['customConfig'])) {
  69. foreach ($opts['customConfig'] as $k => $v) {
  70. $this->setCustomConfig($k, $v);
  71. }
  72. }
  73. if (isset($opts['filter'])) {
  74. $this->setFilter($opts['filter']);
  75. }
  76. if (isset($opts['sort']) && is_array($opts['sort'])) {
  77. foreach ($opts['sort'] as $sort) {
  78. if (!isset($sort['order'])) {
  79. $sort['order'] = SELF::SORT_DECREASE;
  80. }
  81. $this->addSort($sort['field'], $sort['order']);
  82. }
  83. }
  84. if (isset($opts['firstRankName'])) {
  85. $this->setFirstRankName($opts['firstRankName']);
  86. }
  87. if (isset($opts['secondRankName'])) {
  88. $this->setSecondRankName($opts['secondRankName']);
  89. }
  90. if (isset($opts['aggregate']) && isset($opts['aggregate']['groupKey'])) {
  91. $this->addAggregate($opts['aggregate']);
  92. } else if (isset($opts['aggregate']) && isset($opts['aggregate'][0])) {
  93. foreach ($opts['aggregate'] as $aggregate) {
  94. $this->addAggregate($aggregate);
  95. }
  96. }
  97. if (isset($opts['distinct']) && isset($opts['distinct'][0])) {
  98. foreach ($opts['distinct'] as $distinct) {
  99. $this->addDistinct($distinct);
  100. }
  101. } else if (isset($opts['distinct']) && isset($opts['distinct']['key'])) {
  102. $this->addDistinct($opts['distinct']);
  103. }
  104. if (isset($opts['summaries'])) {
  105. foreach ($opts['summaries'] as $summary) {
  106. $this->addSummary($summary);
  107. }
  108. }
  109. if (isset($opts['qp'])) {
  110. if (!is_array($opts['qp'])) {
  111. $opts['qp'] = array($opts['qp']);
  112. }
  113. foreach ($opts['qp'] as $qp) {
  114. $this->addQueryProcessor($qp);
  115. }
  116. }
  117. if (isset($opts['disableFunctions']) && is_array($opts['disableFunctions'])) {
  118. foreach ($opts['disableFunctions'] as $fun) {
  119. $this->addDisableFunctions($fun);
  120. }
  121. } else if (isset($opts['disableFunctions'])) {
  122. $this->addDisableFunctions($opts['disableFunctions']);
  123. }
  124. if (isset($opts['customParams'])) {
  125. foreach ($opts['customParams'] as $key => $value) {
  126. $this->setCustomParam($key, $value);
  127. }
  128. }
  129. if (isset($opts['reRankSize'])) {
  130. $this->setReRankSize($opts['reRankSize']);
  131. }
  132. }
  133. /**
  134. * 设置返回结果的偏移量。
  135. *
  136. * @param int $start 偏移量,范围[0,5000]。
  137. * @return void
  138. */
  139. public function setStart($start) {
  140. $this->searchParams->config->start = (int) $start;
  141. }
  142. /**
  143. * 设置返回结果的条数。
  144. *
  145. * @param int $hits 返回结果的条数,范围[0,500]。
  146. * @return void
  147. */
  148. public function setHits($hits) {
  149. $this->searchParams->config->hits = $hits;
  150. }
  151. /**
  152. * 设置返回结果的格式。
  153. *
  154. * @param String $format 返回结果的格式,有json、fulljson和xml格式。
  155. * @return void
  156. */
  157. public function setFormat($format) {
  158. $upperFormat = strtoupper($format);
  159. $this->searchParams->config->searchFormat = array_search($upperFormat, SearchFormat::$__names);
  160. }
  161. /**
  162. * 设置要搜索的应用名称或ID。
  163. *
  164. * @param String $appName 指定要搜索的应用名称或ID。
  165. * @return void
  166. */
  167. public function setAppName($appNames) {
  168. $this->searchParams->config->appNames = is_array($appNames) ? $appNames : array($appNames);
  169. }
  170. /**
  171. * 设置搜索关键词。
  172. *
  173. * @param String $query 设置的搜索关键词,格式为:索引名:'关键词' [AND|OR ...]
  174. * @return void
  175. */
  176. public function setQuery($query) {
  177. $this->searchParams->query = $query;
  178. }
  179. /**
  180. * 设置KVpairs。
  181. *
  182. * @param String $kvPairs 设置kvpairs。
  183. * @return void
  184. */
  185. public function setKvPairs($kvPairs) {
  186. $this->searchParams->config->kvpairs = $kvPairs;
  187. }
  188. /**
  189. * 设置结果集的返回字段。
  190. *
  191. * @param array $fetchFields 指定的返回字段的列表,例如array('a', 'b')
  192. * @return void
  193. */
  194. public function setFetchFields($fetchFields) {
  195. $this->searchParams->config->fetchFields = $fetchFields;
  196. }
  197. /**
  198. * 如果分组查询时,指定分组的值。
  199. *
  200. * @param Mixed $routeValue 分组字段值。
  201. * @return void
  202. */
  203. public function setRouteValue($routeValue) {
  204. $this->searchParams->config->routeValue = $routeValue;
  205. }
  206. /**
  207. * 设置参与精排个数。
  208. *
  209. * @param int $reRankSize 参与精排个数,范围[0,2000]。
  210. * @return void
  211. */
  212. public function setReRankSize($reRankSize) {
  213. $this->searchParams->rank->reRankSize = $reRankSize;
  214. }
  215. /**
  216. * 在Config字句中增加自定义的参数。
  217. *
  218. * @param String $key 设定自定义参数名。
  219. * @param Mixed $value 设定自定义参数值。
  220. * @return void
  221. */
  222. public function setCustomConfig($key, $value) {
  223. if ($this->searchParams->config->customConfig == null) {
  224. $this->searchParams->config->customConfig = array();
  225. }
  226. $this->searchParams->config->customConfig[$key] = $value;
  227. }
  228. /**
  229. * 添加过滤条件。
  230. *
  231. * @param String $filter 过滤,例如a>1。
  232. * @param String $condition 两个过滤条件的连接符, 例如AND OR等。
  233. * @return void
  234. */
  235. public function addFilter($filter, $condition = 'AND') {
  236. if ($this->searchParams->filter == null) {
  237. $this->searchParams->filter = $filter;
  238. } else {
  239. $this->searchParams->filter .= " {$condition} $filter";
  240. }
  241. }
  242. /**
  243. * 设置过滤条件。
  244. *
  245. * @param String $filterSting 过滤,例如a>1 OR b<2。
  246. * @return void
  247. */
  248. public function setFilter($filterString) {
  249. $this->searchParams->filter = $filterString;
  250. }
  251. /**
  252. * 添加排序规则。
  253. *
  254. * @param String $field 排序字段。
  255. * @param int $sort 排序策略,有降序0或者升序1,默认降序。
  256. * @return void
  257. */
  258. public function addSort($field, $order = self::SORT_DECREASE) {
  259. if ($this->searchParams->sort == null) {
  260. $this->searchParams->sort = new Sort();
  261. $this->searchParams->sort->sortFields = array();
  262. }
  263. $sortField = new SortField(array('field' => $field, 'order' => $order));
  264. $this->searchParams->sort->sortFields[] = $sortField;
  265. }
  266. /**
  267. * 设置粗排表达式名称。
  268. *
  269. * @param String $firstRankName 指定的粗排表达式名称。
  270. * @return void
  271. */
  272. public function setFirstRankName($firstRankName) {
  273. $this->searchParams->rank->firstRankName = $firstRankName;
  274. }
  275. /**
  276. * 设置精排表达式名称。
  277. *
  278. * @param String $secondRankName 指定的精排表达式名称。
  279. * @return void
  280. */
  281. public function setSecondRankName($secondRankName) {
  282. $this->searchParams->rank->secondRankName = $secondRankName;
  283. }
  284. /**
  285. * 设置聚合配置。
  286. *
  287. * @param array $agg 指定的聚合配置。
  288. * @return void
  289. */
  290. public function addAggregate($agg) {
  291. $aggregate = new Aggregate($agg);
  292. if ($this->searchParams->aggregates == null) {
  293. $this->searchParams->aggregates = array();
  294. }
  295. $this->searchParams->aggregates[] = $aggregate;
  296. }
  297. /**
  298. * 设置去重配置。
  299. *
  300. * @param array $dist 指定的去重配置。
  301. * @return void
  302. */
  303. public function addDistinct($dist) {
  304. $distinct = new Distinct($dist);
  305. if ($this->searchParams->distincts == null) {
  306. $this->searchParams->distincts = array();
  307. }
  308. $this->searchParams->distincts[] = $distinct;
  309. }
  310. /**
  311. * 设置搜索结果摘要配置。
  312. *
  313. * @param array $summaryMeta 指定的摘要字段配置。
  314. * @return void
  315. */
  316. public function addSummary($summaryMeta) {
  317. $summary = new Summary($summaryMeta);
  318. if ($this->searchParams->summaries == null) {
  319. $this->searchParams->summaries = array();
  320. }
  321. $this->searchParams->summaries[] = $summary;
  322. }
  323. /**
  324. * 添加查询分析配置。
  325. *
  326. * @param array $qpName 指定的QP名称。
  327. * @return void
  328. */
  329. public function addQueryProcessor($qpName) {
  330. if ($this->searchParams->queryProcessorNames == null) {
  331. $this->searchParams->queryProcessorNames = array();
  332. }
  333. $this->searchParams->queryProcessorNames[] = $qpName;
  334. }
  335. /**
  336. * 添加要关闭的function。
  337. *
  338. * @param String $disabledFunction 指定的要关闭的方法名称。
  339. * @return void
  340. */
  341. public function addDisableFunctions($disabledFunction) {
  342. if ($this->searchParams->disableFunctions == null) {
  343. $this->searchParams->disableFunctions = array();
  344. }
  345. $this->searchParams->disableFunctions[] = $disabledFunction;
  346. }
  347. /**
  348. * 设置自定义参数。
  349. *
  350. * @param String $key 自定义参数的参数名。
  351. * @param String $value 自定义参数的参数值。
  352. * @return void
  353. */
  354. public function setCustomParam($key, $value) {
  355. if ($this->searchParams->customParam == null) {
  356. $this->searchParams->customParam = array();
  357. }
  358. $this->searchParams->customParam[$key] = $value;
  359. }
  360. /**
  361. * 设置扫描数据的过期时间。
  362. *
  363. * @param String $expireTime 设定scroll的过期时间。
  364. * @return void
  365. */
  366. public function setScrollExpire($expiredTime) {
  367. if ($this->searchParams->deepPaging == null) {
  368. $this->searchParams->deepPaging = new DeepPaging();
  369. }
  370. $this->searchParams->deepPaging->scrollExpire = $expiredTime;
  371. }
  372. /**
  373. * 设置扫描数据的scrollId。
  374. *
  375. * ScrollId 为上一次扫描时返回的信息。
  376. *
  377. * @param String $scrollId 设定scroll的scrollId。
  378. * @return void
  379. */
  380. public function setScrollId($scrollId) {
  381. if ($this->searchParams->deepPaging == null) {
  382. $this->searchParams->deepPaging = new DeepPaging();
  383. }
  384. $this->searchParams->deepPaging->scrollId = $scrollId;
  385. }
  386. /**
  387. * 设置abtest数据的sceneTag。
  388. *
  389. * SceneTag 为场景标签。
  390. *
  391. * @param String $sceneTag 设定abtest的sceneTag。
  392. * @return void
  393. */
  394. public function setSceneTag($sceneTag) {
  395. if ($this->searchParams->abtest == null) {
  396. $this->searchParams->abtest = new Abtest();
  397. }
  398. $this->searchParams->abtest->sceneTag = $sceneTag;
  399. }
  400. /**
  401. * 设置abtest数据的flowDivider。
  402. *
  403. * FlowDivider 为流量分配标识。
  404. *
  405. * @param String $flowDivider 设定abtest的flowDivider。
  406. * @return void
  407. */
  408. public function setFlowDivider($flowDivider) {
  409. if ($this->searchParams->abtest == null) {
  410. $this->searchParams->abtest = new Abtest();
  411. }
  412. $this->searchParams->abtest->flowDivider = $flowDivider;
  413. }
  414. /**
  415. * 设置终端用户的id,用来统计uv信息。
  416. *
  417. * @param String $userId 设定终端用户的id。
  418. * @return void
  419. */
  420. public function setUserId($userId) {
  421. $this->searchParams->userId = $userId;
  422. }
  423. /**
  424. * 设置终端用户输入的query。
  425. *
  426. * @param String $rawQuery 设定终端用户输入的query。
  427. * @return void
  428. */
  429. public function setRawQuery($rawQuery) {
  430. $this->searchParams->rawQuery = $rawQuery;
  431. }
  432. /**
  433. * 获取SearchParams对象。
  434. *
  435. * @return SearchParams
  436. */
  437. public function build() {
  438. return $this->searchParams;
  439. }
  440. }