SuggestParamsBuilder.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\SearchParams;
  22. use OpenSearch\Generated\Search\Config;
  23. use OpenSearch\Generated\Search\Suggest;
  24. class SuggestParamsBuilder {
  25. public function __construct() {}
  26. /**
  27. * 创建一个下拉提示的搜索请求。
  28. *
  29. * @param string $appName 指定应用的名称。
  30. * @param string $suggestName 指定下拉提示的名称。
  31. * @param string $query 指定要搜索的关键词。
  32. * @param int $hits 指定要返回的词条个数。
  33. *
  34. * @return \OpenSearch\Generated\Search\SearchParams
  35. */
  36. public static function build($appName, $suggestName, $query, $hits) {
  37. $config = new Config(array('hits' => (int) $hits, 'appNames' => array($appName)));
  38. $suggest = new Suggest(array('suggestName' => $suggestName));
  39. return new SearchParams(array("config" => $config, 'query' => $query, 'suggest' => $suggest));
  40. }
  41. /**
  42. * 根据SearchParams生成下拉提示搜索的参数。
  43. *
  44. * @param \OpenSearch\Generated\Search\SearchParams $searchParams searchParams
  45. *
  46. * @return array
  47. */
  48. public static function getQueryParams($searchParams) {
  49. $query = $searchParams->query;
  50. $hits = $searchParams->config->hits;
  51. return array('query' => $query, 'hit' => $hits);
  52. }
  53. }