mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
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:
parent
72fbb8f3b8
commit
f092abb0c4
@ -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
|
||||
|
@ -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'),
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user