≫ 更新文档
您可以通过更新或替换来修改 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 已安装。
从 SELECT 替换:
REPLACE INTO table
SELECT ... FROM source
REPLACE INTO table (column1, column2, column3)
SELECT ... FROM source
注意:此语法需要 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
- REPLACE ... SELECT
- 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;CREATE TABLE products_src (id int, title text, price float, category_id int);
CREATE TABLE products (id int, title text, price float, category_id int);
INSERT INTO products_src VALUES
(1, 'Notebook Stand', 45.00, 10),
(2, 'USB-C Hub', 79.90, 12),
(3, 'Wireless Mouse', 129.00, 10);
REPLACE INTO products_a (id, price)
SELECT id, price FROM products_src;
REPLACE INTO products_b
SELECT * FROM products_src;
REPLACE INTO products_c (id, title, category_id)
SELECT id, title, category_id
FROM products_src
WHERE price >= 100;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)Query OK, 3 rows 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
}UPDATE 命令用于更改指定表中现有文档的行式存储属性值。请注意,您无法更新全文字段或列式存储属性的内容。如果有此需求,请使用 REPLACE。
RT、PQ 和普通表均支持属性更新。只要属性存储在行式存储中,所有属性类型都可以更新。
请注意,文档 ID 无法更新。
需要特别注意的是,更新属性会禁用其二级索引。如果保持二级索引的连续性至关重要,请考虑完全或部分替换文档。
有关 UPDATE 与部分 REPLACE 的更多信息,请阅读此处。
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
UPDATE products SET enabled=0 WHERE id=10;POST /update
{
"table":"products",
"id":10,
"doc":
{
"enabled":0
}
}$index->updateDocument([
'enabled'=>0
],10);indexApi = api = manticoresearch.IndexApi(client)
indexApi.update({"table" : "products", "id" : 1, "doc" : {"price":10}})indexApi = api = manticoresearch.IndexApi(client)
await indexApi.update({"table" : "products", "id" : 1, "doc" : {"price":10}})res = await indexApi.update({"table" : "products", "id" : 1, "doc" : {"price":10}});UpdateDocumentRequest updateRequest = new UpdateDocumentRequest();
doc = new HashMap<String,Object >(){{
put("price",10);
}};
updateRequest.index("products").id(1L).setDoc(doc);
indexApi.update(updateRequest);Dictionary<string, Object> doc = new Dictionary<string, Object>();
doc.Add("price", 10);
UpdateDocumentRequest updateRequest = new UpdateDocumentRequest(index: "products", id: 1, doc: doc);
indexApi.Update(updateRequest);let mut doc = HashMap::new();
doc.insert("price".to_string(), serde_json::json!(10));
let update_req = UpdateDocumentRequest {
table: serde_json::json!("products"),
doc: serde_json::json!(doc),
id: serde_json::json!(1),
..Default::default(),
};
let update_res = index_api.update(update_req).await;res = await indexApi.update({ index: "test", id: 1, doc: { cat: 10 } });updateDoc = map[string]interface{} {"cat":10}
updateRequest = openapiclient.NewUpdateDocumentRequest("test", updateDoc)
updateRequest.SetId(1)
res, _, _ = apiClient.IndexAPI.Update(context.Background()).UpdateDocumentRequest(*updateRequest).Execute()Query OK, 1 row affected (0.00 sec){
"table":"products",
"updated":1
}Array(
[_index] => products
[_id] => 10
[result] => updated
){'id': 1, 'table': 'products', 'result': 'updated', 'updated': None}{'id': 1, 'table': 'products', 'result': 'updated', 'updated': None}{"table":"products","_id":1,"result":"updated"}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}{
"table":"test",
"_id":1,
"result":"updated"
}{
"table":"test",
"_id":1,
"result":"updated"
}可以在单个语句中更新多个属性。例如:
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
UPDATE products
SET price=100000000000,
coeff=3465.23,
tags1=(3,6,4),
tags2=()
WHERE MATCH('phone') AND enabled=1;POST /update
{
"table":"products",
"doc":
{
"price":100000000000,
"coeff":3465.23,
"tags1":[3,6,4],
"tags2":[]
},
"query":
{
"match": { "*": "phone" },
"equals": { "enabled": 1 }
}
}$query= new BoolQuery();
$query->must(new Match('phone','*'));
$query->must(new Equals('enabled',1));
$index->updateDocuments([
'price' => 100000000000,
'coeff' => 3465.23,
'tags1' => [3,6,4],
'tags2' => []
],
$query
);indexApi = api = manticoresearch.IndexApi(client)
indexApi.update({"table" : "products", "id" : 1, "doc" : {
"price": 100000000000,
"coeff": 3465.23,
"tags1": [3,6,4],
"tags2": []}})indexApi = api = manticoresearch.IndexApi(client)
await indexApi.update({"table" : "products", "id" : 1, "doc" : {
"price": 100000000000,
"coeff": 3465.23,
"tags1": [3,6,4],
"tags2": []}})res = await indexApi.update({"table" : "products", "id" : 1, "doc" : {
"price": 100000000000,
"coeff": 3465.23,
"tags1": [3,6,4],
"tags2": []}});UpdateDocumentRequest updateRequest = new UpdateDocumentRequest();
doc = new HashMap<String,Object >(){{
put("price",10);
put("coeff",3465.23);
put("tags1",new int[]{3,6,4});
put("tags2",new int[]{});
}};
updateRequest.index("products").id(1L).setDoc(doc);
indexApi.update(updateRequest);Dictionary<string, Object> doc = new Dictionary<string, Object>();
doc.Add("price", 10);
doc.Add("coeff", 3465.23);
doc.Add("tags1", new List<int> {3,6,4});
doc.Add("tags2", new List<int> {});
UpdateDocumentRequest updateRequest = new UpdateDocumentRequest(index: "products", id: 1, doc: doc);
indexApi.Update(updateRequest);let mut doc = HashMap::new();
doc.insert("price".to_string(), serde_json::json!(10));
doc.insert("coeff".to_string(), serde_json::json!(3465.23));
doc.insert("tags1".to_string(), serde_json::json!([3,6,4]));
doc.insert("tags2".to_string(), serde_json::json!([]));
let update_req = UpdateDocumentRequest {
table: serde_json::json!("products"),
doc: serde_json::json!(doc),
id: serde_json::json!(1),
..Default::default(),
};
let update_res = index_api.update(update_req).await;res = await indexApi.update({ index: "test", id: 1, doc: { name: "Doc 21", cat: "10" } });updateDoc = map[string]interface{} {"name":"Doc 21", "cat":10}
updateRequest = manticoreclient.NewUpdateDocumentRequest("test", updateDoc)
updateRequest.SetId(1)
res, _, _ = apiClient.IndexAPI.Update(context.Background()).UpdateDocumentRequest(*updateRequest).Execute()Query OK, 148 rows affected (0.0 sec){
"table":"products",
"updated":148
}Array(
[_index] => products
[updated] => 148
){'id': 1, 'table': 'products', 'result': 'updated', 'updated': None}{'id': 1, 'table': 'products', 'result': 'updated', 'updated': None}{"table":"products","_id":1,"result":"updated"}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}{
"table":"test",
"_id":1,
"result":"updated"
}{
"table":"test",
"_id":1,
"result":"updated"
}当为 32 位属性分配超出范围的值时,这些值将被静默截断为其低 32 位。例如,如果您尝试用值 4294967297 更新 32 位无符号整数,实际存储的值将是 1,因为 4294967297(十六进制为 0x100000001)的低 32 位等于 1(十六进制为 0x00000001)。
UPDATE 可用于对数值数据类型或数值数据类型的数组执行部分 JSON 更新。只需确保不要用浮点值更新整数值,因为它会被四舍五入。
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
insert into products (id, title, meta) values (1,'title','{"tags":[1,2,3]}');
update products set meta.tags[0]=100 where id=1;POST /insert
{
"table":"products",
"id":100,
"doc":
{
"title":"title",
"meta": {
"tags":[1,2,3]
}
}
}
POST /update
{
"table":"products",
"id":100,
"doc":
{
"meta.tags[0]":100
}
}$index->insertDocument([
'title' => 'title',
'meta' => ['tags' => [1,2,3]]
],1);
$index->updateDocument([
'meta.tags[0]' => 100
],1);indexApi = api = manticoresearch.IndexApi(client)
indexApi.update({"table" : "products", "id" : 1, "doc" : {
"meta.tags[0]": 100}})indexApi = api = manticoresearch.IndexApi(client)
await indexApi.update({"table" : "products", "id" : 1, "doc" : {
"meta.tags[0]": 100}})res = await indexApi.update({"table" : "products", "id" : 1, "doc" : {
"meta.tags[0]": 100}});UpdateDocumentRequest updateRequest = new UpdateDocumentRequest();
doc = new HashMap<String,Object >(){{
put("meta.tags[0]",100);
}};
updateRequest.index("products").id(1L).setDoc(doc);
indexApi.update(updateRequest);Dictionary<string, Object> doc = new Dictionary<string, Object>();
doc.Add("meta.tags[0]", 100);
UpdateDocumentRequest updateRequest = new UpdateDocumentRequest(index: "products", id: 1, doc: doc);
indexApi.Update(updateRequest);let mut doc = HashMap::new();
doc.insert("meta.tags[0]".to_string(), serde_json::json!(100));
let update_req = UpdateDocumentRequest {
table: serde_json::json!("products"),
doc: serde_json::json!(doc),
id: serde_json::json!(1),
..Default::default(),
};
let update_res = index_api.update(update_req).await;res = await indexApi.update({"table" : "test", "id" : 1, "doc" : { "meta.tags[0]": 100} });updateDoc = map[string]interface{} {"meta.tags[0]":100}
updateRequest = manticoreclient.NewUpdateDocumentRequest("test", updateDoc)
updateRequest.SetId(1)
res, _, _ = apiClient.IndexAPI.Update(context.Background()).UpdateDocumentRequest(*updateRequest).Execute()Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec){
"table":"products",
"_id":100,
"created":true,
"result":"created",
"status":201
}
{
"table":"products",
"updated":1
}Array(
[_index] => products
[_id] => 1
[created] => true
[result] => created
)
Array(
[_index] => products
[updated] => 1
){'id': 1, 'table': 'products', 'result': 'updated', 'updated': None}{'id': 1, 'table': 'products', 'result': 'updated', 'updated': None}{"table":"products","_id":1,"result":"updated"}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}{"table":"test","_id":1,"result":"updated"}{
"table":"test",
"_id":1,
"result":"updated"
}更新其他数据类型或更改 JSON 属性中的属性类型需要进行完整的 JSON 更新。
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
insert into products values (1,'title','{"tags":[1,2,3]}');
update products set data='{"tags":["one","two","three"]}' where id=1;POST /insert
{
"table":"products",
"id":1,
"doc":
{
"title":"title",
"data":"{\"tags\":[1,2,3]}"
}
}
POST /update
{
"table":"products",
"id":1,
"doc":
{
"data":"{\"tags\":[\"one\",\"two\",\"three\"]}"
}
}$index->insertDocument([
'title'=> 'title',
'data' => [
'tags' => [1,2,3]
]
],1);
$index->updateDocument([
'data' => [
'one', 'two', 'three'
]
],1);indexApi.insert({"table" : "products", "id" : 100, "doc" : {"title" : "title", "meta" : {"tags":[1,2,3]}}})
indexApi.update({"table" : "products", "id" : 100, "doc" : {"meta" : {"tags":['one','two','three']}}})await indexApi.insert({"table" : "products", "id" : 100, "doc" : {"title" : "title", "meta" : {"tags":[1,2,3]}}})
await indexApi.update({"table" : "products", "id" : 100, "doc" : {"meta" : {"tags":['one','two','three']}}})res = await indexApi.insert({"table" : "products", "id" : 100, "doc" : {"title" : "title", "meta" : {"tags":[1,2,3]}}});
res = await indexApi.update({"table" : "products", "id" : 100, "doc" : {"meta" : {"tags":['one','two','three']}}});InsertDocumentRequest newdoc = new InsertDocumentRequest();
doc = new HashMap<String,Object>(){{
put("title","title");
put("meta",
new HashMap<String,Object>(){{
put("tags",new int[]{1,2,3});
}});
}};
newdoc.index("products").id(100L).setDoc(doc);
indexApi.insert(newdoc);
updatedoc = new UpdateDocumentRequest();
doc = new HashMap<String,Object >(){{
put("meta",
new HashMap<String,Object>(){{
put("tags",new String[]{"one","two","three"});
}});
}};
updatedoc.index("products").id(100L).setDoc(doc);
indexApi.update(updatedoc);Dictionary<string, Object> meta = new Dictionary<string, Object>();
meta.Add("tags", new List<int> {1,2,3});
Dictionary<string, Object> doc = new Dictionary<string, Object>();
doc.Add("title", "title");
doc.Add("meta", meta);
InsertDocumentRequest newdoc = new InsertDocumentRequest(index: "products", id: 100, doc: doc);
indexApi.Insert(newdoc);
meta = new Dictionary<string, Object>();
meta.Add("tags", new List<string> {"one","two","three"});
doc = new Dictionary<string, Object>();
doc.Add("meta", meta);
UpdateDocumentRequest updatedoc = new UpdateDocumentRequest(index: "products", id: 100, doc: doc);
indexApi.Update(updatedoc);let mut meta = HashMap::new();
meta.insert("tags".to_string(), serde_json::json!([1,2,3]));
let mut doc = HashMap::new();
doc.insert("title".to_string(), serde_json::json!("title"));
doc.insert("meta".to_string(), serde_json::json!(meta));
let insert_req = InsertDocumentRequest {
table: serde_json::json!("products"),
doc: serde_json::json!(doc),
id: serde_json::json!(100),
..Default::default(),
};
let insert_res = index_api.insert(insert_req).await;
meta = HashMap::new();
meta.insert("tags".to_string(), serde_json::json!(["one","two","three"]));
doc = HashMap::new();
doc.insert("meta".to_string(), serde_json::json!(meta));
let update_req = UpdateDocumentRequest {
table: serde_json::json!("products"),
doc: serde_json::json!(doc),
id: serde_json::json!(100),
..Default::default(),
};
let update_res = index_api.update(update_req).await;res = await indexApi.insert({
index: 'test',
id: 1,
doc: { content: 'Text 1', name: 'Doc 1', meta: { tags:[1,2,3] } }
})
res = await indexApi.update({ index: 'test', id: 1, doc: { meta: { tags:['one','two','three'] } } });metaField := map[string]interface{} {"tags": []int{1, 2, 3}}
insertDoc := map[string]interface{} {"name": "Doc 1", "meta": metaField}}
insertRequest := manticoreclient.NewInsertDocumentRequest("test", insertDoc)
insertRequest.SetId(1)
res, _, _ := apiClient.IndexAPI.Insert(context.Background()).InsertDocumentRequest(*insertRequest).Execute();
metaField = map[string]interface{} {"tags": []string{"one", "two", "three"}}
updateDoc := map[string]interface{} {"meta": metaField}
updateRequest := manticoreclient.NewUpdateDocumentRequest("test", updateDoc)
res, _, _ = apiClient.IndexAPI.Update(context.Background()).UpdateDocumentRequest(*updateRequest).Execute()Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec){
"table":"products",
"updated":1
}Array(
[_index] => products
[_id] => 1
[created] => true
[result] => created
)
Array(
[_index] => products
[updated] => 1
){'created': True,
'found': None,
'id': 100,
'table': 'products',
'result': 'created'}
{'id': 100, 'table': 'products', 'result': 'updated', 'updated': None}{'created': True,
'found': None,
'id': 100,
'table': 'products',
'result': 'created'}
{'id': 100, 'table': 'products', 'result': 'updated', 'updated': None}{"table":"products","_id":100,"created":true,"result":"created"}
{"table":"products","_id":100,"result":"updated"}class SuccessResponse {
index: products
id: 100
created: true
result: created
found: null
}
class UpdateResponse {
index: products
updated: null
id: 100
result: updated
}class SuccessResponse {
index: products
id: 100
created: true
result: created
found: null
}
class UpdateResponse {
index: products
updated: null
id: 100
result: updated
}class SuccessResponse {
index: products
id: 100
created: true
result: created
found: null
}
class UpdateResponse {
index: products
updated: null
id: 100
result: updated
}{
"table":"test",
"_id":1,
"created":true,
"result":"created"
}
{
"table":"test",
"_id":1,
"result":"updated"
}{
"table":"test",
"_id":1,
"created":true,
"result":"created"
}
{
"table":"test",
"_id":1,
"result":"updated"
}当使用复制时,表名应在 SQL 中以 cluster_name: 前缀,以便更新将传播到集群中的所有节点。对于通过 HTTP 的查询,应设置一个 cluster 属性。有关更多详细信息,请参阅 设置复制。
{
"cluster":"nodes4",
"table":"test",
"id":1,
"doc":
{
"gid" : 100,
"price" : 1000
}
}
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
update weekly:posts set enabled=0 where id=1;POST /update
{
"cluster":"weekly",
"table":"products",
"id":1,
"doc":
{
"enabled":0
}
}$index->setName('products')->setCluster('weekly');
$index->updateDocument(['enabled'=>0],1);indexApi.update({"cluster":"weekly", "table" : "products", "id" : 1, "doc" : {"enabled" : 0}})await indexApi.update({"cluster":"weekly", "table" : "products", "id" : 1, "doc" : {"enabled" : 0}})res = wait indexApi.update({"cluster":"weekly", "table" : "products", "id" : 1, "doc" : {"enabled" : 0}});updatedoc = new UpdateDocumentRequest();
doc = new HashMap<String,Object >(){{
put("enabled",0);
}};
updatedoc.index("products").cluster("weekly").id(1L).setDoc(doc);
indexApi.update(updatedoc);Dictionary<string, Object> doc = new Dictionary<string, Object>();
doc.Add("enabled", 0);
UpdateDocumentRequest updatedoc = new UpdateDocumentRequest(index: "products", cluster: "weekly", id: 1, doc: doc);
indexApi.Update(updatedoc);let mut doc = HashMap::new();
doc.insert("enabled".to_string(), serde_json::json!(0));
let update_req = UpdateDocumentRequest {
table: serde_json::json!("products"),
cluster: serde_json::json!("weekly"),
doc: serde_json::json!(doc),
id: serde_json::json!(1),
};
let update_res = index_api.update(update_req).await;res = wait indexApi.update( {cluster: 'test_cluster', index : 'test', id : 1, doc : {name : 'Doc 11'}} );updateDoc = map[string]interface{} {"name":"Doc 11"}
updateRequest = manticoreclient.NewUpdateDocumentRequest("test", updateDoc)
updateRequest.SetCluster("test_cluster")
updateRequest.SetId(1)
res, _, _ = apiClient.IndexAPI.Update(context.Background()).UpdateDocumentRequest(*updateRequest).Execute()以下是 SQL UPDATE 语句的语法:
UPDATE table SET col1 = newval1 [, ...] WHERE where_condition [OPTION opt_name = opt_value [, ...]] [FORCE|IGNORE INDEX(id)]
where_condition 的语法与 SELECT 语句相同。
多值属性值集必须作为逗号分隔的列表在括号中指定。要从多值属性中删除所有值,只需将其设置为 ()。
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
UPDATE products SET tags1=(3,6,4) WHERE id=1;
UPDATE products SET tags1=() WHERE id=1;POST /update
{
"table":"products",
"_id":1,
"doc":
{
"tags1": []
}
}$index->updateDocument(['tags1'=>[]],1);indexApi.update({"table" : "products", "id" : 1, "doc" : {"tags1": []}})await indexApi.update({"table" : "products", "id" : 1, "doc" : {"tags1": []}})indexApi.update({"table" : "products", "id" : 1, "doc" : {"tags1": []}})updatedoc = new UpdateDocumentRequest();
doc = new HashMap<String,Object >(){{
put("tags1",new int[]{});
}};
updatedoc.index("products").id(1L).setDoc(doc);
indexApi.update(updatedoc);Dictionary<string, Object> doc = new Dictionary<string, Object>();
doc.Add("tags1", new List<int> {});
UpdateDocumentRequest updatedoc = new UpdateDocumentRequest(index: "products", id: 1, doc: doc);
indexApi.Update(updatedoc);let mut doc = HashMap::new();
doc.insert("tags1".to_string(), serde_json::json!([]));
let update_req = UpdateDocumentRequest {
table: serde_json::json!("products"),
doc: serde_json::json!(doc),
id: serde_json::json!(1),
..Default::default(),
};
let update_res = index_api.update(update_req).await;res = await indexApi.update({ index: 'test', id: 1, doc: { cat: 10 } });updateDoc = map[string]interface{} {"cat":10}
updateRequest = manticoreclient.NewUpdateDocumentRequest("test", updateDoc)
updateRequest.SetId(1)
res, _, _ = apiClient.IndexAPI.Update(context.Background()).UpdateDocumentRequest(*updateRequest).Execute()Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec){
"table":"products",
"updated":1
}Array(
[_index] => products
[updated] => 1
){'id': 1, 'table': 'products', 'result': 'updated', 'updated': None}{'id': 1, 'table': 'products', 'result': 'updated', 'updated': None}{"table":"products","_id":1,"result":"updated"}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}class UpdateResponse {
index: products
updated: null
id: 1
result: updated
}{
"table":"test",
"_id":1,
"result":"updated"
}{
"table":"test",
"_id":1,
"result":"updated"
}OPTION 子句是 Manticore 特定的扩展,允许您控制每次更新的选项。语法如下:
OPTION <optionname>=<value> [ , ... ]
选项与 SELECT 语句相同。特别是对于 UPDATE 语句,您可以使用这些选项:
- 'ignore_nonexistent_columns' - 如果设置为 1,则表示更新将静默忽略任何关于尝试更新当前表模式中不存在的列的警告。默认值为 0。
- 'strict' - 此选项用于部分 JSON 属性更新。默认情况下(strict=1),
UPDATE将在UPDATE查询尝试对非数值属性进行更新时导致错误。strict=0 时,如果多个属性被更新且某些属性不允许更新,则UPDATE将不会导致错误,而是仅对允许的属性进行更改(其余属性将被忽略)。如果UPDATE的SET变化中没有任何允许的更改,则即使 strict=0,命令也会导致错误。
在极少数情况下,Manticore 的内置查询分析器可能无法正确理解查询并确定是否应使用 ID 表。这可能导致类似 UPDATE ... WHERE id = 123 的查询性能不佳。
有关如何强制优化器使用 docid 索引的信息,请参阅 查询优化器提示。
使用 HTTP JSON 协议的更新是通过 /update 端点执行的。语法类似于 /insert 端点,但这次 doc 属性是必需的。
服务器将返回一个 JSON 对象,说明操作是否成功。
- JSON
POST /update
{
"table":"test",
"id":1,
"doc":
{
"gid" : 100,
"price" : 1000
}
}{
"table": "test",
"_id": 1,
"result": "updated"
}需要更新的文档的 ID 可以直接使用 id 属性设置,如上一个示例所示,或者可以通过查询更新文档,并将更新应用于匹配查询的所有文档:
- JSON
POST /update
{
"table":"test",
"doc":
{
"price" : 1000
},
"query":
{
"match": { "*": "apple" }
}
}{
"table":"products",
"updated":1
}查询语法与 /search 端点 相同。请注意,您不能同时指定 id 和 query。
FLUSH ATTRIBUTES
The FLUSH ATTRIBUTES 命令确保所有活动表中的内存属性更新都被刷新到磁盘。它返回一个标识结果磁盘状态的标签,该状态代表自服务器启动以来实际执行的磁盘属性保存次数。
mysql> UPDATE testindex SET channel_id=1107025 WHERE id=1;
Query OK, 1 row affected (0.04 sec)
mysql> FLUSH ATTRIBUTES;
+------+
| tag |
+------+
| 1 |
+------+
1 row in set (0.19 sec)
参见 attr_flush_period 设置。
您可以使用 /bulk 端点在一个调用中执行多个更新操作。此端点仅适用于 Content-Type 设置为 application/x-ndjson 的数据。数据应格式化为新行分隔的 JSON(NDJSON)。这意味着每行应包含恰好一个 JSON 语句,并以新行 \n 结尾,可能还有 \r。
- JSON
POST /bulk
{ "update" : { "table" : "products", "id" : 1, "doc": { "price" : 10 } } }
{ "update" : { "table" : "products", "id" : 2, "doc": { "price" : 20 } } }{
"items":
[
{
"update":
{
"table":"products",
"_id":1,
"result":"updated"
}
},
{
"update":
{
"table":"products",
"_id":2,
"result":"updated"
}
}
],
"errors":false
}/bulk 端点支持插入、替换和删除。每个语句以动作类型开始(在这种情况下,update)。以下是受支持的动作列表:
insert:插入文档。语法与 /insert 端点 相同。create:insert的同义词replace:替换文档。语法与 /replace 相同。index:replace的同义词update:更新文档。语法与 /update 相同。delete:删除文档。语法与 /delete 端点 相同。
还支持通过查询进行更新和通过查询进行删除。
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
POST /bulk
{ "update" : { "table" : "products", "doc": { "coeff" : 1000 }, "query": { "range": { "price": { "gte": 1000 } } } } }
{ "update" : { "table" : "products", "doc": { "coeff" : 0 }, "query": { "range": { "price": { "lt": 1000 } } } } }$client->bulk([
['update'=>[
'table' => 'products',
'doc' => [
'coeff' => 100
],
'query' => [
'range' => ['price'=>['gte'=>1000]]
]
]
],
['update'=>[
'table' => 'products',
'doc' => [
'coeff' => 0
],
'query' => [
'range' => ['price'=>['lt'=>1000]]
]
]
]
]);docs = [ \
{ "update" : { "table" : "products", "doc": { "coeff" : 1000 }, "query": { "range": { "price": { "gte": 1000 } } } } }, \
{ "update" : { "table" : "products", "doc": { "coeff" : 0 }, "query": { "range": { "price": { "lt": 1000 } } } } } ]
indexApi.bulk('\n'.join(map(json.dumps,docs)))docs = [ \
{ "update" : { "table" : "products", "doc": { "coeff" : 1000 }, "query": { "range": { "price": { "gte": 1000 } } } } }, \
{ "update" : { "table" : "products", "doc": { "coeff" : 0 }, "query": { "range": { "price": { "lt": 1000 } } } } } ]
await indexApi.bulk('\n'.join(map(json.dumps,docs)))docs = [
{ "update" : { "table" : "products", "doc": { "coeff" : 1000 }, "query": { "range": { "price": { "gte": 1000 } } } } },
{ "update" : { "table" : "products", "doc": { "coeff" : 0 }, "query": { "range": { "price": { "lt": 1000 } } } } } ];
res = await indexApi.bulk(docs.map(e=>JSON.stringify(e)).join('\n'));String body = "{ \"update\" : { \"index\" : \"products\", \"doc\": { \"coeff\" : 1000 }, \"query\": { \"range\": { \"price\": { \"gte\": 1000 } } } }} "+"\n"+
"{ \"update\" : { \"index\" : \"products\", \"doc\": { \"coeff\" : 0 }, \"query\": { \"range\": { \"price\": { \"lt\": 1000 } } } } }"+"\n";
indexApi.bulk(body);string body = "{ \"update\" : { \"index\" : \"products\", \"doc\": { \"coeff\" : 1000 }, \"query\": { \"range\": { \"price\": { \"gte\": 1000 } } } }} "+"\n"+
"{ \"update\" : { \"index\" : \"products\", \"doc\": { \"coeff\" : 0 }, \"query\": { \"range\": { \"price\": { \"lt\": 1000 } } } } }"+"\n";
indexApi.Bulk(body);string body = r#"{ "update" : { "index" : "products", "doc": { "coeff" : 1000 }, "query": { "range": { "price": { "gte": 1000 } } } }}
{ "update" : { "index" : "products", "doc": { "coeff" : 0 }, "query": { "range": { "price": { "lt": 1000 } } } } }
"#;
index_api.bulk(body).await;updateDocs = [
{
update: {
index: 'test',
id: 1,
doc: { content: 'Text 11', cat: 1, name: 'Doc 11' },
},
},
{
update: {
index: 'test',
id: 2,
doc: { content: 'Text 22', cat: 9, name: 'Doc 22' },
},
},
];
res = await indexApi.bulk(
updateDocs.map((e) => JSON.stringify(e)).join("\n")
);body := "{\"update\": {\"index\": \"test\", \"id\": 1, \"doc\": {\"content\": \"Text 11\", \"name\": \"Doc 11\", \"cat\": 1 }}}" + "\n" +
"{\"update\": {\"index\": \"test\", \"id\": 2, \"doc\": {\"content\": \"Text 22\", \"name\": \"Doc 22\", \"cat\": 9 }}}" +"\n";
res, _, _ := apiClient.IndexAPI.Bulk(context.Background()).Body(body).Execute(){
"items":
[
{
"update":
{
"table":"products",
"updated":1
}
},
{
"update":
{
"table":"products",
"updated":3
}
}
],
"errors":false
}Array(
[items] => Array (
Array(
[update] => Array(
[_index] => products
[updated] => 1
)
)
Array(
[update] => Array(
[_index] => products
[updated] => 3
)
)
){'error': None,
'items': [{u'update': {u'table': u'products', u'updated': 1}},
{u'update': {u'table': u'products', u'updated': 3}}]}{'error': None,
'items': [{u'update': {u'table': u'products', u'updated': 1}},
{u'update': {u'table': u'products', u'updated': 3}}]}{"items":[{"update":{"table":"products","updated":1}},{"update":{"table":"products","updated":3}}],"errors":false}class BulkResponse {
items: [{update={_index=products, _id=1, created=false, result=updated, status=200}}, {update={_index=products, _id=2, created=false, result=updated, status=200}}]
error: null
additionalProperties: {errors=false}
}class BulkResponse {
items: [{update={_index=products, _id=1, created=false, result=updated, status=200}}, {update={_index=products, _id=2, created=false, result=updated, status=200}}]
error: null
additionalProperties: {errors=false}
}class BulkResponse {
items: [{update={_index=products, _id=1, created=false, result=updated, status=200}}, {update={_index=products, _id=2, created=false, result=updated, status=200}}]
error: null
additionalProperties: {errors=false}
}{
"items":
[
{
"update":
{
"table":"test",
"updated":1
}
},
{
"update":
{
"table":"test",
"updated":1
}
}
],
"errors":false
}{
"items":
[
{
"update":
{
"table":"test",
"updated":1
}
},
{
"update":
{
"table":"test",
"updated":1
}
}
],
"errors":false
}请记住,如果批量操作在导致错误的第一个查询处停止。
attr_update_reserve=size
attr_update_reserve 是一个针对每个表的设置,用于确定为二进制大对象属性更新保留的空间。此设置是可选的,默认值为 128k。
当二进制大对象属性(MVA、字符串、JSON)更新时,它们的长度可能会改变。如果更新后的字符串(或 MVA 或 JSON)比旧字符串短,则会覆盖 .spb 文件中的旧字符串。但是,如果更新后的字符串更长,则更新将写入 .spb 文件的末尾。此文件是内存映射的,这意味着调整其大小可能是一个相当慢的过程,具体取决于操作系统实现的内存映射文件。
为了避免频繁调整大小,可以使用此选项指定在 .spb 文件末尾保留的额外空间。
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
- TypeScript
- Go
- CONFIG
create table products(title text, price float) attr_update_reserve = '1M'POST /cli -d "
create table products(title text, price float) attr_update_reserve = '1M'"$params = [
'body' => [
'settings' => [
'attr_update_reserve' => '1M'
],
'columns' => [
'title'=>['type'=>'text'],
'price'=>['type'=>'float']
]
],
'table' => 'products'
];
$index = new \Manticoresearch\Index($client);
$index->create($params);utilsApi.sql('create table products(title text, price float) attr_update_reserve = \'1M\'')await utilsApi.sql('create table products(title text, price float) attr_update_reserve = \'1M\'')res = await utilsApi.sql('create table products(title text, price float) attr_update_reserve = \'1M\'');utilsApi.sql("create table products(title text, price float) attr_update_reserve = '1M'", true);utilsApi.Sql("create table products(title text, price float) attr_update_reserve = '1M'", true);utils_api.sql("create table products(title text, price float) attr_update_reserve = '1M'", Some(true)).await;utilsApi.sql("create table test(content text, name string, cat int) attr_update_reserve = '1M'");apiClient.UtilsAPI.Sql(context.Background()).Body("create table test(content text, name string, cat int) attr_update_reserve = '1M'").Execute()table products {
attr_update_reserve = 1M
type = rt
path = tbl
rt_field = title
rt_attr_uint = price
}attr_flush_period = 900 # persist updates to disk every 15 minutes
在更新属性时,更改首先被写入属性的内存副本。此设置允许设置将更新刷新到磁盘之间的间隔。默认值为 0,这禁用了周期性刷新,但刷新仍会在正常关闭时发生。