你是否曾想过如何在 Manticore Search 中创建表格?无论你是刚开始还是希望优化你的设置,我们都能为你提供帮助。在本指南中,我们将探讨多种表格创建方法 - 从最简单的自动模式到更高级的配置选项。
自动模式的魔力:零配置要求
如果我们告诉你,你甚至不需要显式创建表格呢?得益于 Manticore 的自动模式功能,你可以立即开始插入数据。让我们看看这个魔法是如何实现的:
drop table if exists test;
Query OK, 0 rows affected (0.00 sec)
insert into test (name, in_stock, price, properties) values('nice bag', 117, 1.15, '{"color": "red"}');
Query OK, 1 row affected (0.01 sec)
就是这样!表格会自动创建。让我们检查一下它的结构:
desc test;
+------------+--------+----------------+
| Field | Type | Properties |
+------------+--------+----------------+
| id | bigint | |
| name | text | indexed stored |
| in_stock | uint | |
| price | float | |
| properties | json | |
+------------+--------+----------------+
5 rows in set (0.01 sec)
我们的数据已经在那里了:
select * from test;
+---------------------+----------+----------+----------+-----------------+
| id | name | in_stock | price | properties |
+---------------------+----------+----------+----------+-----------------+
| 1516257118578016257 | nice bag | 117 | 1.150000 | {"color":"red"} |
+---------------------+----------+----------+----------+-----------------+
1 row in set (0.00 sec)
--- 1 out of 1 results in 1ms ---
专业提示:虽然自动模式很方便,但它也有其权衡。系统根据你的第一份文档检测字段类型,这可能并不总是符合你的意图。例如,你可能想要一个字符串属性,但它可能会被创建为全文字段。
手动创建表格:掌控全局
当你需要精确控制表格结构时,手动创建是最佳选择。以下是使用 SQL 的方法:
drop table if exists test;
Query OK, 0 rows affected (0.01 sec)
create table test(name text, in_stock int, price float, properties json);
Query OK, 0 rows affected (0.00 sec)
HTTP 方式:用于表格创建的 REST API
喜欢使用 HTTP 吗?Manticore 可以满足你的需求!你可以使用 /cli 端点来管理你的表格:
curl "0:9308/cli" -d 'drop table if exists test'
Query OK, 0 rows affected (0.002 sec)
curl "0:9308/cli" -d 'create table test(name text, in_stock int, price float, properties json)'
Query OK, 0 rows affected (0.011 sec)
curl "0:9308/cli" -d 'desc test'
+------------+--------+----------------+
| Field | Type | Properties |
+------------+--------+----------------+
| id | bigint | |
| name | text | indexed stored |
| in_stock | uint | |
| price | float | |
| properties | json | |
+------------+--------+----------------+
5 rows in set (0.001 sec)
克隆表格:更聪明地工作,而不是更辛苦
需要一个与现有表格结构相同的表格吗?CREATE TABLE LIKE 语句是你的好帮手:
drop table if exists test2;
Query OK, 0 rows affected (0.00 sec)
create table test2 like test;
Query OK, 0 rows affected (0.01 sec)
desc test2;
+------------+--------+----------------+
| Field | Type | Properties |
+------------+--------+----------------+
| id | bigint | |
| name | text | indexed stored |
| in_stock | uint | |
| price | float | |
| properties | json | |
+------------+--------+----------------+
5 rows in set (0.00 sec)
高级配置:使用配置文件
虽然实时模式是推荐的默认设置,但有时你需要更多控制或想要提前声明一些内容。这时配置文件就派上用场了。以下是一个示例配置,演示了实时表格和普通表格的结合:
searchd {
listen = 9315:mysql41
log = searchd.log
pid_file = searchd.pid
binlog_path =
}
source src {
type = csvpipe
csvpipe_command = echo "1,abc" && echo "3,def"
csvpipe_field = F
}
table plain {
type = plain
source = src
path = /tmp/plain
}
table goods {
type = rt
path = /tmp/rt
rt_field = title
rt_attr_uint = quantity
rt_attr_float = price
}
在使用此配置启动 Manticore 后:
mysql -P9315 -h0 -e "show tables"
+-------+------+
| Table | Type |
+-------+------+
| goods | rt |
+-------+------+
要创建并填充普通表格,我们需要运行索引器:
indexer -c manticore_test.conf --all --rotate
在重新加载表格后:
mysql -P9315 -h0 -e "show tables"
+-------+-------+
| Table | Type |
+-------+-------+
| goods | rt |
| plain | local |
+-------+-------+
mysql -P9315 -v -h0 -e "select * from goods; select * from plain"
--------------
select * from goods
--------------
--------------
select * from plain
--------------
+------+------+
| id | f |
+------+------+
| 1 | abc |
| 3 | def |
+------+------+
总结
我们已经介绍了在 Manticore Search 中创建表格的多种方法:
- 自动模式以快速启动
- 手动创建以精确控制
- HTTP API 以基于 REST 的管理
- 表格克隆以快速复制
- 配置文件以应对高级场景
每种方法都有其使用场景,选择合适的方法取决于你的具体需求。实时模式(默认)为大多数用例提供了简单性和灵活性,而配置文件方法则提供了更多控制和更容易在服务器之间部署。
读者挑战:尝试使用每种方法创建一个表格,看看哪种最适合你的工作流程!
喜欢观看而不是阅读?查看我们关于此主题的详细视频教程:
