✔️实时表

实时表 是 Manticore 中的主要表类型。它允许您添加、更新和删除文档,并且可以立即看到这些更改。您可以通过配置文件设置实时表,或使用 CREATEUPDATEDELETEALTER 等命令来操作。

在内部,实时表由一个或多个称为 chunk(块)普通表 组成。chunk 分为两种类型:

  • 多个 磁盘块 - 这些块保存到磁盘中,其结构类似于 普通表

  • 一个 内存块 - 存储在内存中,收集所有的更改。

内存块的大小由 rt_mem_limit 设置控制。一旦达到此限制,内存块将被传输到磁盘作为磁盘块。如果磁盘块过多,Manticore 会 合并部分块 以提高性能。

创建实时表:

您可以通过两种方式创建新的实时表:使用 CREATE TABLE 命令,或通过 HTTP JSON API 的 _mapping endpoint

CREATE TABLE 命令:

您可以通过 SQL 和 HTTP 协议使用此命令:

通过 SQL 协议创建实时表:

CREATE TABLE products(title text, price float) morphology='stem_en';
Query OK, 0 rows affected (0.00 sec)

通过 HTTP 使用 JSON 创建实时表:

POST /cli -d "CREATE TABLE products(title text, price float)  morphology='stem_en'"
{
"total":0,
"error":"",
"warning":""
}

通过PHP客户端创建实时表:

$index = new \Manticoresearch\Index($client);
$index->setName('products');
$index->create([
    'title'=>['type'=>'text'],
    'price'=>['type'=>'float'],
]);

Python:

utilsApi.sql('CREATE TABLE forum(title text, price float)')

Javascript:

res = await utilsApi.sql('CREATE TABLE forum(title text, price float)');

Java:

utilsApi.sql("CREATE TABLE forum(title text, price float)");

C#:

utilsApi.Sql("CREATE TABLE forum(title text, price float)");

通过配置文件创建实时表:

table products {
  type = rt
  path = tbl
  rt_field = title
  rt_attr_uint = price
  stored_fields = title
}

_mapping API:

注意:_mapping API 需要 Manticore Buddy。如果它不起作用,请确保 Buddy 已安装。

您还可以通过 _mapping 端点创建一个新表。此端点允许您定义类似 Elasticsearch 的表结构,并将其转换为 Manticore 表。

请求的主体必须具有以下结构:

"properties"
{
  "FIELD_NAME_1":
  {
    "type": "FIELD_TYPE_1"
  },
  "FIELD_NAME_2":
  {
    "type": "FIELD_TYPE_2"
  },

  ...

  "FIELD_NAME_N":
  {
    "type": "FIELD_TYPE_M"
  }
}

创建表时,Elasticsearch 数据类型将根据以下规则映射到 Manticore 类型:

  • aggregate_metric => json

  • binary => string

  • boolean => bool

  • byte => int

  • completion => string

  • date => timestamp

  • date_nanos => bigint

  • date_range => json

  • dense_vector => json

  • flattened => json

  • flat_object => json

  • float => float

  • float_range => json

  • geo_point => json

  • geo_shape => json

  • half_float => float

  • histogram => json

  • integer => int

  • integer_range => json

  • ip => string

  • ip_range => json

  • keyword => string

  • knn_vector => float_vector

  • long => bigint

  • long_range => json

  • match_only_text => text

  • object => json

  • point => json

  • scaled_float => float

  • search_as_you_type => text

  • shape => json

  • short => int

  • text => text

  • unsigned_long => int

  • version => string

通过 _mapping 端点创建一个实时表:

POST /your_table_name/_mapping -d '
{
  "test": {
    "mappings": {
      "properties": {
        "price": {
            "type": "float"
        },
        "title": {
            "type": "text"
        }
      }
    }
  }
}
'
{
"total":0,
"error":"",
"warning":""
}

CREATE TABLE LIKE:

你可以创建一个实时表的副本,包含或不包含数据。如果表很大,复制数据可能会花费一些时间。复制操作是同步模式的,但如果连接中断,它会在后台继续执行。

CREATE TABLE table_name LIKE old_table_name [WITH DATA]

注意:复制表需要 Manticore Buddy。如果无法正常工作,请确保 Buddy 已安装。

示例:

create table products LIKE old_products;

示例 (WITH DATA):

create table products LIKE old_products WITH DATA;

👍 你可以用实时表做的事:

  • 通过 更新 过程更新属性和全文字段。

  • 使用 ALTER 命令在线更改表结构,如在线更新表结构 中所述。

  • 按照 定义表 的说明,在配置文件中定义表。

  • 使用 UUID 功能自动分配ID。

⛔ 你不能用实时表做的事:

  • 使用 indexer 功能导入数据。

  • 连接到 sources 以便从外部存储轻松索引数据。

  • 更新 killlist_target,因为它由实时表自动管理。

实时表文件结构

下表列出了实时表中不同文件扩展名及其对应的描述:

扩展名
描述

.lock

锁文件,确保同一时间内只有一个进程可以访问该表。

.ram

表的RAM块,存储在内存中,用作变更的累加器。

.meta

实时表的头文件,定义了表的结构和设置。

.*.sp*

磁盘块,存储在磁盘上,格式与普通表相同。当RAM块大小超过 rt_mem_limit 时,创建这些文件。

有关磁盘块结构的更多信息,请参考 普通表文件结构

最后更新于