Deleting documents

Deleting is only supported for:

  • real-time tables,
  • percolate tables
  • distributed tables that contain only RT tables as agents

You can delete existing rows (documents) from an existing table based on ID or conditions.

Deleting documents is supported via SQL and HTTP interfaces.

SQL response for successful operation will show the number of rows deleted.

json/delete is an HTTP endpoint for deleting. The server will respond with a JSON object stating if the operation was successful or not and the number of rows deleted.

To delete all documents from a table it’s recommended to use instead the table truncation as it’s a much faster operation.

  • SQL
  • JSON

SQL JSON

  1. DELETE FROM table WHERE where_condition
  • table is a name of the table from which the row should be deleted.
  • where_condition for SQL has the same syntax as in the SELECT statement.
  1. POST /delete -d '
  2. {
  3. "index": "test",
  4. "id": 1
  5. }'
  6. POST /delete -d '
  7. {
  8. "index": "test",
  9. "query":
  10. {
  11. "match": { "*": "apple" }
  12. }
  13. }'
  • id for JSON is the row id which should be deleted.
  • query for JSON is the full-text condition and has the same syntax as in the JSON/update.
  • cluster for JSON is cluster name property and should be set along with table property to delete a row from a table which is inside a replication cluster.

In this example we are deleting all documents that match full-text query dummy from table named test:

  • SQL
  • JSON
  • PHP
  • Python
  • javascript
  • Java

SQL JSON PHP Python javascript Java

  1. SELECT * FROM TEST;
  2. DELETE FROM TEST WHERE MATCH ('dummy');
  3. SELECT * FROM TEST;
  1. POST /delete -d '
  2. {
  3. "index":"test",
  4. "query":
  5. {
  6. "match": { "*": "dummy" }
  7. }
  8. }'
  1. $index->deleteDocuments(new Match('dummy','*'));
  1. indexApi.delete({"index" : "products", "query": { "match": { "*": "dummy" }}})
  1. res = await indexApi.delete({"index" : "products", "query": { "match": { "*": "dummy" }}});
  1. DeleteDocumentRequest deleteRequest = new DeleteDocumentRequest();
  2. query = new HashMap<String,Object>();
  3. query.put("match",new HashMap<String,Object>(){{
  4. put("*","dummy");
  5. }});
  6. deleteRequest.index("products").setQuery(query);
  7. indexApi.delete(deleteRequest);

Response

  1. +------+------+-------------+------+
  2. | id | gid | mva1 | mva2 |
  3. +------+------+-------------+------+
  4. | 100 | 1000 | 100,201 | 100 |
  5. | 101 | 1001 | 101,202 | 101 |
  6. | 102 | 1002 | 102,203 | 102 |
  7. | 103 | 1003 | 103,204 | 103 |
  8. | 104 | 1004 | 104,204,205 | 104 |
  9. | 105 | 1005 | 105,206 | 105 |
  10. | 106 | 1006 | 106,207 | 106 |
  11. | 107 | 1007 | 107,208 | 107 |
  12. +------+------+-------------+------+
  13. 8 rows in set (0.00 sec)
  14. Query OK, 2 rows affected (0.00 sec)
  15. +------+------+-------------+------+
  16. | id | gid | mva1 | mva2 |
  17. +------+------+-------------+------+
  18. | 100 | 1000 | 100,201 | 100 |
  19. | 101 | 1001 | 101,202 | 101 |
  20. | 102 | 1002 | 102,203 | 102 |
  21. | 103 | 1003 | 103,204 | 103 |
  22. | 104 | 1004 | 104,204,205 | 104 |
  23. | 105 | 1005 | 105,206 | 105 |
  24. +------+------+-------------+------+
  25. 6 rows in set (0.00 sec)
  1. {
  2. "_index":"test",
  3. "deleted":2,
  4. }
  1. Array(
  2. [_index] => test
  3. [deleted] => 2
  4. )
  1. {'deleted': 2, 'id': None, 'index': 'products', 'result': None}
  1. {"_index":"products","deleted":2}
  1. class DeleteResponse {
  2. index: products
  3. deleted: 2
  4. id: null
  5. result: null
  6. }

Here - deleting a document with id 100 from table named test:

  • SQL
  • JSON
  • PHP
  • Python
  • javascript
  • Java

