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