-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
521 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.