# Manticore Search - RT mode - index administration Definitive Guide

我们最近发布了一篇[关于新实时模式的文章](https://manticoresearch.com/blog/rt-mode/)。在本文中，我们将详细探讨其工作原理以及如何在您自己的系统中使用它。我们将学习如何使用SQL语句在Manticore Search中操作索引。

# 索引创建

---

让我们首先通过SQL连接到Manticore：

```sql
root@rtmode-79f9d6d867-fptcc:/# mysql -P9306 -h0
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 3.4.0 0686d9f0@200326 release
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

```
一个简单的表创建语句如下所示：

```sql
MySQL [(none)]> CREATE TABLE testrt1 (title TEXT, category INT);

```
这将创建一个包含文档ID（无需声明）、文本字段和整数属性的索引。

索引支持一系列可以在声明时传递的选项：

例如：

```sql
MySQL [(none)]> CREATE TABLE testrt2 (title TEXT, category INT)  html_strip='1' docstore_compression_level = '12';

```
在此索引中，文本将被剥离HTML，并且文档存储的压缩级别设置为最高级别。

如果未指定类型，默认情况下索引将是实时的。如果我们想要创建一个分布式索引，需要传递类型：

```sql
MySQL [(none)]> CREATE TABLE testrtdist type='distributed' local='testrt1' local='testrt2';

```
如果我们不确定表是否已经存在，可以使用'IF NOT EXISTS'子句来避免错误：

```sql
MySQL [(none)]> CREATE TABLE IF NOT EXISTS testrt1 (title TEXT, category INT);

```
还可以使用现有索引的设置和模式来创建索引：

```sql
MySQL [(none)]> CREATE TABLE testrt3 LIKE testrt2;

```


# 更多表操作

---


要删除索引，请使用DROP TABLE：

```sql
MySQL [(none)]> DROP TABLE testrt3;

```
要清空索引：

```sql
MySQL [(none)]> TRUNCATE TABLE testrt2;

```
要添加或删除列：

```sql
MySQL [(none)]> ALTER TABLE testrt2 ADD COLUMN tagId INT;

```

```sql
MySQL [(none)]> ALTER TABLE testrt2 DROP COLUMN tagId;

```
请注意，目前无法添加或删除TEXT字段。如果您需要修改TEXT字段，必须重新创建表。

也可以更改索引设置：

```sql
MySQL [(none)]> ALTER TABLE testrt2 ignore_chars='.';

```
请注意，分词设置不会应用于全文组件中的现有数据，只有在ALTER之后添加的新文档才会受到影响。
如果您需要更新整个集合，必须使用所需的设置重新创建索引。


# 表信息

---


有几个语句可以提供有关索引的信息。

要获取索引模式：

```sql
MySQL [(none)]> DESCRIBE testrt2;
+----------+--------+----------------+
| Field    | Type   | Properties     |
+----------+--------+----------------+
| id       | bigint |                |
| title    | field  | indexed stored |
| category | uint   |                |
+----------+--------+----------------+

```
使用SHOW INDEX STATUS可以查看文档数量、大小和性能指标的信息：

```sql
MySQL [(none)]> SHOW INDEX testrt2 STATUS;
+-------------------+--------------------------------------------------------------------------+
| Variable_name     | Value                                                                    |
+-------------------+--------------------------------------------------------------------------+
| index_type        | rt                                                                       |
| indexed_documents | 0                                                                        |
| indexed_bytes     | 0                                                                        |
| ram_bytes         | 5840                                                                     |
| disk_bytes        | 331                                                                      |
| ram_chunk         | 0                                                                        |
| ram_chunks_count  | 0                                                                        |
| disk_chunks       | 0                                                                        |
| mem_limit         | 134217728                                                                |
| ram_bytes_retired | 0                                                                        |
| tid               | 0                                                                        |
| tid_saved         | 0                                                                        |
| query_time_1min   | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| query_time_5min   | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| query_time_15min  | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| query_time_total  | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| found_rows_1min   | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| found_rows_5min   | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| found_rows_15min  | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| found_rows_total  | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
+-------------------+--------------------------------------------------------------------------+

```
SHOW INDEX SETTINGS还可以提供索引设置列表：

```sql
MySQL [(none)]> SHOW INDEX testrt2 SETTINGS;
+---------------+----------------------------------------------------------+
| Variable_name | Value                                                    |
+---------------+----------------------------------------------------------+
| settings      | html_strip = 1                                           |
|               | charset_table = non_cjk                                  |
|		| ignore_chars = .                                         |
+---------------+----------------------------------------------------------+

```
如果您需要导出完整的索引声明，请使用SHOW CREATE TABLE：

```sql
MySQL [(none)]> SHOW CREATE TABLE testrt2;
+---------+-------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                  |
+---------+-------------------------------------------------------------------------------------------------------------------------------+
| testrt2 | CREATE TABLE testrt2 (category integer,title text indexed stored) html_strip='1' charset_table='non_cjk' ignore_chars='.'     |
+---------+-------------------------------------------------------------------------------------------------------------------------------+

```


# 导入索引

---


可以使用IMPORT TABLE语句导入RT索引。
索引文件将被复制到配置中指定的data_dir目录中。

示例：

```sql
MySQL [(none)]> IMPORT TABLE movies FROM '/index/movies_rt';

```

```sql
MySQL [(none)]> SELECT * FROM moviesG
*************************** 1. row ***************************
id: 1
num_critic_for_reviews: 4
duration: 96
director_facebook_likes: 0
actor_3_facebook_likes: 460
actor_1_facebook_likes: 708
gross: 0
num_voted_users: 961
cast_total_facebook_likes: 2307
facenumber_in_poster: 0
num_user_for_reviews: 12
budget: 3500000
title_year: 2009
actor_2_facebook_likes: 574
movie_facebook_likes: 211
imdb_score: 4.800000
aspect_ration: 2.350000
color: Color
movie_imdb_link: http://www.imdb.com/title/tt1002561/?ref_=fn_tt_tt_1
language: English
country: USA
content_rating:
director_name: Charles Adelman
actor_2_name: Kevin Pollak
actor_1_name: Teri Polo
movie_title: 02:13
actor_3_name: Jere Burns
plot_keywords: death|forensic|murder|profiler|serial killer
...
*************************** 20. row ***************************
id: 20
num_critic_for_reviews: 1
duration: 111
director_facebook_likes: 0
actor_3_facebook_likes: 247
actor_1_facebook_likes: 1000
gross: 14616
num_voted_users: 314
cast_total_facebook_likes: 2059
facenumber_in_poster: 1
num_user_for_reviews: 10
budget: 12000000
title_year: 2015
actor_2_facebook_likes: 445
movie_facebook_likes: 26000
imdb_score: 7.500000
aspect_ration: 1.850000
color: Color
movie_imdb_link: http://www.imdb.com/title/tt3453052/?ref_=fn_tt_tt_1
language: English
country: USA
content_rating: R
director_name: Timothy Hines
actor_2_name: Kelly LeBrock
actor_1_name: Christopher Lambert
movie_title: 10 Days in a Madhouse
actor_3_name: Alexandra Callas
plot_keywords:

```
我们认为新的RT模式作为默认模式将使在大多数情况下维护Manticore Search中的索引变得更加容易。对于其他情况，普通模式仍然受支持。您可以通过[我们的文档](https://docs.manticoresearch.com/latest/html/getting-started/configuration.html)了解差异。
[![img](./manticoresearch-rt-mode-index-administration-definitive-guide/RT-mode-index.gif)](https://play.manticoresearch.com/rtmode/)  

# [互动课程](https://play.manticoresearch.com/rtmode/)

您可以通过我们的“ManticoreSearch - RT模式 - 索引管理”互动课程了解更多关于实时索引管理的内容，该课程包含命令行以方便学习。
