REPLACE
works similarly to INSERT, but it marks the previous document with the same ID as deleted before inserting a new one.
For HTTP JSON protocol, two request formats are available: Manticore and Elasticsearch-like. You can find both examples in the provided examples.
REPLACE INTO products VALUES(1, "document one", 10);
Query OK, 1 row affected (0.00 sec)
POST /replace
-H "Content-Type: application/x-ndjson" -d '
{
"index":"products",
"id":1,
"doc":
{
"title":"product one",
"price":10
}
}
'
{
"_index":"products",
"_id":1,
"created":false,
"result":"updated",
"status":200
}
PUT /products/_doc/2
{
"title": "product two",
"price": 20
}
POST /products/_doc/
{
"title": "product three",
"price": 10
}
{
"_id":2,
"_index":"products",
"_primary_term":1,
"_seq_no":0,
"_shards":{
"failed":0,
"successful":1,
"total":1
},
"_type":"_doc",
"_version":1,
"result":"created"
}
{
"_id":2235747273424240642,
"_index":"products",
"_primary_term":1,
"_seq_no":0,
"_shards":{
"failed":0,
"successful":1,
"total":1
},
"_type":"_doc",
"_version":1,
"result":"created"
}
$index->replaceDocument([
'title' => 'document one',
'price' => 10
],1);
Array(
[_index] => products
[_id] => 1
[created] => false
[result] => updated
[status] => 200
)
"index" : "products", "id" : 1, "doc" : {"title" : "document one","price":10}}) indexApi.replace({
'created': False,
{'found': None,
'id': 1,
'index': 'products',
'result': 'updated'}
= await indexApi.replace({"index" : "products", "id" : 1, "doc" : {"title" : "document one","price":10}}); res
"_index":"products","_id":1,"result":"updated"} {
new InsertDocumentRequest();
docRequest = HashMap<String,Object> doc = new HashMap<String,Object>(){{
put("title","document one");
put("price",10);
}};index("products").id(1L).setDoc(doc);
docRequest.replace(docRequest); sqlresult = indexApi.
class SuccessResponse {
index: products1
id: false
created:
result: updatednull
found: }
REPLACE
is available for both RT and PQ tables.
When you run a REPLACE
, the previous document is not removed, but it’s marked as deleted, so the table size grows until chunk merging happens, and the marked documents won’t be included. To force a chunk merge, use the OPTIMIZE statement.
The syntax of the REPLACE
statement is the same as the INSERT statement syntax.
REPLACE INTO table [(column1, column2, ...)]
VALUES (value1, value2, ...)
...)] [, (
To use the HTTP JSON interface with REPLACE
, use the /replace
endpoint. There’s also a synonym endpoint, /index
.
You can replace multiple documents at once. Check bulk adding documents for more information.
REPLACE INTO products(id,title,tag) VALUES (1, 'doc one', 10), (2,' doc two', 20);
Query OK, 2 rows affected (0.00 sec)
POST /bulk
-H "Content-Type: application/x-ndjson" -d '
{ "replace" : { "index" : "products", "id":1, "doc": { "title": "doc one", "tag" : 10 } } }
{ "replace" : { "index" : "products", "id":2, "doc": { "title": "doc two", "tag" : 20 } } }
'
{
"items":
[
{
"replace":
{
"_index":"products",
"_id":1,
"created":false,
"result":"updated",
"status":200
}
},
{
"replace":
{
"_index":"products",
"_id":2,
"created":false,
"result":"updated",
"status":200
}
}
],
"errors":false
}
$index->replaceDocuments([
[
'id' => 1,
'title' => 'document one',
'tag' => 10
],
[
'id' => 2,
'title' => 'document one',
'tag' => 20
]
);
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
)
= manticoresearch.IndexApi(client)
indexApi = [ \
docs "replace": {"index" : "products", "id" : 1, "doc" : {"title" : "document one"}}}, \
{"replace": {"index" : "products", "id" : 2, "doc" : {"title" : "document two"}}} ]
{= indexApi.bulk('\n'.join(map(json.dumps,docs))) api_resp
'error': None,
{'items': [{u'replace': {u'_id': 1,
u'_index': u'products',
u'created': False,
u'result': u'updated',
u'status': 200}},
u'replace': {u'_id': 2,
{u'_index': u'products',
u'created': False,
u'result': u'updated',
u'status': 200}}]}
= [
docs "replace": {"index" : "products", "id" : 1, "doc" : {"title" : "document one"}}},
{"replace": {"index" : "products", "id" : 2, "doc" : {"title" : "document two"}}} ];
{= await indexApi.bulk(docs.map(e=>JSON.stringify(e)).join('\n')); res
"items":[{"replace":{"_index":"products","_id":1,"created":false,"result":"updated","status":200}},{"replace":{"_index":"products","_id":2,"created":false,"result":"updated","status":200}}],"errors":false} {
"{\"replace\": {\"index\" : \"products\", \"id\" : 1, \"doc\" : {\"title\" : \"document one\"}}}" +"\n"+
body = "{\"replace\": {\"index\" : \"products\", \"id\" : 2, \"doc\" : {\"title\" : \"document two\"}}}"+"\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= }