Skip to content

Commit

Permalink
add zinc search engine support
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed May 20, 2022
1 parent ecc72f7 commit 93aa785
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/Components/Search/GoFoundDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function auth(): ?array
return null;
}

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

/**
Expand Down
168 changes: 168 additions & 0 deletions app/Components/Search/ZincSearchDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

namespace App\Components\Search;

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

/**
* ZincSearchDriver
*
* https://docs.zincsearch.com/
*/
class ZincSearchDriver implements Driver
{
/**
* @var Client
*/
private $client = null;

/**
* Index name
*
* @var string
*/
private $index = '';

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

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

/**
* 搜索类型
*
* @var string
*/
private $searchType = 'matchphrase';

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

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

return [$this->authUsername, $this->authPassword];
}

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


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

$resp = $this->client->put("/api/{$this->index}/_doc/{$doc->id}", [
'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/{$this->index}/_search", [
'json' => [
'search_type' => $this->searchType,
'query' => [
'term' => $keyword,
],
'from' => $page * $perPage,
'max_results' => $perPage * 2,
'_source' => ['id', 'type'],
],
'auth' => $this->auth(),
]);

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

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

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

if (empty($respBody['error'])) {
$sortIds = collect($respBody['hits']['hits'] ?? [])->map(function ($doc) {
return $doc['_id'];
})->toArray();

return new Result(array_slice($sortIds, 0, $perPage), [$keyword]);
}

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

return null;
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/SyncDocumentToIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle()
Search::get()->syncIndex($doc);
$this->info(sprintf("sync document %s ok", $doc->title));
} catch (\Exception $ex) {
$this->error($ex->getMessage());
$this->error("{$ex->getFile()}:{$ex->getLine()} {$ex->getMessage()}");
}
}
});
Expand Down
15 changes: 15 additions & 0 deletions config/wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
*
* GoFound: App\Components\Search\GoFoundDriver
* Null: App\Components\Search\NullDriver
* ZincSearch: App\Components\Search\ZincSearchDriver
*/
'driver' => env('WIZARD_SEARCH_DRIVER', 'App\Components\Search\NullDriver'),

Expand All @@ -176,6 +177,20 @@
'username' => env('WIZARD_GOFOUND_USERNAME', ''),
'password' => env('WIZARD_GOFOUND_PASSWORD', ''),
],
/**
* ZincSearchDriver
*
* https://docs.zincsearch.com/
*/
'zinc' => [
'server' => env('WIZARD_ZINC_SERVER', 'http:https://localhost:4080'),
'index' => env('WIZARD_ZINC_INDEX', 'wizard'),
'username' => env('WIZARD_ZINC_USERNAME', ''),
'password' => env('WIZARD_ZINC_PASSWORD', ''),
// 支持: alldocuments,wildcard,fuzzy,term,daterange,matchall,match,matchphrase,multiphrase,prefix,querystring
// 参考文档: https://docs.zincsearch.com/API%20Reference/search/1_search/
'search_type' => env('WIZARD_ZINC_SEARCH_TYPE', 'matchphrase'),
],
],
],
];

0 comments on commit 93aa785

Please sign in to comment.