您是否曾对 Manticore Search 中的不同模式感到困惑?您并不孤单。许多开发人员在首次使用 Manticore Search 时都难以理解实时(RT)模式和普通模式之间的区别。但别担心——一旦您理解了核心概念,这其实非常简单。
在本指南中,我们将揭开这两种操作模式的神秘面纱,并向您展示何时以及如何使用每种模式。
理解 Manticore Search 模式
Manticore Search 提供两种主要操作模式:
- 实时(RT)模式 - 默认模式,允许您动态创建和删除表
- 普通模式 - 与静态模式和从外部存储源构建的普通表一起工作
让我们深入了解每种模式,以理解它们的能力和局限性。
实时(RT)模式:动态和灵活
RT 模式是 Manticore Search 中默认和最常用的模式。它旨在用于动态环境,您需要在不重启服务的情况下创建、修改或删除表。
如何识别 RT 模式
RT 模式的关键指标是配置文件中存在 data_dir 指令。让我们看一下典型的配置:
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 模式的最大优势之一是能够动态创建表:
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.
创建表就像这样简单:
mysql> create table t;
Query OK, 0 rows affected (0.00 sec)
删除表同样简单:
mysql> drop table t;
Query OK, 0 rows affected (0.01 sec)
复制支持
RT 模式的另一个强大功能是复制。您可以创建集群并向其中添加表:
mysql> create table t;
Query OK, 0 rows affected (0.00 sec)
mysql> create cluster c;
Query OK, 0 rows affected (0.44 sec)
mysql> alter cluster c add t;
Query OK, 0 rows affected (0.00 sec)
普通模式:稳定和结构化
普通模式旨在用于模式相对稳定并提前定义的环境。它在处理外部数据源时特别有用。
如何识别普通模式
配置文件中缺少 data_dir 指令表示普通模式。让我们看一下普通模式的配置:
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:
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 表可用:
mysql -P9315 -h0 -e "show tables"
+-------+------+
| Table | Type |
+-------+------+
| rt | rt |
+-------+------+
要使普通表可用,我们需要使用索引工具构建它:
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).
在重新加载表后:
mysql -P9315 -h0 -e "reload tables"
现在两个表都可用:
mysql -P9315 -h0 -e "show tables"
+-------+-------+
| Table | Type |
+-------+-------+
| idx | local |
| rt | rt |
+-------+-------+
普通模式的局限性
在普通模式下,您无法动态创建表:
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
复制功能也不可用:
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 模式是推荐的选择,因为它的灵活性和功能集。然而,普通模式仍然有其存在的价值,特别是在模式稳定性和配置可移植性优先的环境中。
更喜欢观看而不是阅读?查看我们关于此主题的视频:
请记住,识别您正在使用的模式的关键很简单:如果您的配置中有 data_dir 指令,则您处于 RT 模式;如果没有,则您处于普通模式。
