Skip to content

Commit

Permalink
updatE
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed May 19, 2022
1 parent 02527ca commit 74b3575
Show file tree
Hide file tree
Showing 28 changed files with 521 additions and 75 deletions.
38 changes: 38 additions & 0 deletions app/Components/Search/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Components\Search;

use App\Repositories\Document;

interface Driver
{
/**
* 删除文档索引
*
* @param $id
*
* @return void
*/
public function deleteIndex($id);

/**
* 同步索引
*
* @param Document $doc
*
* @return void
* @throws \Exception
*/
public function syncIndex(Document $doc);

/**
* 执行文档搜索
*
* @param string $keyword
* @param int $page
* @param int $perPage
*
* @return Result|null
*/
public function search(string $keyword, int $page, int $perPage): ?Result;
}
154 changes: 154 additions & 0 deletions app/Components/Search/GoFoundDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace App\Components\Search;

use App\Repositories\Document;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class GoFoundDriver implements Driver
{
/**
* @var Client
*/
private $client = null;

/**
* Database
*
* @var string
*/
private $database = '';

/**
* Basic Auth username
* @var string
*/
private $authUsername = '';

/**
* Basic Auth password
*
* @var string
*/
private $authPassword = '';

public function __construct()
{
$this->client = new Client([
'base_uri' => config('wizard.search.drivers.gofound.server', 'http:https://localhost:5678'),
'timeout' => 3.0,
]);
$this->database = config('wizard.search.drivers.gofound.database', 'default');
$this->authUsername = config('wizard.search.drivers.gofound.username');
$this->authPassword = config('wizard.search.drivers.gofound.password');
}

/**
* 鉴权
*
* @return array|null
*/
private function auth(): ?array
{
if (empty($this->authUsername)) {
return null;
}

return ['username' => $this->authUsername, 'password' => $this->authPassword];
}

/**
* 删除文档索引
*
* @param $id
*
* @return void
*/
public function deleteIndex($id)
{
$this->client->post(
"/api/index/remove?database={$this->database}",
[
'json' => ['id' => $id],
'auth' => $this->auth(),
]
);
}


/**
* 同步索引
*
* @param Document $doc
*
* @return void
* @throws \Exception
*/
public function syncIndex(Document $doc)
{
$req = [
'id' => $doc->id,
'text' => $doc->title . '\n\n' . $doc->content,
'document' => [
'id' => $doc->id,
'project_id' => $doc->project_id,
],
];

$resp = $this->client->post("/api/index?database={$this->database}", [
'json' => $req,
'auth' => $this->auth(),
]);

if ($resp->getStatusCode() !== 200) {
throw new \Exception("sync document to server failed: " . $resp->getReasonPhrase() . ", response: " . $resp->getBody()->getContents());
}
}


/**
* 执行文档搜索
*
* @param string $keyword
* @param int $page
* @param int $perPage
*
* @return Result|null
*/
public function search(string $keyword, int $page, int $perPage): ?Result
{
try {
$resp = $this->client->post("/api/query?database={$this->database}", [
'json' => [
'query' => $keyword,
'page' => $page,
'limit' => $perPage * 2,
],
'auth' => $this->auth(),
]);

if ($resp->getStatusCode() !== 200) {
return null;
}

$respBody = json_decode($resp->getBody()->getContents(), true);

Log::info('search-request', $respBody);

if ($respBody['state']) {
$sortIds = collect($respBody['data']['documents'] ?? [])->map(function ($doc) {
return $doc['id'];
})->toArray();

return new Result(array_slice($sortIds, 0, $perPage), $respBody['data']['words'] ?? []);
}

return null;
} catch (\Exception $ex) {
Log::error('search failed', ['message' => $ex->getMessage()]);
}

return null;
}
}
33 changes: 33 additions & 0 deletions app/Components/Search/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Components\Search;

/**
* 搜索结果
*/
class Result
{
/**
* 文档 ID 列表
*
* @var array
*/
public $ids;

/**
* 搜索关键字列表
*
* @var array
*/
public $words;

/**
* @param array $ids
* @param array $words
*/
public function __construct(array $ids, array $words)
{
$this->ids = $ids;
$this->words = $words;
}
}
69 changes: 69 additions & 0 deletions app/Components/Search/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Components\Search;

use App\Repositories\Document;

class Search implements Driver
{
/**
* 搜索驱动实例
*
* @var Driver
*/
private static $driver;

/**
* 获取搜索实现驱动实例
*
* @return Driver
*/
public static function get(): Driver
{
if (is_null(self::$driver)) {
$driverName = config('wizard.search.driver');
self::$driver = new $driverName();
}

return self::$driver;
}

/**
* 删除索引
*
* @param $id
*
* @return void
*/
public function deleteIndex($id)
{
self::$driver->deleteIndex($id);
}

/**
* 同步索引
*
* @param Document $doc
*
* @return void
* @throws \Exception
*/
public function syncIndex(Document $doc)
{
self::$driver->syncIndex($doc);
}

/**
* 关键词搜索
*
* @param string $keyword
* @param int $page
* @param int $perPage
*
* @return Result|null
*/
public function search(string $keyword, int $page, int $perPage): ?Result
{
return self::$driver->search($keyword, $page, $perPage);
}
}
49 changes: 49 additions & 0 deletions app/Console/Commands/SyncDocumentToIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Console\Commands;

use App\Components\Search\Search;
use App\Repositories\Document;
use Illuminate\Console\Command;

/**
* 文档索引同步到搜索引擎
*/
class SyncDocumentToIndex extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync-index:document';

/**
* The console command description.
*
* @var string
*/
protected $description = 'sync document to index server for search';


/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Document::chunk(10, function ($docs) {
/** @var Document $doc */
foreach ($docs as $doc) {
try {
Search::get()->syncIndex($doc);
$this->info(sprintf("sync document %s ok", $doc->title));
} catch (\Exception $ex) {
$this->error($ex->getMessage());
}
}
});
}

}
5 changes: 3 additions & 2 deletions app/Http/Controllers/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,13 @@ public function getJson($id, $page_id)
/**
* 获取Swagger文档内容
*
* @param Request $request
* @param $id
* @param $page_id
*
* @return string
*/
private function getSwaggerContent($id, $page_id): string
private function getSwaggerContent(Request $request, $id, $page_id): string
{
/** @var Project $project */
$project = Project::findOrFail($id);
Expand All @@ -498,7 +499,7 @@ private function getSwaggerContent($id, $page_id): string
abort(422, '该文档不是Swagger文档');
}

return $page->content;
return highlight($page->content, $request->input('keyword', ''));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,17 @@ public function project(Request $request, $id)
->orderBy('created_at', 'desc')
->limit(10)->get();
}

return view('project.project', [
'project' => $project,
'pageID' => $pageID,
'pageItem' => $page,
'keyword' => trim(urldecode($request->input('keyword', ''))),
'scores' => $scores ?? [],
'useful_score_users' => $usefulScoreUsers ?? [],
'user_score_type' => $userScoreType ?? 0,
'type' => $type,
'code' => '',
'operationLogs' => isset($operationLogs) ? $operationLogs : [],
'operationLogs' => $operationLogs ?? [],
'comment_highlight' => $request->input('cm', ''),
'navigators' => navigator($id, $pageID),
'history' => $history ?? false,
Expand Down
Loading

0 comments on commit 74b3575

Please sign in to comment.