SQL JSON PHP Python javascript Java

  1. DELETE FROM TEST WHERE id=100;
  2. SELECT * FROM TEST;
  1. POST /delete -d '
  2. {
  3. "index":"test",
  4. "id": 100
  5. }'
  1. $index->deleteDocument(100);
  1. indexApi.delete({"index" : "products", "id" : 1})
  1. res = await indexApi.delete({"index" : "products", "id" : 1});
  1. DeleteDocumentRequest deleteRequest = new DeleteDocumentRequest();
  2. deleteRequest.index("products").setId(1L);
  3. indexApi.delete(deleteRequest);

Response

  1. Query OK, 1 rows affected (0.00 sec)
  2. +------+------+-------------+------+
  3. | id | gid | mva1 | mva2 |
  4. +------+------+-------------+------+
  5. | 101 | 1001 | 101,202 | 101 |
  6. | 102 | 1002 | 102,203 | 102 |
  7. | 103 | 1003 | 103,204 | 103 |
  8. | 104 | 1004 | 104,204,205 | 104 |
  9. | 105 | 1005 | 105,206 | 105 |
  10. +------+------+-------------+------+
  11. 5 rows in set (0.00 sec)
  1. {
  2. "_index":"test",
  3. "_id":100,
  4. "found":true,
  5. "result":"deleted"
  6. }
  1. Array(
  2. [_index] => test
  3. [_id] => 100
  4. [found] => true
  5. [result] => deleted
  6. )
  1. {'deleted': None, 'id': 1, 'index': 'products', 'result': 'deleted'}
  1. {"_index":"products","_id":1,"result":"deleted"}
  1. class DeleteResponse {
  2. index: products
  3. _id: 1
  4. result: deleted
  5. }

Manticore SQL allows to use complex conditions for the DELETE statement.

For example here we are deleting documents that match full-text query dummy and have attribute mva1 with a value greater than 206 or mva1 values 100 or 103 from table named test:

  • SQL

SQL

  1. DELETE FROM TEST WHERE MATCH ('dummy') AND ( mva1>206 or mva1 in (100, 103) );
  2. SELECT * FROM TEST;

Response

  1. Query OK, 4 rows affected (0.00 sec)
  2. +------+------+-------------+------+
  3. | id | gid | mva1 | mva2 |
  4. +------+------+-------------+------+
  5. | 101 | 1001 | 101,202 | 101 |
  6. | 102 | 1002 | 102,203 | 102 |
  7. | 104 | 1004 | 104,204,205 | 104 |
  8. | 105 | 1005 | 105,206 | 105 |
  9. +------+------+-------------+------+
  10. 6 rows in set (0.00 sec)

Here is an example of deleting documents in cluster nodes4‘s table test:

  • SQL
  • JSON
  • PHP
  • Python
  • javascript
  • Java

SQL JSON PHP Python javascript Java

  1. delete from nodes4:test where id=100;
  1. POST /delete -d '
  2. {
  3. "cluster":"nodes4",
  4. "index":"test",
  5. "id": 100
  6. }'
  1. $index->setCluster('nodes4');
  2. $index->deleteDocument(100);
  1. indexApi.delete({"cluster":"nodes4","index" : "products", "id" : 100})
  1. indexApi.delete({"cluster":"nodes4","index" : "products", "id" : 100})
  1. DeleteDocumentRequest deleteRequest = new DeleteDocumentRequest();
  2. deleteRequest.cluster("nodes4").index("products").setId(100L);
  3. indexApi.delete(deleteRequest);

Response

  1. Array(
  2. [_index] => test
  3. [_id] => 100
  4. [found] => true
  5. [result] => deleted
  6. )
  1. {'deleted': None, 'id': 100, 'index': 'products', 'result': 'deleted'}
  1. {"_index":"products","_id":100,"result":"deleted"}
  1. class DeleteResponse {
  2. index: products
  3. _id: 100
  4. result: deleted
  5. }

Transactions

Manticore supports basic transactions when performing deleting and insertion into real-time and percolate tables. That is: each change to a table is first saved into an internal changeset, and then is actually committed to the table. By default each command is wrapped into an individual automatic transaction, making it transparent: you just ‘insert’ something, and can see the inserted result after it completes, having no care about transactions. However that behaviour can be explicitly managed by starting and committing transactions manually.

Transactions are supported for the following commands:

Transactions are not supported for:

Please also note, that transactions in Manticore do not aim to provide isolation. The idea of transactions in Manticore is to let you accumulate a number of writes and run them at once at commit or rollback them all if needed. Transactions are integrated with binary log which gives durability and consistency.

