UPDATE changes row-wise attribute values of existing documents in a specified table with new values. Note that you can’t update the contents of a fulltext field or a columnar attribute. If there’s such a need, use REPLACE.
Attribute updates are supported for RT, PQ, and plain tables. All attribute types can be updated as long as they are stored in the traditional row-wise storage.
Note that the document ID cannot be updated.
Note that when you update an attribute, its secondary index gets disabled, so consider replacing the document instead.
UPDATE products SET enabled=0 WHERE id=10;
Query OK, 1 row affected (0.00 sec)
POST /update
{
"index":"products",
"id":10,
"doc":
{
"enabled":0
}
}
{
"_index":"products",
"updated":1
}
$index->updateDocument([
'enabled'=>0
],10);
Array(
[_index] => products
[_id] => 10
[result] => updated
)
= api = manticoresearch.IndexApi(client)
indexApi "index" : "products", "id" : 1, "doc" : {"price":10}}) indexApi.update({
'id': 1, 'index': 'products', 'result': 'updated', 'updated': None} {
= await indexApi.update({"index" : "products", "id" : 1, "doc" : {"price":10}}); res
"_index":"products","_id":1,"result":"updated"} {
new UpdateDocumentRequest();
UpdateDocumentRequest updateRequest = new HashMap<String,Object >(){{
doc = put("price",10);
}};index("products").id(1L).setDoc(doc);
updateRequest.update(updateRequest); indexApi.
class UpdateResponse {
index: productsnull
updated: 1
id:
result: updated }
Multiple attributes can be updated in a single statement. Example:
UPDATE products
SET price=100000000000,
=3465.23,
coeff=(3,6,4),
tags1=()
tags2WHERE MATCH('phone') AND enabled=1;
Query OK, 148 rows affected (0.0 sec)
POST /update
{
"index":"products",
"doc":
{
"price":100000000000,
"coeff":3465.23,
"tags1":[3,6,4],
"tags2":[]
},
"query":
{
"match": { "*": "phone" },
"equals": { "enabled": 1 }
}
}
{
"_index":"products",
"updated":148
}
$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
);
Array(
[_index] => products
[updated] => 148
)
= api = manticoresearch.IndexApi(client)
indexApi "index" : "products", "id" : 1, "doc" : {
indexApi.update({"price": 100000000000,
"coeff": 3465.23,
"tags1": [3,6,4],
"tags2": []}})
'id': 1, 'index': 'products', 'result': 'updated', 'updated': None} {
= await indexApi.update({"index" : "products", "id" : 1, "doc" : {
res "price": 100000000000,
"coeff": 3465.23,
"tags1": [3,6,4],
"tags2": []}});
"_index":"products","_id":1,"result":"updated"} {
new UpdateDocumentRequest();
UpdateDocumentRequest updateRequest = new HashMap<String,Object >(){{
doc = put("price",10);
put("coeff",3465.23);
put("tags1",new int[]{3,6,4});
put("tags2",new int[]{});
}};index("products").id(1L).setDoc(doc);
updateRequest.update(updateRequest); indexApi.
class UpdateResponse {
index: productsnull
updated: 1
id:
result: updated }
When assigning out-of-range values to 32-bit attributes, they will be trimmed to their lower 32 bits without a prompt. For example, if you try to update the 32-bit unsigned int with a value of 4294967297, the value of 1 will actually be stored, because the lower 32 bits of 4294967297 (0x100000001 in hex) amount to 1 (0x00000001 in hex).
UPDATE
can be used to perform partial JSON updates on numeric data types or arrays of numeric data types. Just make sure you don’t update an integer value with a float value as it will be rounded off.
insert into products (id, title, meta) values (1,'title','{"tags":[1,2,3]}');
update products set meta.tags[0]=100 where id=1;
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
POST /insert
{
"index":"products",
"id":100,
"doc":
{
"title":"title",
"meta": {
"tags":[1,2,3]
}
}
}
POST /update
{
"index":"products",
"id":100,
"doc":
{
"meta.tags[0]":100
}
}
{
"_index":"products",
"_id":100,
"created":true,
"result":"created",
"status":201
}
{
"_index":"products",
"updated":1
}
$index->insertDocument([
'title' => 'title',
'meta' => ['tags' => [1,2,3]]
],1);
$index->updateDocument([
'meta.tags[0]' => 100
],1);
Array(
[_index] => products
[_id] => 1
[created] => true
[result] => created
)
Array(
[_index] => products
[updated] => 1
)
= api = manticoresearch.IndexApi(client)
indexApi "index" : "products", "id" : 1, "doc" : {
indexApi.update({"meta.tags[0]": 100}})
'id': 1, 'index': 'products', 'result': 'updated', 'updated': None} {
= await indexApi.update({"index" : "products", "id" : 1, "doc" : {
res "meta.tags[0]": 100}});
"_index":"products","_id":1,"result":"updated"} {
new UpdateDocumentRequest();
UpdateDocumentRequest updateRequest = new HashMap<String,Object >(){{
doc = put("meta.tags[0]",100);
}};index("products").id(1L).setDoc(doc);
updateRequest.update(updateRequest); indexApi.
class UpdateResponse {
index: productsnull
updated: 1
id:
result: updated }
Updating other data types or changing property type in a JSON attribute requires a full JSON update.
insert into products values (1,'title','{"tags":[1,2,3]}');
update products set data='{"tags":["one","two","three"]}' where id=1;
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
POST /insert
{
"index":"products",
"id":1,
"doc":
{
"title":"title",
"data":"{\"tags\":[1,2,3]}"
}
}
POST /update
{
"index":"products",
"id":1,
"doc":
{
"data":"{\"tags\":[\"one\",\"two\",\"three\"]}"
}
}
{
"_index":"products",
"updated":1
}
$index->insertDocument([
'title'=> 'title',
'data' => [
'tags' => [1,2,3]
]
],1);
$index->updateDocument([
'data' => [
'one', 'two', 'three'
]
],1);
Array(
[_index] => products
[_id] => 1
[created] => true
[result] => created
)
Array(
[_index] => products
[updated] => 1
)
"index" : "products", "id" : 100, "doc" : {"title" : "title", "meta" : {"tags":[1,2,3]}}})
indexApi.insert({"index" : "products", "id" : 100, "doc" : {"meta" : {"tags":['one','two','three']}}}) indexApi.update({
'created': True,
{'found': None,
'id': 100,
'index': 'products',
'result': 'created'}
'id': 100, 'index': 'products', 'result': 'updated', 'updated': None} {
= await indexApi.insert({"index" : "products", "id" : 100, "doc" : {"title" : "title", "meta" : {"tags":[1,2,3]}}});
res = await indexApi.update({"index" : "products", "id" : 100, "doc" : {"meta" : {"tags":['one','two','three']}}}); res
"_index":"products","_id":100,"created":true,"result":"created"}
{"_index":"products","_id":100,"result":"updated"} {
new InsertDocumentRequest();
InsertDocumentRequest newdoc = new HashMap<String,Object>(){{
doc = put("title","title");
put("meta",
new HashMap<String,Object>(){{
put("tags",new int[]{1,2,3});
}});
}};index("products").id(100L).setDoc(doc);
newdoc.insert(newdoc);
indexApi.
new UpdateDocumentRequest();
updatedoc = new HashMap<String,Object >(){{
doc = put("meta",
new HashMap<String,Object>(){{
put("tags",new String[]{"one","two","three"});
}});
}};index("products").id(100L).setDoc(doc);
updatedoc.update(updatedoc); indexApi.
class SuccessResponse {
index: products100
id: true
created:
result: creatednull
found:
}
class UpdateResponse {
index: productsnull
updated: 100
id:
result: updated }
When using replication, the table name should be prepended with cluster_name:
(in SQL) so that updates will be propagated to all nodes in the cluster. For queries via HTTP, you should set a cluster
property. See setting up replication for more information.
{
"cluster":"nodes4",
"index":"test",
"id":1,
"doc":
{
"gid" : 100,
"price" : 1000
}
}
update weekly:posts set enabled=0 where id=1;
POST /update
{
"cluster":"weekly",
"index":"products",
"id":1,
"doc":
{
"enabled":0
}
}
$index->setName('products')->setCluster('weekly');
$index->updateDocument(['enabled'=>0],1);
"cluster":"weekly", "index" : "products", "id" : 1, "doc" : {"enabled" : 0}}) indexApi.update({
= wait indexApi.update({"cluster":"weekly", "index" : "products", "id" : 1, "doc" : {"enabled" : 0}}); res
new UpdateDocumentRequest();
updatedoc = new HashMap<String,Object >(){{
doc = put("enabled",0);
}};index("products").cluster("weekly").id(1L).setDoc(doc);
updatedoc.update(updatedoc); indexApi.
class UpdateResponse {
index: productsnull
updated: 1
id:
result: updated }
Here is the syntax for the SQL UPDATE
statement:
UPDATE table SET col1 = newval1 [, ...] WHERE where_condition [OPTION opt_name = opt_value [, ...]] [FORCE|IGNORE INDEX(id)]
where_condition
has the same syntax as in the SELECT statement.
Multi-value attribute value sets must be specified as comma-separated lists in parentheses. To remove all values from a multi-value attribute, just assign ()
to it.
UPDATE products SET tags1=(3,6,4) WHERE id=1;
UPDATE products SET tags1=() WHERE id=1;
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
POST /update
{
"index":"products",
"_id":1,
"doc":
{
"tags1": []
}
}
{
"_index":"products",
"updated":1
}
$index->updateDocument(['tags1'=>[]],1);
Array(
[_index] => products
[updated] => 1
)
"index" : "products", "id" : 1, "doc" : {"tags1": []}}) indexApi.update({
'id': 1, 'index': 'products', 'result': 'updated', 'updated': None} {
.update({"index" : "products", "id" : 1, "doc" : {"tags1": []}}) indexApi
"_index":"products","_id":1,"result":"updated"} {
new UpdateDocumentRequest();
updatedoc = new HashMap<String,Object >(){{
doc = put("tags1",new int[]{});
}};index("products").id(1L).setDoc(doc);
updatedoc.update(updatedoc); indexApi.
class UpdateResponse {
index: productsnull
updated: 1
id:
result: updated }
OPTION
clause is a Manticore-specific extension that lets you control a number of per-update options. The syntax is:
OPTION <optionname>=<value> [ , ... ]
The options are the same as for the SELECT statement. Specifically for the UPDATE
statement, you can use these options:
UPDATE
will result in an error if the UPDATE
query tries to perform an update on non-numeric properties. With strict=0, if multiple properties are updated and some are not allowed, the UPDATE
will not result in an error and will perform the changes only on allowed properties (with the rest being ignored). If none of the SET
changes of the UPDATE
re permitted, the command will result in an error even with strict=0.In rare cases, Manticore’s built-in query analyzer may be incorrect in understanding a query and determining whether a table by ID should be used. This can result in poor performance for queries like UPDATE ... WHERE id = 123
. For information on how to force the optimizer to use a docid index, see Query optimizer hints.
Updates using HTTP JSON protocol are performed via the /update
endpoint. The syntax is similar to the /insert endpoint, but this time the doc
property is mandatory.
The server will respond with a JSON object stating if the operation was successful or not.
POST /update
{
"index":"test",
"id":1,
"doc":
{
"gid" : 100,
"price" : 1000
}
}
{
"_index": "test",
"_id": 1,
"result": "updated"
}
The ID of the document that needs to be updated can be set directly using the id
property, as shown in the previous example, or you can update documents by query and apply the update to all the documents that match the query:
POST /update
{
"index":"test",
"doc":
{
"price" : 1000
},
"query":
{
"match": { "*": "apple" }
}
}
{
"_index":"products",
"updated":1
}
The query syntax is the same as in the /search endpoint. Note that you can’t specify id
and query
at the same time.
FLUSH ATTRIBUTES
The FLUSH ATTRIBUTES command flushes all in-memory attribute updates in all the active tables to disk. It returns a tag that identifies the result on-disk state, which represents the number of actual disk attribute saves performed since the server startup.
> UPDATE testindex SET channel_id=1107025 WHERE id=1;
mysqlQuery OK, 1 row affected (0.04 sec)
> FLUSH ATTRIBUTES;
mysql+------+
| tag |+------+
1 |
| +------+
1 row in set (0.19 sec)
See also attr_flush_period setting.
You can perform multiple update operations in a single call using the /bulk
endpoint. This endpoint only works with data that has Content-Type
set to application/x-ndjson
. The data should be formatted as newline-delimited JSON (NDJSON). Essentially, this means that each line should contain exactly one JSON statement and end with a newline \n
and, possibly, a \r
.
POST /bulk
{ "update" : { "index" : "products", "id" : 1, "doc": { "price" : 10 } } }
{ "update" : { "index" : "products", "id" : 2, "doc": { "price" : 20 } } }
{
"items":
[
{
"update":
{
"_index":"products",
"_id":1,
"result":"updated"
}
},
{
"update":
{
"_index":"products",
"_id":2,
"result":"updated"
}
}
],
"errors":false
}
The /bulk
endpoint supports inserts, replaces, and deletes. Each statement begins with an action type (in this case, update
). Here’s a list of the supported actions:
insert
: Inserts a document. The syntax is the same as in the /insert endpoint.create
: a synonym for insert
replace
: Replaces a document. The syntax is the same as in the /replace.index
: a synonym for replace
update
: Updates a document. The syntax is the same as in the /update.delete
: Deletes a document. The syntax is the same as in the /delete endpoint.Updates by query and deletes by query are also supported.
POST /bulk
{ "update" : { "index" : "products", "doc": { "coeff" : 1000 }, "query": { "range": { "price": { "gte": 1000 } } } } }
{ "update" : { "index" : "products", "doc": { "coeff" : 0 }, "query": { "range": { "price": { "lt": 1000 } } } } }
{
"items":
[
{
"update":
{
"_index":"products",
"updated":0
}
},
{
"update":
{
"_index":"products",
"updated":3
}
}
],
"errors":false
}
$client->bulk([
['update'=>[
'index' => 'products',
'doc' => [
'coeff' => 100
],
'query' => [
'range' => ['price'=>['gte'=>1000]]
]
]
],
['update'=>[
'index' => 'products',
'doc' => [
'coeff' => 0
],
'query' => [
'range' => ['price'=>['lt'=>1000]]
]
]
]
]);
Array(
[items] => Array (
Array(
[update] => Array(
[_index] => products
[updated] => 0
)
)
Array(
[update] => Array(
[_index] => products
[updated] => 3
)
)
)
= [ \
docs "update" : { "index" : "products", "doc": { "coeff" : 1000 }, "query": { "range": { "price": { "gte": 1000 } } } } }, \
{ "update" : { "index" : "products", "doc": { "coeff" : 0 }, "query": { "range": { "price": { "lt": 1000 } } } } } ]
{ '\n'.join(map(json.dumps,docs))) indexApi.bulk(
'error': None,
{'items': [{u'update': {u'_index': u'products', u'updated': 1}},
u'update': {u'_index': u'products', u'updated': 3}}]} {
= [
docs "update" : { "index" : "products", "doc": { "coeff" : 1000 }, "query": { "range": { "price": { "gte": 1000 } } } } },
{ "update" : { "index" : "products", "doc": { "coeff" : 0 }, "query": { "range": { "price": { "lt": 1000 } } } } } ];
{ = await indexApi.bulk(docs.map(e=>JSON.stringify(e)).join('\n')); res
"items":[{"update":{"_index":"products","updated":1}},{"update":{"_index":"products","updated":5}}],"errors":false} {
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";
bulk(body); indexApi.
class BulkResponse {
1, created=false, result=updated, status=200}}, {replace={_index=products, _id=2, created=false, result=updated, status=200}}]
items: [{replace={_index=products, _id=null
error: false}
additionalProperties: {errors= }
Keep in mind that the bulk operation stops at the first query that results in an error.
attr_update_reserve=size
attr_update_reserve
is a per-table setting that determines the space reserved for blob attribute updates. This setting is optional, with a default value of 128k.
When blob attributes (MVAs, strings, JSON) are updated, their length may change. If the updated string (or MVA, or JSON) is shorter than the old one, it overwrites the old one in the .spb
file. However, if the updated string is longer, updates are written to the end of the .spb
file. This file is memory-mapped, which means resizing it may be a rather slow process, depending on the OS implementation of memory-mapped files.
To avoid frequent resizes, you can specify the extra space to be reserved at the end of the .spb
file using this option.
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']
]
],
'index' => 'products'
];
$index = new \Manticoresearch\Index($client);
$index->create($params);
'create table products(title text, price float) attr_update_reserve = \'1M\'') utilsApi.sql(
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.
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
When updating attributes the changes are first written to in-memory copy of attributes. This setting allows to set the interval between flushing the updates to disk. It defaults to 0, which disables the periodic flushing, but flushing will still occur at normal shut-down.