perf: 优化ES模块

This commit is contained in:
kuaifan 2025-03-08 11:04:43 +08:00
parent 58cb49b125
commit ed8e443f3a
3 changed files with 35 additions and 23 deletions

View File

@ -19,7 +19,7 @@ class ElasticSearchBase
*
* @var \Elastic\Elasticsearch\Client
*/
public $client;
protected $client;
/**
* 当前操作的索引名称
@ -279,4 +279,30 @@ class ElasticSearchBase
return ['error' => $e->getMessage(), 'hits' => ['total' => ['value' => 0], 'hits' => []]];
}
}
/**
* 索引名称
*/
const indexName = 'default';
/**
* 获取索引名称
* @param string $index 索引名称
* @param string|null $prefix 索引前缀
* @param string|null $subfix 索引后缀
* @return string
*/
public static function indexName($index = '', $prefix = '', $subfix = '')
{
$index = $index ?: static::indexName;
$prefix = $prefix ?: env('ES_INDEX_PREFIX', '');
$subfix = $subfix ?: env('ES_INDEX_SUFFIX', '');
if ($prefix) {
$index = rtrim($prefix, '_') . '_' . $index;
}
if ($subfix) {
$index = $index . '_' . ltrim($subfix, '_');
}
return $index;
}
}

View File

@ -13,6 +13,8 @@ use Illuminate\Support\Facades\Log;
*/
class ElasticSearchKeyValue extends ElasticSearchBase
{
const indexName = 'key_value_store';
/**
* 构造函数
* @return ElasticSearchBase
@ -27,15 +29,6 @@ class ElasticSearchKeyValue extends ElasticSearchBase
/** *********************************** 键值存储方法 ******************************************************** */
/** ******************************************************************************************************** */
/**
* 键值存储索引名称
* @return string
*/
public static function indexName()
{
return "key_value_store" . env("ES_INDEX_SUFFIX", "");
}
/**
* 创建键值存储索引
* @return array
@ -55,8 +48,8 @@ class ElasticSearchKeyValue extends ElasticSearchBase
'properties' => [
'key' => ['type' => 'keyword'],
'value' => ['type' => 'text', 'fields' => ['keyword' => ['type' => 'keyword']]],
'created_at' => ['type' => 'date'],
'updated_at' => ['type' => 'date']
'created_at' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis'],
'updated_at' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis']
]
];
@ -97,8 +90,8 @@ class ElasticSearchKeyValue extends ElasticSearchBase
'key' => $key,
'value' => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value,
'namespace' => $namespace,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
'created_at' => date('Y-m-d\TH:i:s\Z', strtotime(date('Y-m-d H:i:s'))),
'updated_at' => date('Y-m-d\TH:i:s\Z', strtotime(date('Y-m-d H:i:s')))
];
// 索引文档

View File

@ -14,6 +14,8 @@ use Illuminate\Support\Facades\Log;
*/
class ElasticSearchUserMsg extends ElasticSearchBase
{
const indexName = 'dialog_user_msg';
/**
* 构造函数
* @return ElasticSearchBase
@ -28,15 +30,6 @@ class ElasticSearchUserMsg extends ElasticSearchBase
/** *********************************************** 基础 ************************************************** */
/** ******************************************************************************************************** */
/**
* 索引名称
* @return string
*/
public static function indexName()
{
return "dialog_user_msg" . env("ES_INDEX_SUFFIX", "");
}
/**
* 创建聊天系统索引 - 使用父子关系
* @return array