⚠️ 此页面为自动翻译,翻译可能不完美。
blog-post

Creating Tables in Manticore Search: The Complete Guide

你是否想过如何在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