Use elastic index instead of type for searchableAs. (#2750)

Use elastic `index` instead of `type` for `searchableAs`, when the config of `index` is `null` or the elastic version is more than `7.0.0`.

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
pandaLIU 2020-11-10 18:38:58 +08:00 committed by GitHub
parent 72fbb8f3b8
commit f092abb0c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 14 deletions

View File

@ -5,6 +5,10 @@
- [#2783](https://github.com/hyperf/hyperf/pull/2783) Fixed nsq consumer does not works in coroutine style server.
- [#2788](https://github.com/hyperf/hyperf/pull/2788) Fixed call non-static method `__handlePropertyHandler()` statically in class proxy.
## Optimized
- [#2750](https://github.com/hyperf/hyperf/pull/2750) Use elastic `index` instead of `type` for `searchableAs`, when the config of `index` is `null` or the elastic version is more than `7.0.0`.
# v2.0.18 - 2020-11-09
## Added

View File

@ -20,7 +20,7 @@ return [
'concurrency' => 100,
'engine' => [
'elasticsearch' => [
'driver' => \Hyperf\Scout\Provider\ElasticsearchProvider::class,
'driver' => Hyperf\Scout\Provider\ElasticsearchProvider::class,
'index' => env('ELASTICSEARCH_INDEX', 'hyperf'),
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://localhost'),

View File

@ -22,10 +22,17 @@ use Hyperf\Utils\Collection as BaseCollection;
class ElasticsearchEngine extends Engine
{
/**
* Index where the models will be saved.
* Elastic server version.
*
* @var string
*/
public static $version;
/**
* Index where the models will be saved.
*
* @var null|string
*/
protected $index;
/**
@ -37,13 +44,13 @@ class ElasticsearchEngine extends Engine
/**
* Create a new engine instance.
*
* @param $index
*/
public function __construct(Client $client, $index)
public function __construct(Client $client, ?string $index = null)
{
$this->elastic = $client;
$this->index = $index;
if ($index) {
$this->index = $this->initIndex($client, $index);
}
}
/**
@ -55,13 +62,19 @@ class ElasticsearchEngine extends Engine
{
$params['body'] = [];
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'update' => [
if ($this->index) {
$update = [
'_id' => $model->getKey(),
'_index' => $this->index,
'_type' => $model->searchableAs(),
],
];
];
} else {
$update = [
'_id' => $model->getKey(),
'_index' => $model->searchableAs(),
];
}
$params['body'][] = ['update' => $update];
$params['body'][] = [
'doc' => $model->toSearchableArray(),
'doc_as_upsert' => true,
@ -79,13 +92,19 @@ class ElasticsearchEngine extends Engine
{
$params['body'] = [];
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'delete' => [
if ($this->index) {
$delete = [
'_id' => $model->getKey(),
'_index' => $this->index,
'_type' => $model->searchableAs(),
],
];
];
} else {
$delete = [
'_id' => $model->getKey(),
'_index' => $model->searchableAs(),
];
}
$params['body'][] = ['delete' => $delete];
});
$this->elastic->bulk($params);
}
@ -171,6 +190,24 @@ class ElasticsearchEngine extends Engine
->unsearchable();
}
protected function initIndex(Client $client, string $index): ?string
{
if (! static::$version) {
try {
static::$version = $client->info()['version']['number'];
} catch (\Throwable $exception) {
static::$version = '0.0.0';
}
}
// When the version of elasticsearch is more than 7.0.0, it does not support type, so set `null` to `$index`.
if (version_compare(static::$version, '7.0.0', '<')) {
return $index;
}
return null;
}
/**
* Perform the given search on the engine.
*
@ -189,6 +226,10 @@ class ElasticsearchEngine extends Engine
],
],
];
if (! $this->index) {
unset($params['type']);
$params['index'] = $builder->index ?: $builder->model->searchableAs();
}
if ($sort = $this->sort($builder)) {
$params['body']['sort'] = $sort;
}