DocumentClient.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Document\Command;
  22. use OpenSearch\Generated\Document\Constant;
  23. use OpenSearch\Generated\Document\DocumentServiceIf;
  24. /**
  25. * 应用文档操作类。
  26. *
  27. * 管理应用的文档,包含推送文档,删除文档,更新文档,批量推送文档等。
  28. *
  29. */
  30. class DocumentClient implements DocumentServiceIf {
  31. private $openSearchClient;
  32. const DOCUMENT_API_PATH = '/apps';
  33. /**
  34. * @var sdk缓存的文档的数量。
  35. */
  36. public $docs = array();
  37. /**
  38. * 构造方法。
  39. *
  40. * @param \OpenSearch\Client\OpenSearchClient $openSearchClient 基础类,负责计算签名,和服务端进行交互和返回结果。
  41. * @return void
  42. */
  43. public function __construct($openSearchClient) {
  44. $this->openSearchClient = $openSearchClient;
  45. }
  46. /**
  47. * 增加一条文档。
  48. *
  49. * > Note:
  50. * >
  51. * > 这条文档只是增加到sdk client buffer中,没有正式提交到服务端;只有调用了commit方法才会被提交到服务端。
  52. * 你可以add多次然后调用commit() 统一提交。
  53. *
  54. * @param array $fields 一条文档的所有字段,例如array("id" => 1, "name" => "tony");
  55. * @return \OpenSearch\Generated\Common\OpenSearchResult
  56. */
  57. public function add($fields) {
  58. $this->pushOneDoc($fields, Command::$__names[Command::ADD]);
  59. }
  60. /**
  61. * 修改一条文档。
  62. *
  63. * > Note:
  64. * >
  65. * > 这条文档只是增加到sdk client buffer中,没有正式提交到服务端;只有调用了commit方法才会被提交到服务端。
  66. * 你可以update多次然后调用commit() 统一提交。
  67. *
  68. * > 标准版不支持update操作。
  69. *
  70. * @param array $fields 一条文档的所有字段,例如array("id" => 1, "name" => "tony");
  71. * @return \OpenSearch\Generated\Common\OpenSearchResult
  72. */
  73. public function update($fields) {
  74. $this->pushOneDoc($fields, Command::$__names[Command::UPDATE]);
  75. }
  76. /**
  77. * 删除一条文档。
  78. *
  79. * > Note:
  80. * >
  81. * > 这条文档只是增加到sdk client buffer中,没有正式提交到服务端;只有调用了commit方法才会被提交到服务端。
  82. * 你可以remove多次然后调用commit() 统一提交。
  83. *
  84. * @param array $fields 一条文档的主键字段,例如array("id" => 1);
  85. * @return \OpenSearch\Generated\Common\OpenSearchResult
  86. */
  87. public function remove($fields) {
  88. $this->pushOneDoc($fields, Command::$__names[Command::DELETE]);
  89. }
  90. /**
  91. * 批量推送文档。
  92. *
  93. * > Note:
  94. * >
  95. * > 此操作会同步发送到服务端。
  96. *
  97. * @param string $docsJson 文档list的json,例如[{"cmd":"ADD","fields":{"id":"1","name":"tony"}},...]
  98. * @param string $appName 指定的app name或者app ID
  99. * @param string $tableName 指定的table name
  100. * @return \OpenSearch\Generated\Common\OpenSearchResult
  101. */
  102. public function push($docsJson, $appName, $tableName) {
  103. $path = self::_getPath($appName, $tableName);
  104. return $this->openSearchClient->post($path, $docsJson);
  105. }
  106. /**
  107. * 把client buffer中的文档发布到服务端。
  108. *
  109. * > Note:
  110. * >
  111. * > 在发送之前会把buffer中的文档清空,所以如果服务端返回错误需要重试的情况下,需要重新生成文档并commit,避免丢数据的可能。
  112. *
  113. * @param string $appName 指定的app name或者app ID
  114. * @param string $tableName 指定的table name
  115. * @return \OpenSearch\Generated\Common\OpenSearchResult
  116. */
  117. public function commit($appName, $tableName) {
  118. $json = json_encode($this->docs);
  119. $this->docs = array();
  120. return $this->push($json, $appName, $tableName);
  121. }
  122. /**
  123. * 推送一条文档到客户端buffer中。
  124. *
  125. * @param array $fields 一条文档的所有字段,例如array("id" => 1, "name" => "tony");
  126. * @param string $cmd 文档的操作类型,有ADD, UPDATE和DELETE;
  127. * @return \OpenSearch\Generated\Common\OpenSearchResult
  128. */
  129. public function pushOneDoc($fields, $cmd) {
  130. $cmdName = Constant::get('DOC_KEY_CMD');
  131. $fieldName = Constant::get('DOC_KEY_FIELDS');
  132. $this->docs[] = array($cmdName => $cmd, $fieldName => $fields);
  133. }
  134. private static function _getPath($appName, $tableName) {
  135. return self::DOCUMENT_API_PATH . sprintf("/%s/%s/actions/bulk", $appName, $tableName);
  136. }
  137. }