Commit 5326d3ed authored by clone's avatar clone

1

parent 0114749e
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
$basePath = __DIR__ . '/../../';
require_once $basePath . 'OpenSearch/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $basePath . 'OpenSearch');
$loader->registerNamespace('OpenSearch', $basePath);
$loader->registerDefinition('OpenSearch', $basePath);
$loader->register();
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace OpenSearch\Client;
use OpenSearch\Generated\App\AppServiceIf;
use OpenSearch\Generated\Common\Pageable;
/**
* 应用基本信息管理类。
*
* 管理应用的基本信息,包含创建应用(save)、修改应用(updateById)、删除应用(removeById)
* 、获取应用的基本详情(getById)、获取应用列表(listAll)、给应用导入全量数据(reindexById)
* 等方法。
*
*/
class AppClient implements AppServiceIf {
private $openSearchClient = null;
private $path = "/apps";
/**
* 构造方法。
*
* @param \OpenSearch\Client\OpenSearchClient $openSearchClient 基础类,负责计算签名,和服务端进行交互和返回结果。
* @return void
*/
public function __construct($openSearchClient) {
$this->openSearchClient = $openSearchClient;
}
/**
* 创建一个新应用,或者创建一个新版本。
*
* 创建一个新的应用或者创建一个新的版本,如果在$app中指定了name,则会创建一个新版本,否则会创建一个新应用。
*
* > 创建版本的个数依赖服务端的限制。
*
* @param string $app 要创建的应用主体JSON,包含name、type、schema、quota、first_ranks、second_ranks、summary、data_sources、suggest、fetch_fields、query_processors等信息。
* @return \OpenSearch\Generated\Common\OpenSearchResult OpenSearchResult类
*/
public function save($app) {
return $this->openSearchClient->post($this->path, $app);
}
/**
* 通过应用名称或者应用ID获取一个应用的详情信息。
*
* @param string $identity 要查询的应用名称或者应用ID,如果应用有多个版本,则指定应用名称为当前应用的在线版本。
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function getById($identity) {
$path = $this->path . "/" . $identity;
return $this->openSearchClient->get($path);
}
/**
* 获取应用列表。
*
* @param \OpenSearch\Generated\Common\Pageable $pageable 分页信息,包含页码和每页展示条数。
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function listAll(Pageable $pageable) {
return $this->openSearchClient->get(
$this->path, array('page' => $pageable->page, 'size' => $pageable->size)
);
}
/**
* 根据指定的应用id或名称删除应用版本或者应用;当指定的为应用名称,则表示指定的为当前应用分组中的在线的应用。。
*
* 如果当前应用只有一个版本,则会删除这个应用的整个分组;
* 如果当前应用分组有多个应用,则需要当前要删除的版本不能处于在线状态。
*
* @param string $identity 指定的应用ID或者应用名称。
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function removeById($identity) {
$path = $this->path . "/" . $identity;
return $this->openSearchClient->delete($path);
}
/**
* 更新某个应用的信息。
*
* @param string $identity 指定的应用ID或者应用名称;当指定的为应用名称,则表示指定的为当前应用分组中的在线的应用。
* @param string $app 修改一个应用的应用结构json,包含name、type、schema、quota、first_ranks、second_ranks、summary、data_sources、suggest、fetch_fields、query_processors等信息。
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function updateById($identity, $app) {
$path = $this->path . "/" . $identity;
return $this->openSearchClient->patch($path, $app);
}
/**
* 在创建过程中全量导入数据。
*
* @param string $identity 指定的应用ID或者应用名称;当指定的为应用名称,则表示指定的为当前应用分组中的在线的应用。。
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function reindexById($identity) {
$path = $this->path . "/{$identity}/actions/reindex";
return $this->openSearchClient->post($path);
}
}
\ No newline at end of file
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace OpenSearch\Client;
use OpenSearch\Generated\BehaviorCollection\Command;
use OpenSearch\Generated\BehaviorCollection\Constant;
use OpenSearch\Generated\BehaviorCollection\BehaviorCollectionServiceIf;
use OpenSearch\Client\OpenSearchClient;
/**
* 搜索行为数据文档推送类。
*
* 管理搜索应用的行为数据推送,包含单条推送文档、批量推送文档等。
*
*/
class BehaviorCollectionClient implements BehaviorCollectionServiceIf {
private $openSearchClient;
private $recordBuffer = array();
const SEARCH_DOC_CLICK_EVENT_ID = 2001;
/**
* 构造方法。
*
* @param \OpenSearch\Client\OpenSearchClient $openSearchClient 基础类,负责计算签名,和服务端进行交互和返回结果。
* @return void
*/
public function __construct($openSearchClient) {
$this->openSearchClient = $openSearchClient;
}
/**
* 增加一条搜索点击文档。
*
* > Note:
* >
* > 这条文档只是增加到sdk client buffer中,没有正式提交到服务端;只有调用了commit方法才会被提交到服务端。
* > 你可以多次addSearchDocClickRecord然后调用commit() 统一提交。
*
* @param string $searchDocListPage 搜索结果列表所在的页面名称
* @param string $docDetailPage 某个搜索文档被点击后,搜索文档的详情页面名称
* @param int $detailPageStayTime 用户在详情页停留的时长(单位为ms)
* @param string $objectId 被点击的文档的主键,不能为空
* @param string $opsRequestMisc opensearch返回的查询结果中的ops_request_misc字段
* @param string $basicFields 其他基础字段, 非必需字段
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function addSearchDocClickRecord(
$searchDocListPage,
$docDetailPage,
$detailPageStayTime,
$objectId,
$opsRequestMisc,
array $basicFields = []) {
$jsonFields = [
'event_id' => self::SEARCH_DOC_CLICK_EVENT_ID,
'sdk_type' => OpenSearchClient::SDK_TYPE,
'sdk_version' => OpenSearchClient::SDK_VERSION,
'page' => $docDetailPage,
'arg1' => $searchDocListPage,
'arg2' => "",
'arg3' => $detailPageStayTime,
'args' => self::createSearchDocClickArgs($objectId, $opsRequestMisc),
];
if (!empty($basicFields)) {
foreach ($basicFields as $key => $value) {
$jsonFields[$key] = $value;
}
}
$this->addOneRecord($jsonFields, Command::$__names[Command::ADD]);
}
/**
* 把sdk client buffer中的文档发布到服务端。
*
* > Note:
* >
* > 在发送之前会把buffer中的文档清空,所以如果服务端返回错误需要重试的情况下,需要重新生成文档并commit,避免丢数据的可能。
*
* @param string $searchAppName 关联的搜索应用名
* @param string $behaviorCollectionName 行为数据采集名称,开通时控制台会返回该名称
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function commit($searchAppName, $behaviorCollectionName) {
$recordsJson = json_encode($this->recordBuffer);
$this->recordBuffer = array();
return $this->doPush($recordsJson, $searchAppName, $behaviorCollectionName);
}
/**
* 批量推送文档。
*
* > Note:
* >
* > 此操作会同步发送文档到服务端。
*
* @param string $recordsJson 文档list的json
* @param string $searchAppName 关联的搜索应用名
* @param string $behaviorCollectionName 行为数据采集名称,开通时控制台会返回该名称
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function push($recordsJson, $searchAppName, $behaviorCollectionName) {
return $this->doPush($recordsJson, $searchAppName, $behaviorCollectionName);
}
private function doPush($recordsJson, $searchAppName, $behaviorCollectionName) {
$path = self::createPushPath($searchAppName, $behaviorCollectionName);
return $this->openSearchClient->post($path, $recordsJson);
}
private function addOneRecord($jsonFields, $command) {
$cmdName = Constant::get('DOC_KEY_CMD');
$fieldsName = Constant::get('DOC_KEY_FIELDS');
$jsonRecord = [
$cmdName => $command,
$fieldsName => $jsonFields
];
$this->recordBuffer[] = $jsonRecord;
}
private static function createPushPath($searchAppName, $behaviorCollectionName) {
return sprintf("/app-groups/%s/data-collections/%s/actions/bulk", $searchAppName, $behaviorCollectionName);
}
private static function createSearchDocClickArgs($objectId, $opsRequestMisc) {
return sprintf("object_id=%s,object_type=ops_search_doc,ops_request_misc=%s", $objectId, $opsRequestMisc);
}
}
\ No newline at end of file
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace OpenSearch\Client;
use OpenSearch\Generated\Document\Command;
use OpenSearch\Generated\Document\Constant;
use OpenSearch\Generated\Document\DocumentServiceIf;
/**
* 应用文档操作类。
*
* 管理应用的文档,包含推送文档,删除文档,更新文档,批量推送文档等。
*
*/
class DocumentClient implements DocumentServiceIf {
private $openSearchClient;
const DOCUMENT_API_PATH = '/apps';
/**
* @var sdk缓存的文档的数量。
*/
public $docs = array();
/**
* 构造方法。
*
* @param \OpenSearch\Client\OpenSearchClient $openSearchClient 基础类,负责计算签名,和服务端进行交互和返回结果。
* @return void
*/
public function __construct($openSearchClient) {
$this->openSearchClient = $openSearchClient;
}
/**
* 增加一条文档。
*
* > Note:
* >
* > 这条文档只是增加到sdk client buffer中,没有正式提交到服务端;只有调用了commit方法才会被提交到服务端。
* 你可以add多次然后调用commit() 统一提交。
*
* @param array $fields 一条文档的所有字段,例如array("id" => 1, "name" => "tony");
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function add($fields) {
$this->pushOneDoc($fields, Command::$__names[Command::ADD]);
}
/**
* 修改一条文档。
*
* > Note:
* >
* > 这条文档只是增加到sdk client buffer中,没有正式提交到服务端;只有调用了commit方法才会被提交到服务端。
* 你可以update多次然后调用commit() 统一提交。
*
* > 标准版不支持update操作。
*
* @param array $fields 一条文档的所有字段,例如array("id" => 1, "name" => "tony");
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function update($fields) {
$this->pushOneDoc($fields, Command::$__names[Command::UPDATE]);
}
/**
* 删除一条文档。
*
* > Note:
* >
* > 这条文档只是增加到sdk client buffer中,没有正式提交到服务端;只有调用了commit方法才会被提交到服务端。
* 你可以remove多次然后调用commit() 统一提交。
*
* @param array $fields 一条文档的主键字段,例如array("id" => 1);
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function remove($fields) {
$this->pushOneDoc($fields, Command::$__names[Command::DELETE]);
}
/**
* 批量推送文档。
*
* > Note:
* >
* > 此操作会同步发送到服务端。
*
* @param string $docsJson 文档list的json,例如[{"cmd":"ADD","fields":{"id":"1","name":"tony"}},...]
* @param string $appName 指定的app name或者app ID
* @param string $tableName 指定的table name
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function push($docsJson, $appName, $tableName) {
$path = self::_getPath($appName, $tableName);
return $this->openSearchClient->post($path, $docsJson);
}
/**
* 把client buffer中的文档发布到服务端。
*
* > Note:
* >
* > 在发送之前会把buffer中的文档清空,所以如果服务端返回错误需要重试的情况下,需要重新生成文档并commit,避免丢数据的可能。
*
* @param string $appName 指定的app name或者app ID
* @param string $tableName 指定的table name
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function commit($appName, $tableName) {
$json = json_encode($this->docs);
$this->docs = array();
return $this->push($json, $appName, $tableName);
}
/**
* 推送一条文档到客户端buffer中。
*
* @param array $fields 一条文档的所有字段,例如array("id" => 1, "name" => "tony");
* @param string $cmd 文档的操作类型,有ADD, UPDATE和DELETE;
* @return \OpenSearch\Generated\Common\OpenSearchResult
*/
public function pushOneDoc($fields, $cmd) {
$cmdName = Constant::get('DOC_KEY_CMD');
$fieldName = Constant::get('DOC_KEY_FIELDS');
$this->docs[] = array($cmdName => $cmd, $fieldName => $fields);
}
private static function _getPath($appName, $tableName) {
return self::DOCUMENT_API_PATH . sprintf("/%s/%s/actions/bulk", $appName, $tableName);
}
}
\ No newline at end of file
This diff is collapsed.
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace OpenSearch\Client;
use OpenSearch\Generated\Search\Config;
use OpenSearch\Generated\Search\OpenSearchSearcherServiceIf;
use OpenSearch\Generated\Search\SearchFormat;
use OpenSearch\Generated\Search\SearchParams;
use OpenSearch\Util\UrlParamsBuilder;
/**
* 应用搜索操作类。
*
* 通过制定关键词、过滤条件搜索应用结果。
*
*/
class SearchClient implements OpenSearchSearcherServiceIf {
const SEARCH_API_PATH = '/apps/%s/search';
private $openSearchClient;
/**
* 构造方法。
*
* @param \OpenSearch\Client\OpenSearchClient $openSearchClient 基础类,负责计算签名,和服务端进行交互和返回结果。
* @return void
*/
public function __construct($openSearchClient) {
$this->openSearchClient = $openSearchClient;
}
/**
* 执行搜索操作。
*
* @param \OpenSearch\Generated\Search\SearchParams $searchParams 制定的搜索条件。
* @return \OpenSearch\Generated\Common\OpenSearchResult OpenSearchResult类
*/
public function execute(SearchParams $searchParams) {
$path = self::getPath($searchParams);
$builder = new UrlParamsBuilder($searchParams);
return $this->openSearchClient->get($path, $builder->getHttpParams());
}
private static function getPath($searchParams) {
$appNames = isset($searchParams->config->appNames) ? implode(',', $searchParams->config->appNames) : '';
return sprintf(self::SEARCH_API_PATH, $appNames);
}
}
\ No newline at end of file
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace OpenSearch\Client;
use OpenSearch\Generated\Search\OpenSearchSearcherServiceIf;
use OpenSearch\Generated\Search\SearchParams;
use OpenSearch\Util\SuggestParamsBuilder;
/**
* 应用下拉提示操作类。
*
* 通过制定关键词、过滤条件搜索应用的下拉提示的结果。
*
*/
class SuggestClient implements OpenSearchSearcherServiceIf {
const SUGGEST_API_PATH = '/apps/%s/suggest/%s/search';
private $openSearchClient;
/**
* 构造方法。
*
* @param \OpenSearch\Client\OpenSearchClient $openSearchClient 基础类,负责计算签名,和服务端进行交互和返回结果。
* @return void
*/
public function __construct($openSearchClient) {
$this->openSearchClient = $openSearchClient;
}
/**
* 执行搜索操作。
*
* @param \OpenSearch\Generated\Search\SearchParams $searchParams 制定的搜索条件。
* @return \OpenSearch\Generated\Common\OpenSearchResult OpenSearchResult类
*/
public function execute(SearchParams $searchParams) {
$path = self::getPath($searchParams);
$params = SuggestParamsBuilder::getQueryParams($searchParams);
return $this->openSearchClient->get($path, $params);
}
private static function getPath($searchParams) {
$appName = implode(',', $searchParams->config->appNames);
$suggestName = $searchParams->suggest->suggestName;
return sprintf(self::SUGGEST_API_PATH, $appName, $suggestName);
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
<?php
namespace OpenSearch\Generated\BehaviorCollection;
/**
* Autogenerated by Thrift Compiler (0.9.3)
*
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
* @generated
*/
use Thrift\Base\TBase;
use Thrift\Type\TType;
use Thrift\Type\TMessageType;
use Thrift\Exception\TException;
use Thrift\Exception\TProtocolException;
use Thrift\Protocol\TProtocol;
use Thrift\Protocol\TBinaryProtocolAccelerated;
use Thrift\Exception\TApplicationException;
final class Command {
const ADD = 0;
static public $__names = array(
0 => 'ADD',
);
}
final class Constant extends \Thrift\Type\TConstant {
static protected $DOC_KEY_CMD;
static protected $DOC_KEY_FIELDS;
static protected function init_DOC_KEY_CMD() {
return "cmd";
}
static protected function init_DOC_KEY_FIELDS() {
return "fields";
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment