# Mastering Manticore Search: RT vs Plain Mode Explained

Understand the key differences between RT and Plain modes in Manticore Search and when to use each for optimal performance

你是否曾经对 Manticore Search 中的不同模式感到困惑？你并不孤单。许多开发人员在首次使用 Manticore Search 时，都会难以理解实时（RT）模式和普通模式之间的区别。但不用担心——一旦你理解了核心概念，其实这非常简单。

在本指南中，我们将揭开这两种操作模式的神秘面纱，并向你展示何时以及如何使用每种模式。

## 了解 Manticore Search 模式

Manticore Search 提供了两种主要的操作模式：

* **实时（RT）模式** - 默认模式，允许你随时创建和删除表
* **普通模式** - 与静态模式架构一起工作，从外部存储源构建普通表

让我们深入了解每种模式，以了解它们的功能和限制。

## 实时（RT）模式：动态且灵活

RT 模式是 Manticore Search 的默认且最常用模式。它专为需要在不重启服务的情况下创建、修改或删除表的动态环境而设计。

### 如何识别 RT 模式

RT 模式的标志是配置文件中存在 `data_dir` 指令。让我们看一下典型的配置：

```bash
cat /opt/homebrew/etc/manticoresearch/manticore.conf
```

```
searchd {
    listen = 127.0.0.1:9312
    listen = 127.0.0.1:9306:mysql
    listen = 127.0.0.1:9308:http
    log = /opt/homebrew/var/log/manticore/searchd.log
    query_log = /opt/homebrew/var/log/manticore/query.log
    pid_file = /opt/homebrew/var/run/manticore/searchd.pid
    data_dir = /opt/homebrew/var/manticore
}
```

注意配置中的 `data_dir` 指令。这一行代码就是启用 RT 模式的全部内容。

### 动态创建表

RT 模式最大的优势之一是能够动态创建表：

```bash
mysql -P9306 -h0 -v
```

```
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 759523
Server version: 7.0.1 763f4a0b9@25013111 dev (columnar 4.0.1 9f6686d@25012409) (secondary 4.0.1 9f6686d@25012409) (knn 4.0.1 9f6686d@25012409) git branch master...origin/master

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Reading history-file /Users/sn/.mysql_history
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
```

创建表就像这样简单：

```sql
mysql> create table t;
```

```
Query OK, 0 rows affected (0.00 sec)
```

删除表也同样容易：

```sql
mysql> drop table t;
```

```
Query OK, 0 rows affected (0.01 sec)
```

### 复制支持

RT 模式独有的另一个强大功能是复制。你可以创建集群并将表添加到其中：

```sql
mysql> create table t;
```

```
Query OK, 0 rows affected (0.00 sec)
```

```sql
mysql> create cluster c;
```

```
Query OK, 0 rows affected (0.44 sec)
```

```sql
mysql> alter cluster c add t;
```

```
Query OK, 0 rows affected (0.00 sec)
```

## 普通模式：稳定且结构化

普通模式适用于架构相对稳定且提前定义的环境。它特别适用于与外部数据源一起工作时。

### 如何识别普通模式

配置文件中没有 `data_dir` 指令表示普通模式。让我们看一下普通模式的配置：

```bash
cat ~/manticore/plain.conf
```

```
searchd {
  listen = 9315:mysql41
  log = searchd.log
  pid_file = searchd.pid
  binlog_path =
}

source src {
  type = csvpipe
  csvpipe_command = echo "1,abcdef,123"
  csvpipe_field = f
  csvpipe_attr_uint = a
}

table idx {
  type = plain
  source = src
  path = idx
}

table rt {
  type = rt
  path = rt
  rt_field = f
  rt_attr_uint = a
}
```

注意这里没有 `data_dir` 指令，但配置文件中直接有表定义。

### 启动普通模式实例

让我们以普通模式启动 Manticore Search：

```bash
searchd -c plain.conf
```

```
Manticore 7.0.0 92c650401@25013002 (columnar 4.0.0 5aa8e43@25012409) (secondary 4.0.0 5aa8e43@25012409) (knn 4.0.0 5aa8e43@25012409)
Copyright (c) 2001-2016, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
Copyright (c) 2017-2024, Manticore Software LTD (https://manticoresearch.com)

[26:49.002] [20433033] using config file '/Users/sn/manticore/plain.conf' (344 chars)...
starting daemon version '7.0.0 92c650401@25013002 (columnar 4.0.0 5aa8e43@25012409) (secondary 4.0.0 5aa8e43@25012409) (knn 4.0.0 5aa8e43@25012409)' ...
listening on all interfaces for mysql, port=9315
precaching table 'idx'
Index header format is not json, will try it as binary...
WARNING: Unable to load header... Error failed to open idx.sph: No such file or directory
WARNING: table 'idx': prealloc: failed to open idx.sph: No such file or directory - NOT SERVING
precaching table 'rt'
precached 1 tables in 0.004 sec
```

### 在普通模式下使用表

当我们检查可用表时，最初只能看到 RT 表：

```bash
mysql -P9315 -h0 -e "show tables"
```

```
+-------+------+
| Table | Type |
+-------+------+
| rt    | rt   |
+-------+------+
```

要使普通表可用，我们需要使用索引器工具构建它：

```bash
indexer -c plain.conf --all --rotate
```

```
Manticore 7.0.0 92c650401@25013002 (columnar 4.0.0 5aa8e43@25012409) (secondary 4.0.0 5aa8e43@25012409) (knn 4.0.0 5aa8e43@25012409)
Copyright (c) 2001-2016, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
Copyright (c) 2017-2024, Manticore Software LTD (https://manticoresearch.com)

using config file '/Users/sn/manticore/plain.conf'...
indexing table 'idx'...
collected 1 docs, 0.0 MB
creating secondary index
creating lookup: 0.0 Kdocs, 100.0% done
sorted 0.0 Mhits, 100.0% done
total 1 docs, 6 bytes
total 0.029 sec, 203 bytes/sec, 33.96 docs/sec
WARNING: skipping non-plain table 'rt'...
total 3 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
total 15 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
rotating tables: successfully sent SIGHUP to searchd (pid=89954).
```

重新加载表后：

```bash
mysql -P9315 -h0 -e "reload tables"
```

现在两个表都可用：

```bash
mysql -P9315 -h0 -e "show tables"
```

```
+-------+-------+
| Table | Type  |
+-------+-------+
| idx   | local |
| rt    | rt    |
+-------+-------+
```

### 普通模式的限制

在普通模式下，你无法动态创建表：

```bash
mysql -P9315 -h0 -e "create table t"
```

```
ERROR 1064 (42000) at line 1: CREATE TABLE requires data_dir to be set in the config file
```

复制功能也不可用：

```bash
mysql -P9315 -h0 -e "create cluster c"
```

```
ERROR 1064 (42000) at line 1: can not create cluster 'c': no 'listen' is found, cannot set incoming addresses, replication is disabled
```

## 何时使用每种模式

**使用 RT 模式时：**
* 你需要频繁创建或修改表
* 你希望使用复制功能
* 你更喜欢动态、灵活的环境
* 你正在构建需要适应变化需求的应用程序

**使用普通模式时：**
* 你的架构稳定且定义明确
* 你主要与外部数据源一起工作
* 你希望确保部署中的架构一致性
* 你需要更受控、以配置驱动的方法

## 结论

了解 Manticore Search 中 RT 模式和普通模式之间的差异对于优化你的搜索实现至关重要。RT 模式提供了灵活性和动态表管理，而普通模式则提供了稳定性和结构。

对于大多数现代应用程序，由于其灵活性和功能集，RT 模式是推荐的选择。然而，普通模式仍然有其用武之地，尤其是在架构稳定性和配置可移植性优先的环境中。

更喜欢观看而不是阅读？查看我们关于此主题的视频：

{{< youtube WUNP7e-h48s >}}

记住，识别你使用的是哪种模式的关键很简单：如果你的配置中有 `data_dir` 指令，那么你使用的是 RT 模式；如果没有，则使用的是普通模式。