Automatic and manual mode

  1. SET AUTOCOMMIT = {0 | 1}

SET AUTOCOMMIT controls the autocommit mode in the active session. AUTOCOMMIT is set to 1 by default. With default you have not to care about transactions, since every statement that performs any changes on any table is implicitly wrapped into separate transaction. Setting it to 0 allows you to manage transactions manually. I.e., they will not be visible until you explicitly commit them.

Transactions are limited to a single RT or percolate table, and also limited in size. They are atomic, consistent, overly isolated, and durable. Overly isolated means that the changes are not only invisible to the concurrent transactions but even to the current session itself.

BEGIN, COMMIT, and ROLLBACK

  1. START TRANSACTION | BEGIN
  2. COMMIT
  3. ROLLBACK

BEGIN statement (or its START TRANSACTION alias) forcibly commits pending transaction, if any, and begins a new one.

COMMIT statement commits the current transaction, making all its changes permanent.

ROLLBACK statement rolls back the current transaction, canceling all its changes.

Examples

Automatic commits (default)

  1. insert into indexrt (id, content, title, channel_id, published) values (1, 'aa', 'blabla', 1, 10);
  2. Query OK, 1 rows affected (0.00 sec)
  3. select * from indexrt where id=1;
  4. +------+------------+-----------+--------+
  5. | id | channel_id | published | title |
  6. +------+------------+-----------+--------+
  7. | 1 | 1 | 10 | blabla |
  8. +------+------------+-----------+--------+
  9. 1 row in set (0.00 sec)

Inserted value immediately visible by following ‘select’ statement.

Manual commits (autocommit=0)

  1. set autocommit=0;
  2. Query OK, 0 rows affected (0.00 sec)
  3. insert into indexrt (id, content, title, channel_id, published) values (3, 'aa', 'bb', 1, 1);
  4. Query OK, 1 row affected (0.00 sec)
  5. insert into indexrt (id, content, title, channel_id, published) values (4, 'aa', 'bb', 1, 1);
  6. Query OK, 1 row affected (0.00 sec)
  7. select * from indexrt where id=3;
  8. Empty set (0.01 sec)
  9. select * from indexrt where id=4;
  10. Empty set (0.00 sec)

Here changes is NOT automatically committed. So, insertion is not visible even in the same session, since they’re not committed. Also, despite absent BEGIN statement, transaction is implicitly started.

So, let’s finally commit it:

  1. commit;
  2. Query OK, 0 rows affected (0.00 sec)
  3. select * from indexrt where id=4;
  4. +------+------------+-----------+-------+
  5. | id | channel_id | published | title |
  6. +------+------------+-----------+-------+
  7. | 4 | 1 | 1 | bb |
  8. +------+------------+-----------+-------+
  9. 1 row in set (0.00 sec)
  10. select * from indexrt where id=3;
  11. +------+------------+-----------+-------+
  12. | id | channel_id | published | title |
  13. +------+------------+-----------+-------+
  14. | 3 | 1 | 1 | bb |
  15. +------+------------+-----------+-------+
  16. 1 row in set (0.00 sec)

Now it is finished and visible.

Manual transaction

Using BEGIN and COMMIT you can define bounds of transaction explicitly, so no need to care about autocommit in this case.

  1. begin;
  2. Query OK, 0 rows affected (0.00 sec)
  3. insert into indexrt (id, content, title, channel_id, published) values (2, 'aa', 'bb', 1, 1);
  4. Query OK, 1 row affected (0.00 sec)
  5. select * from indexrt where id=2;
  6. Empty set (0.01 sec)
  7. commit;
  8. Query OK, 0 rows affected (0.01 sec)
  9. select * from indexrt where id=2;
  10. +------+------------+-----------+-------+
  11. | id | channel_id | published | title |
  12. +------+------------+-----------+-------+
  13. | 2 | 1 | 1 | bb |
  14. +------+------------+-----------+-------+
  15. 1 row in set (0.01 sec)

SET TRANSACTION

  1. SET TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED
  2. | READ COMMITTED
  3. | REPEATABLE READ
  4. | SERIALIZABLE }

SET TRANSACTION statement does nothing. It was implemented to maintain compatibility with 3rd party MySQL client libraries, connectors, and frameworks that may need to run this statement when connecting. They just goes across syntax parser and then returns ‘ok’. Nothing usable for your own programs, just a stubs to make third-party clients happy.

  1. mysql> SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
  2. Query OK, 0 rows affected (0.00 sec)

✔ Searching