blog-post

在 Manticore Search 中创建表:完整指南

曾经想过如何在 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 为基础的管理
  • 表克隆以进行快速复制
  • 配置文件以应对高级场景

每种方法都有其使用场景,选择合适的方法取决于您的具体需求。实时模式(默认)为大多数使用场景提供了简单性和灵活性,而配置文件方法则提供了更多控制权,并且更容易在服务器之间部署。

读者挑战:尝试使用每种方法创建一个表格,看看哪一种最适合您的工作流程!

更喜欢观看而不是阅读?查看我们关于此主题的详细视频教程:

安装Manticore Search

安装Manticore Search