SuggestClient.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Client;
  21. use OpenSearch\Generated\Search\OpenSearchSearcherServiceIf;
  22. use OpenSearch\Generated\Search\SearchParams;
  23. use OpenSearch\Util\SuggestParamsBuilder;
  24. /**
  25. * 应用下拉提示操作类。
  26. *
  27. * 通过制定关键词、过滤条件搜索应用的下拉提示的结果。
  28. *
  29. */
  30. class SuggestClient implements OpenSearchSearcherServiceIf {
  31. const SUGGEST_API_PATH = '/apps/%s/suggest/%s/search';
  32. private $openSearchClient;
  33. /**
  34. * 构造方法。
  35. *
  36. * @param \OpenSearch\Client\OpenSearchClient $openSearchClient 基础类,负责计算签名,和服务端进行交互和返回结果。
  37. * @return void
  38. */
  39. public function __construct($openSearchClient) {
  40. $this->openSearchClient = $openSearchClient;
  41. }
  42. /**
  43. * 执行搜索操作。
  44. *
  45. * @param \OpenSearch\Generated\Search\SearchParams $searchParams 制定的搜索条件。
  46. * @return \OpenSearch\Generated\Common\OpenSearchResult OpenSearchResult类
  47. */
  48. public function execute(SearchParams $searchParams) {
  49. $path = self::getPath($searchParams);
  50. $params = SuggestParamsBuilder::getQueryParams($searchParams);
  51. return $this->openSearchClient->get($path, $params);
  52. }
  53. private static function getPath($searchParams) {
  54. $appName = implode(',', $searchParams->config->appNames);
  55. $suggestName = $searchParams->suggest->suggestName;
  56. return sprintf(self::SUGGEST_API_PATH, $appName, $suggestName);
  57. }
  58. }