BehaviorCollectionClient.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\BehaviorCollection\Command;
  22. use OpenSearch\Generated\BehaviorCollection\Constant;
  23. use OpenSearch\Generated\BehaviorCollection\BehaviorCollectionServiceIf;
  24. use OpenSearch\Client\OpenSearchClient;
  25. /**
  26. * 搜索行为数据文档推送类。
  27. *
  28. * 管理搜索应用的行为数据推送,包含单条推送文档、批量推送文档等。
  29. *
  30. */
  31. class BehaviorCollectionClient implements BehaviorCollectionServiceIf {
  32. private $openSearchClient;
  33. private $recordBuffer = array();
  34. const SEARCH_DOC_CLICK_EVENT_ID = 2001;
  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. * > Note:
  48. * >
  49. * > 这条文档只是增加到sdk client buffer中,没有正式提交到服务端;只有调用了commit方法才会被提交到服务端。
  50. * > 你可以多次addSearchDocClickRecord然后调用commit() 统一提交。
  51. *
  52. * @param string $searchDocListPage 搜索结果列表所在的页面名称
  53. * @param string $docDetailPage 某个搜索文档被点击后,搜索文档的详情页面名称
  54. * @param int $detailPageStayTime 用户在详情页停留的时长(单位为ms)
  55. * @param string $objectId 被点击的文档的主键,不能为空
  56. * @param string $opsRequestMisc opensearch返回的查询结果中的ops_request_misc字段
  57. * @param string $basicFields 其他基础字段, 非必需字段
  58. * @return \OpenSearch\Generated\Common\OpenSearchResult
  59. */
  60. public function addSearchDocClickRecord(
  61. $searchDocListPage,
  62. $docDetailPage,
  63. $detailPageStayTime,
  64. $objectId,
  65. $opsRequestMisc,
  66. array $basicFields = []) {
  67. $jsonFields = [
  68. 'event_id' => self::SEARCH_DOC_CLICK_EVENT_ID,
  69. 'sdk_type' => OpenSearchClient::SDK_TYPE,
  70. 'sdk_version' => OpenSearchClient::SDK_VERSION,
  71. 'page' => $docDetailPage,
  72. 'arg1' => $searchDocListPage,
  73. 'arg2' => "",
  74. 'arg3' => $detailPageStayTime,
  75. 'args' => self::createSearchDocClickArgs($objectId, $opsRequestMisc),
  76. ];
  77. if (!empty($basicFields)) {
  78. foreach ($basicFields as $key => $value) {
  79. $jsonFields[$key] = $value;
  80. }
  81. }
  82. $this->addOneRecord($jsonFields, Command::$__names[Command::ADD]);
  83. }
  84. /**
  85. * 把sdk client buffer中的文档发布到服务端。
  86. *
  87. * > Note:
  88. * >
  89. * > 在发送之前会把buffer中的文档清空,所以如果服务端返回错误需要重试的情况下,需要重新生成文档并commit,避免丢数据的可能。
  90. *
  91. * @param string $searchAppName 关联的搜索应用名
  92. * @param string $behaviorCollectionName 行为数据采集名称,开通时控制台会返回该名称
  93. * @return \OpenSearch\Generated\Common\OpenSearchResult
  94. */
  95. public function commit($searchAppName, $behaviorCollectionName) {
  96. $recordsJson = json_encode($this->recordBuffer);
  97. $this->recordBuffer = array();
  98. return $this->doPush($recordsJson, $searchAppName, $behaviorCollectionName);
  99. }
  100. /**
  101. * 批量推送文档。
  102. *
  103. * > Note:
  104. * >
  105. * > 此操作会同步发送文档到服务端。
  106. *
  107. * @param string $recordsJson 文档list的json
  108. * @param string $searchAppName 关联的搜索应用名
  109. * @param string $behaviorCollectionName 行为数据采集名称,开通时控制台会返回该名称
  110. * @return \OpenSearch\Generated\Common\OpenSearchResult
  111. */
  112. public function push($recordsJson, $searchAppName, $behaviorCollectionName) {
  113. return $this->doPush($recordsJson, $searchAppName, $behaviorCollectionName);
  114. }
  115. private function doPush($recordsJson, $searchAppName, $behaviorCollectionName) {
  116. $path = self::createPushPath($searchAppName, $behaviorCollectionName);
  117. return $this->openSearchClient->post($path, $recordsJson);
  118. }
  119. private function addOneRecord($jsonFields, $command) {
  120. $cmdName = Constant::get('DOC_KEY_CMD');
  121. $fieldsName = Constant::get('DOC_KEY_FIELDS');
  122. $jsonRecord = [
  123. $cmdName => $command,
  124. $fieldsName => $jsonFields
  125. ];
  126. $this->recordBuffer[] = $jsonRecord;
  127. }
  128. private static function createPushPath($searchAppName, $behaviorCollectionName) {
  129. return sprintf("/app-groups/%s/data-collections/%s/actions/bulk", $searchAppName, $behaviorCollectionName);
  130. }
  131. private static function createSearchDocClickArgs($objectId, $opsRequestMisc) {
  132. return sprintf("object_id=%s,object_type=ops_search_doc,ops_request_misc=%s", $objectId, $opsRequestMisc);
  133. }
  134. }