我们最近发布了一篇关于 新实时模式的文章 。在这篇文章中,我们将详细了解它是如何工作的,以及如何在您自己身上使用它。我们将学习如何使用SQL语句在Manticore Search中操作索引。
索引创建
首先通过SQL连接到Manticore:
root@rtmode-79f9d6d867-fptcc:/# mysql -P9306 -h0
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 3.4.0 0686d9f0@200326 release
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
一个简单的表创建语句如下:
MySQL [(none)]> CREATE TABLE testrt1 (title TEXT, category INT);
这将创建一个索引,包含文档ID(不需要声明),一个文本字段和一个整数属性。
索引支持在声明时传递的一系列选项:
例如:
MySQL [(none)]> CREATE TABLE testrt2 (title TEXT, category INT) html_strip='1' docstore_compression_level = '12';
在这个索引中,文本将被剥离HTML,文档存储的压缩级别设置为最高级别。
如果未指定类型,默认情况下索引将是实时的。如果我们想创建一个分布式索引,则需要传递类型:
MySQL [(none)]> CREATE TABLE testrtdist type='distributed' local='testrt1' local='testrt2';
如果我们不确定表是否已经创建,可以使用“IF NOT EXISTS”子句来避免错误:
MySQL [(none)]> CREATE TABLE IF NOT EXISTS testrt1 (title TEXT, category INT);
也可以使用已经存在的索引的设置和模式创建一个索引:
MySQL [(none)]> CREATE TABLE testrt3 LIKE testrt2;
更多表操作
要删除一个索引,使用DROP TABLE:
MySQL [(none)]> DROP TABLE testrt3;
要清空一个索引:
MySQL [(none)]> TRUNCATE TABLE testrt2;
要添加或删除一个列:
MySQL [(none)]> ALTER TABLE testrt2 ADD COLUMN tagId INT;
MySQL [(none)]> ALTER TABLE testrt2 DROP COLUMN tagId;
请注意,目前无法添加或删除TEXT字段。如果需要修改TEXT字段,则需要重新创建表。
也可以更改索引设置:
MySQL [(none)]> ALTER TABLE testrt2 ignore_chars='.';
请注意,令牌化设置不会应用于全文本组件中现有的数据,只有在ALTER后添加的新文档会受到影响。
如果需要更新整个集合,必须使用所需的设置重新创建索引。
表信息
有几条语句可以提供关于索引的信息。
要获取索引模式:
MySQL [(none)]> DESCRIBE testrt2;
+----------+--------+----------------+
| Field | Type | Properties |
+----------+--------+----------------+
| id | bigint | |
| title | field | indexed stored |
| category | uint | |
+----------+--------+----------------+
关于文档数量、大小和性能指标的信息可以通过SHOW INDEX STATUS查看:
MySQL [(none)]> SHOW INDEX testrt2 STATUS;
+-------------------+--------------------------------------------------------------------------+
| Variable_name | Value |
+-------------------+--------------------------------------------------------------------------+
| index_type | rt |
| indexed_documents | 0 |
| indexed_bytes | 0 |
| ram_bytes | 5840 |
| disk_bytes | 331 |
| ram_chunk | 0 |
| ram_chunks_count | 0 |
| disk_chunks | 0 |
| mem_limit | 134217728 |
| ram_bytes_retired | 0 |
| tid | 0 |
| tid_saved | 0 |
| query_time_1min | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| query_time_5min | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| query_time_15min | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| query_time_total | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| found_rows_1min | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| found_rows_5min | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| found_rows_15min | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
| found_rows_total | {"queries":0, "avg":"-", "min":"-", "max":"-", "pct95":"-", "pct99":"-"} |
+-------------------+--------------------------------------------------------------------------+
SHOW INDEX SETTINGS也可以提供索引设置的列表:
MySQL [(none)]> SHOW INDEX testrt2 SETTINGS;
+---------------+----------------------------------------------------------+
| Variable_name | Value |
+---------------+----------------------------------------------------------+
| settings | html_strip = 1 |
| | charset_table = non_cjk |
| | ignore_chars = . |
+---------------+----------------------------------------------------------+
如果您需要导出完整的索引声明,请使用 SHOW CREATE TABLE:
MySQL [(none)]> SHOW CREATE TABLE testrt2;
+---------+-------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-------------------------------------------------------------------------------------------------------------------------------+
| testrt2 | CREATE TABLE testrt2 (category integer,title text indexed stored) html_strip='1' charset_table='non_cjk' ignore_chars='.' |
+---------+-------------------------------------------------------------------------------------------------------------------------------+
导入索引
RT 索引可以使用 IMPORT TABLE 语句导入。
索引文件将被复制到您在配置中指定的 data_dir。
示例:
MySQL [(none)]> IMPORT TABLE movies FROM '/index/movies_rt';
MySQL [(none)]> SELECT * FROM moviesG
*************************** 1. row ***************************
id: 1
num_critic_for_reviews: 4
duration: 96
director_facebook_likes: 0
actor_3_facebook_likes: 460
actor_1_facebook_likes: 708
gross: 0
num_voted_users: 961
cast_total_facebook_likes: 2307
facenumber_in_poster: 0
num_user_for_reviews: 12
budget: 3500000
title_year: 2009
actor_2_facebook_likes: 574
movie_facebook_likes: 211
imdb_score: 4.800000
aspect_ration: 2.350000
color: Color
movie_imdb_link: http://www.imdb.com/title/tt1002561/?ref_=fn_tt_tt_1
language: English
country: USA
content_rating:
director_name: Charles Adelman
actor_2_name: Kevin Pollak
actor_1_name: Teri Polo
movie_title: 02:13
actor_3_name: Jere Burns
plot_keywords: death|forensic|murder|profiler|serial killer
...
*************************** 20. row ***************************
id: 20
num_critic_for_reviews: 1
duration: 111
director_facebook_likes: 0
actor_3_facebook_likes: 247
actor_1_facebook_likes: 1000
gross: 14616
num_voted_users: 314
cast_total_facebook_likes: 2059
facenumber_in_poster: 1
num_user_for_reviews: 10
budget: 12000000
title_year: 2015
actor_2_facebook_likes: 445
movie_facebook_likes: 26000
imdb_score: 7.500000
aspect_ration: 1.850000
color: Color
movie_imdb_link: http://www.imdb.com/title/tt3453052/?ref_=fn_tt_tt_1
language: English
country: USA
content_rating: R
director_name: Timothy Hines
actor_2_name: Kelly LeBrock
actor_1_name: Christopher Lambert
movie_title: 10 Days in a Madhouse
actor_3_name: Alexandra Callas
plot_keywords:
我们相信新的 RT 模式作为新的默认模式将在大多数情况下更容易维护 Manticore Search 中的索引。对于其他情况,纯模式仍然受支持。您可以从
我们的文档
中了解差异。
<img src="RT-mode-index-optimized.webp" alt="img">
互动课程
您可以在我们的 “ManticoreSearch - RT mode - index administration” 互动课程中了解更多关于实时索引管理的知识,该课程提供了命令行以便于学习。