表旋转是一个过程,其中 searchd 服务器查找配置中定义的表的新版本。旋转仅支持在纯模式下操作。
有两种情况:
- 对于已经加载的纯表
- 配置中添加了表,但尚未加载
在第一种情况下,索引器无法将表的新版本上线,因为正在运行的副本被 searchd 锁定并加载。在这种情况下,需要使用带有 --rotate 参数的 indexer 命令。如果使用 rotate,indexer 会创建带有 .new. 名称的新表文件,并向 searchd 发送 HUP 信号,通知它有新版本。searchd 会执行查找,将新版本的表替换旧版本并丢弃旧版本。在某些情况下,可能希望创建表的新版本,但不立即执行旋转。例如,可能希望先检查新表版本的健康状况。在这种情况下,indexer 可以接受 --nohup 参数,禁止向服务器发送 HUP 信号。
新表可以通过旋转加载;然而,HUP 信号的常规处理是在服务器启动后配置发生变化时才检查新表。如果表已经在配置中定义,应该先运行不带旋转的 indexer 创建表,然后执行 RELOAD TABLES 语句。
还有两个专用语句可用于对表执行旋转:
RELOAD TABLE tbl [ FROM '/path/to/table_files' [ OPTION switchover=1 ] ];
RELOAD TABLE 命令通过 SQL 实现表旋转。
该命令有三种模式。第一种模式,不指定路径时,Manticore 服务器检查由 path 指定目录中的新表文件。新表文件必须命名为 tbl.new.sp?。
如果指定路径,服务器将在该目录中搜索表文件,将它们移动到表的 path 目录,将文件名从 tbl.sp? 重命名为 tbl.new.sp?,然后执行旋转。
第三种模式,通过 OPTION switchover=1 激活,将索引切换到新路径。此时,守护进程尝试直接从新路径加载表而不移动文件。如果加载成功,新索引将取代旧索引。
此外,守护进程会在由 path 指定的目录中写入唯一的链接文件(tbl.link),以保持持久重定向。
如果将重定向的索引恢复到配置中指定的路径,守护进程会检测到并删除链接文件。
一旦重定向,守护进程将从新链接的路径检索表。旋转时,它会在新重定向的路径查找新表版本。请注意,守护进程会检查配置中的常见错误,如不同表之间路径重复。但它不会识别多个表通过重定向指向同一路径。在正常操作中,表通过 .spl 文件锁定,但禁用锁定可能导致问题。如果出现错误(例如路径因任何原因不可访问),应手动修正(或简单删除)链接文件。
indextool 会遵循链接文件,但其他工具(indexer、index_converter 等)不识别链接文件,始终使用配置文件中定义的路径,忽略任何重定向。因此,可以使用 indextool 检查索引,它会从新位置读取。但更复杂的操作如合并不会识别任何链接文件。
mysql> RELOAD TABLE plain_table;
mysql> RELOAD TABLE plain_table FROM '/home/mighty/new_table_files';
mysql> RELOAD TABLE plain_table FROM '/home/mighty/new/place/for/table/table_files' OPTION switchover=1;
RELOAD TABLES;
该命令的功能类似于 HUP 系统信号,触发表的旋转。然而,它并不完全等同于典型的 HUP 信号(可能来自 kill -HUP 命令或 indexer --rotate)。该命令主动搜索需要旋转的表,并能够重新读取配置。假设你以纯模式启动 Manticore,配置文件指向一个不存在的纯表。如果随后尝试 indexer --rotate 该表,服务器不会识别新表,直到执行 RELOAD TABLES 或重启服务器。
根据 seamless_rotate 设置的值,新查询可能会短暂阻塞,客户端会收到临时错误。
mysql> RELOAD TABLES;
Query OK, 0 rows affected (0.01 sec)
旋转假设旧表版本被丢弃,新表版本被加载并替换现有版本。在此交换过程中,服务器还需要处理针对将被更新的表发起的查询。为避免查询阻塞,服务器默认实现了表的无缝旋转,具体如下。
表可能包含需要预先缓存到内存的数据。目前,.spa、.spb、.spi 和 .spm 文件会被完全预缓存(它们分别包含属性数据、blob 属性数据、关键字表和已删除行映射)。如果没有无缝旋转,旋转表时尽量少用内存,工作流程如下:
- 新查询暂时被拒绝(返回“重试”错误码)。
searchd等待所有当前运行的查询完成。- 旧表被释放,其文件被重命名。
- 新表文件被重命名并分配所需内存。
- 新表的属性和字典数据预加载到内存。
searchd恢复从新表提供查询服务。
然而,如果属性或字典数据量很大,预加载步骤可能需要显著时间——预加载 1-5+ GB 文件时可能需要几分钟。
启用无缝旋转后,旋转的工作流程如下:
- 分配新的表RAM存储。
- 异步预加载新的表属性和字典数据到RAM。
- 成功后,释放旧表并重命名两个表的文件。
- 失败时,释放新表。
- 在任何时刻,查询要么从旧表副本,要么从新表副本提供服务。
无缝旋转的代价是在旋转期间内存的峰值使用量更高(因为在预加载新副本时,旧副本和新副本的.spa/.spb/.spi/.spm数据都需要在RAM中)。但是,平均使用量保持不变。
示例:
seamless_rotate = 1
≫ 更新文档
您可以通过更新或替换来修改 RT 或 PQ 表中的现有数据。
UPDATE 会用新值替换现有文档的按行属性值。全文字段和列式属性不能被更新。如果您需要更改全文字段或列式属性的内容,请使用 REPLACE。
REPLACE 的工作方式类似于 INSERT,但如果旧文档与新文档具有相同的 ID,则在插入新文档之前,旧文档会被标记为已删除。请注意,旧文档不会从表中物理删除。删除只能在表的块合并时发生,例如,作为 OPTIMIZE 的结果。
UPDATE 和部分 REPLACE 都可以更改字段的值,但它们的操作方式不同:
UPDATE只能更改既非列式也非全文的字段。此过程是在原地完成的,通常比REPLACE更快。- 部分
REPLACE可以更改文档中的任何字段,但要求表中的所有字段都设置为“存储”(尽管这是默认设置)。UPDATE则不需要此要求。
REPLACE 的工作方式类似于 INSERT,但它在插入新文档之前,会将具有相同 ID 的先前文档标记为已删除。
如果您正在寻找原地更新,请参见 本节。
SQL REPLACE 语句的语法如下:
替换整个文档:
REPLACE INTO table [(column1, column2, ...)]
VALUES (value1, value2, ...)
[, (...)]
SQL 语句中未明确包含的列将被设置为其默认值,例如 0 或空字符串,具体取决于其数据类型。
仅替换选定字段:
REPLACE INTO table
SET field1=value1[, ..., fieldN=valueN]
WHERE id = <id>
注意,在此模式下,您只能按 id 进行过滤。
注意:部分替换需要 Manticore Buddy。如果不起作用,请确保已安装 Buddy。
关于 UPDATE 与部分 REPLACE 的更多信息,请参见 这里。
详情请参见示例。
-
/replace:POST /replace { "table": "<table name>", "id": <document id>, "doc": { "<field1>": <value1>, ... "<fieldN>": <valueN> } }/index是别名端点,功能相同。 -
类似 Elasticsearch 的端点
<table>/_doc/<id>:PUT/POST /<table name>/_doc/<id> { "<field1>": <value1>, ... "<fieldN>": <valueN> }注意:类似 Elasticsearch 的替换需要 Manticore Buddy。如果不起作用,请确保已安装 Buddy。
-
部分替换:
POST /<{table | cluster:table}>/_update/<id> { "<field1>": <value1>, ... "<fieldN>": <valueN> }<table name>可以是仅表名,也可以是cluster:table格式。如果需要,可以跨特定集群进行更新。注意:部分替换需要 Manticore Buddy。如果不起作用,请确保已安装 Buddy。
详情请参见示例。
- SQL
- REPLACE SET
- JSON
- Elasticsearch-like
- Elasticsearch-like partial
- Elasticsearch-like partial in cluster
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
REPLACE INTO products VALUES(1, "document one", 10);REPLACE INTO products SET description='HUAWEI Matebook 15', price=10 WHERE id = 55;POST /replace
-H "Content-Type: application/x-ndjson" -d '
{
"table":"products",
"id":1,
"doc":
{
"title":"product one",
"price":10
}
}
'注意:类似 Elasticsearch 的替换需要 Manticore Buddy。如果不起作用,请确保已安装 Buddy。
PUT /products/_doc/2
{
"title": "product two",
"price": 20
}
POST /products/_doc/3
{
"title": "product three",
"price": 10
}注意:部分替换需要 Manticore Buddy。如果不起作用,请确保已安装 Buddy。
POST /products/_update/55
{
"doc": {
"description": "HUAWEI Matebook 15",
"price": 10
}
}POST /cluster_name:products/_update/55
{
"doc": {
"description": "HUAWEI Matebook 15",
"price": 10
}
}$index->replaceDocument([
'title' => 'document one',
'price' => 10
],1);indexApi.replace({"table" : "products", "id" : 1, "doc" : {"title" : "document one","price":10}})await indexApi.replace({"table" : "products", "id" : 1, "doc" : {"title" : "document one","price":10}})res = await indexApi.replace({"table" : "products", "id" : 1, "doc" : {"title" : "document one","price":10}});docRequest = new InsertDocumentRequest();
HashMap<String,Object> doc = new HashMap<String,Object>(){{
put("title","document one");
put("price",10);
}};
docRequest.index("products").id(1L).setDoc(doc);
sqlresult = indexApi.replace(docRequest);Dictionary<string, Object> doc = new Dictionary<string, Object>();
doc.Add("title", "document one");
doc.Add("price", 10);
InsertDocumentRequest docRequest = new InsertDocumentRequest(index: "products", id: 1, doc: doc);
var sqlresult = indexApi.replace(docRequest);let mut doc = HashMap::new();
doc.insert("title".to_string(), serde_json::json!("document one"));
doc.insert("price".to_string(), serde_json::json!(10));
let insert_req = InsertDocumentRequest::new("products".to_string(), serde_json::json!(doc));
let insert_res = index_api.replace(insert_req).await;res = await indexApi.replace({
index: 'test',
id: 1,
doc: { content: 'Text 11', name: 'Doc 11', cat: 3 },
});replaceDoc := map[string]interface{} {"content": "Text 11", "name": "Doc 11", "cat": 3}
replaceRequest := manticoreclient.NewInsertDocumentRequest("test", replaceDoc)
replaceRequest.SetId(1)
res, _, _ := apiClient.IndexAPI.Replace(context.Background()).InsertDocumentRequest(*replaceRequest).Execute()Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec){
"table":"products",
"_id":1,
"created":false,
"result":"updated",
"status":200
}{
"_id":2,
"table":"products",
"_primary_term":1,
"_seq_no":0,
"_shards":{
"failed":0,
"successful":1,
"total":1
},
"_type":"_doc",
"_version":1,
"result":"updated"
}
{
"_id":3,
"table":"products",
"_primary_term":1,
"_seq_no":0,
"_shards":{
"failed":0,
"successful":1,
"total":1
},
"_type":"_doc",
"_version":1,
"result":"updated"
}{
"table":"products",
"updated":1
}{
"table":"products",
"updated":1
}Array(
[_index] => products
[_id] => 1
[created] => false
[result] => updated
[status] => 200
){'created': False,
'found': None,
'id': 1,
'table': 'products',
'result': 'updated'}{'created': False,
'found': None,
'id': 1,
'table': 'products',
'result': 'updated'}{"table":"products","_id":1,"result":"updated"}class SuccessResponse {
index: products
id: 1
created: false
result: updated
found: null
}class SuccessResponse {
index: products
id: 1
created: false
result: updated
found: null
}class SuccessResponse {
index: products
id: 1
created: false
result: updated
found: null
}{
"table":"test",
"_id":1,
"created":false
"result":"updated"
"status":200
}{
"table":"test",
"_id":1,
"created":false
"result":"updated"
"status":200
}REPLACE 可用于实时表和预选表。您不能替换普通表中的数据。
当您执行 REPLACE 时,先前的文档不会被删除,而是被标记为已删除,因此表的大小会增长,直到发生块合并。要强制块合并,请使用 OPTIMIZE 语句。
您可以一次替换多个文档。更多信息请查看 批量添加文档。
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
REPLACE INTO products(id,title,tag) VALUES (1, 'doc one', 10), (2,' doc two', 20);POST /bulk
-H "Content-Type: application/x-ndjson" -d '
{ "replace" : { "table" : "products", "id":1, "doc": { "title": "doc one", "tag" : 10 } } }
{ "replace" : { "table" : "products", "id":2, "doc": { "title": "doc two", "tag" : 20 } } }
'$index->replaceDocuments([
[
'id' => 1,
'title' => 'document one',
'tag' => 10
],
[
'id' => 2,
'title' => 'document one',
'tag' => 20
]
);indexApi = manticoresearch.IndexApi(client)
docs = [ \
{"replace": {"table" : "products", "id" : 1, "doc" : {"title" : "document one"}}}, \
{"replace": {"table" : "products", "id" : 2, "doc" : {"title" : "document two"}}} ]
api_resp = indexApi.bulk('\n'.join(map(json.dumps,docs)))indexApi = manticoresearch.IndexApi(client)
docs = [ \
{"replace": {"table" : "products", "id" : 1, "doc" : {"title" : "document one"}}}, \
{"replace": {"table" : "products", "id" : 2, "doc" : {"title" : "document two"}}} ]
api_resp = await indexApi.bulk('\n'.join(map(json.dumps,docs)))docs = [
{"replace": {"table" : "products", "id" : 1, "doc" : {"title" : "document one"}}},
{"replace": {"table" : "products", "id" : 2, "doc" : {"title" : "document two"}}} ];
res = await indexApi.bulk(docs.map(e=>JSON.stringify(e)).join('\n'));body = "{\"replace\": {\"index\" : \"products\", \"id\" : 1, \"doc\" : {\"title\" : \"document one\"}}}" +"\n"+
"{\"replace\": {\"index\" : \"products\", \"id\" : 2, \"doc\" : {\"title\" : \"document two\"}}}"+"\n" ;
indexApi.bulk(body);string body = "{\"replace\": {\"index\" : \"products\", \"id\" : 1, \"doc\" : {\"title\" : \"document one\"}}}" +"\n"+
"{\"replace\": {\"index\" : \"products\", \"id\" : 2, \"doc\" : {\"title\" : \"document two\"}}}"+"\n" ;
indexApi.Bulk(body);string body = r#"{"replace": {"index" : "products", "id" : 1, "doc" : {"title" : "document one"}}}
{"replace": {"index" : "products", "id" : 2, "doc" : {"title" : "document two"}}}
"#;
index_api.bulk(body).await;replaceDocs = [
{
replace: {
index: 'test',
id: 1,
doc: { content: 'Text 11', cat: 1, name: 'Doc 11' },
},
},
{
replace: {
index: 'test',
id: 2,
doc: { content: 'Text 22', cat: 9, name: 'Doc 22' },
},
},
];
res = await indexApi.bulk(
replaceDocs.map((e) => JSON.stringify(e)).join("\n")
);body := "{\"replace\": {\"index\": \"test\", \"id\": 1, \"doc\": {\"content\": \"Text 11\", \"name\": \"Doc 11\", \"cat\": 1 }}}" + "\n" +
"{\"replace\": {\"index\": \"test\", \"id\": 2, \"doc\": {\"content\": \"Text 22\", \"name\": \"Doc 22\", \"cat\": 9 }}}" +"\n";
res, _, _ := apiClient.IndexAPI.Bulk(context.Background()).Body(body).Execute()Query OK, 2 rows affected (0.00 sec){
"items":
[
{
"replace":
{
"table":"products",
"_id":1,
"created":false,
"result":"updated",
"status":200
}
},
{
"replace":
{
"table":"products",
"_id":2,
"created":false,
"result":"updated",
"status":200
}
}
],
"errors":false
}Array(
[items] =>
Array(
Array(
[_index] => products
[_id] => 2
[created] => false
[result] => updated
[status] => 200
)
Array(
[_index] => products
[_id] => 2
[created] => false
[result] => updated
[status] => 200
)
)
[errors => false
){'error': None,
'items': [{u'replace': {u'_id': 1,
u'table': u'products',
u'created': False,
u'result': u'updated',
u'status': 200}},
{u'replace': {u'_id': 2,
u'table': u'products',
u'created': False,
u'result': u'updated',
u'status': 200}}]}{'error': None,
'items': [{u'replace': {u'_id': 1,
u'table': u'products',
u'created': False,
u'result': u'updated',
u'status': 200}},
{u'replace': {u'_id': 2,
u'table': u'products',
u'created': False,
u'result': u'updated',
u'status': 200}}]}{"items":[{"replace":{"table":"products","_id":1,"created":false,"result":"updated","status":200}},{"replace":{"table":"products","_id":2,"created":false,"result":"updated","status":200}}],"errors":false}class BulkResponse {
items: [{replace={_index=products, _id=1, created=false, result=updated, status=200}}, {replace={_index=products, _id=2, created=false, result=updated, status=200}}]
error: null
additionalProperties: {errors=false}
}class BulkResponse {
items: [{replace={_index=products, _id=1, created=false, result=updated, status=200}}, {replace={_index=products, _id=2, created=false, result=updated, status=200}}]
error: null
additionalProperties: {errors=false}
}class BulkResponse {
items: [{replace={_index=products, _id=1, created=false, result=updated, status=200}}, {replace={_index=products, _id=2, created=false, result=updated, status=200}}]
error: null
additionalProperties: {errors=false}
}{
"items":
[
{
"replace":
{
"table":"test",
"_id":1,
"created":false,
"result":"updated",
"status":200
}
},
{
"replace":
{
"table":"test",
"_id":2,
"created":false,
"result":"updated",
"status":200
}
}
],
"errors":false
}{
"items":
[
{
"replace":
{
"table":"test",
"_id":1,
"created":false,
"result":"updated",
"status":200
}
},
{
"replace":
{
"table":"test",
"_id":2,
"created":false,
"result":"updated",
"status":200
}
}
],
"errors":false
}