你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
从 Stratio Cassandra 派生的 Cassandra Lucene Index 是 Apache Cassandra 的插件。 Lucene Index 扩展了其索引功能,提供全文搜索能力和无限制通用的多变量、地理空间及双时空搜索。 它通过基于 Apache Lucene 的 Cassandra 辅助索引实现来实现,其中群集的每个节点为其自己的数据编制索引。 本快速入门演示如何使用 Lucene Index 搜索 Azure Managed Instance for Apache Cassandra。
重要说明
Lucene Index 为公共预览版。 此功能不附带服务级别协议。 建议不要将它用于生产工作负载。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款。
Lucene 索引插件不能仅在索引中执行跨分区搜索。 Cassandra 需要将查询发送到每个节点。 对于可能影响稳定状态工作负荷的跨分区搜索,此限制可能会导致性能(内存和 CPU 负载)出现问题。
如果搜索要求非常重要,建议部署专用辅助数据中心以仅用于搜索。 节点数最少,每个节点应具有大量核心(最小为 16 个)。 然后,应将主(作)数据中心中的密钥空间配置为将数据复制到辅助(搜索)数据中心。
Prerequisites
如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
部署 Azure Managed Instance for Apache Cassandra 群集。 可以通过 Azure 门户执行此步骤。 从门户部署群集时,默认情况下会启用 Lucene 索引。 如果要将 Lucene 索引添加到现有群集,请在门户“概述”窗格中选择“更新”。 选择 Cassandra Lucene 索引,然后选择更新进行部署。
从 Cassandra 查询语言命令行界面(CQLSH)连接到群集。
使用 Lucene Index 创建数据
在 CQLSH 命令窗口中,创建密钥空间和表:
CREATE KEYSPACE demo WITH REPLICATION = {'class': 'NetworkTopologyStrategy', 'datacenter-1': 3}; USE demo; CREATE TABLE tweets ( id INT PRIMARY KEY, user TEXT, body TEXT, time TIMESTAMP, latitude FLOAT, longitude FLOAT );
现在,使用 Lucene 索引对表创建自定义辅助索引:
CREATE CUSTOM INDEX tweets_index ON tweets () USING 'com.stratio.cassandra.lucene.Index' WITH OPTIONS = { 'refresh_seconds': '1', 'schema': '{ fields: { id: {type: "integer"}, user: {type: "string"}, body: {type: "text", analyzer: "english"}, time: {type: "date", pattern: "yyyy/MM/dd"}, place: {type: "geo_point", latitude: "latitude", longitude: "longitude"} } }' };
插入以下示例推文:
INSERT INTO tweets (id,user,body,time,latitude,longitude) VALUES (1,'theo','Make money fast, 5 easy tips', '2023-04-01T11:21:59.001+0000', 0.0, 0.0); INSERT INTO tweets (id,user,body,time,latitude,longitude) VALUES (2,'theo','Click my link, like my stuff!', '2023-04-01T11:21:59.001+0000', 0.0, 0.0); INSERT INTO tweets (id,user,body,time,latitude,longitude) VALUES (3,'quetzal','Click my link, like my stuff!', '2023-04-02T11:21:59.001+0000', 0.0, 0.0); INSERT INTO tweets (id,user,body,time,latitude,longitude) VALUES (4,'quetzal','Click my link, like my stuff!', '2023-04-01T11:21:59.001+0000', 40.3930, -3.7328); INSERT INTO tweets (id,user,body,time,latitude,longitude) VALUES (5,'quetzal','Click my link, like my stuff!', '2023-04-01T11:21:59.001+0000', 40.3930, -3.7329);
控制读取一致性
前面创建的索引为表中具有指定类型的所有列编制索引。 用于搜索的读取索引每秒刷新一次。 或者,可以使用一致性为
ALL
的空搜索显式刷新所有索引分片:CONSISTENCY ALL SELECT * FROM tweets WHERE expr(tweets_index, '{refresh:true}'); CONSISTENCY QUORUM
现在,可以搜索特定日期范围内的推文:
SELECT * FROM tweets WHERE expr(tweets_index, '{filter: {type: "range", field: "time", lower: "2023/03/01", upper: "2023/05/01"}}');
还可以通过强制显式刷新所涉及的索引分片来执行此搜索:
SELECT * FROM tweets WHERE expr(tweets_index, '{ filter: {type: "range", field: "time", lower: "2023/03/01", upper: "2023/05/01"}, refresh: true }') limit 100;
搜索数据
要搜索内容更相关的前 100 条推文,其中
body
字段包含短语Click my link
,并且这些推文位于特定日期范围内:SELECT * FROM tweets WHERE expr(tweets_index, '{ filter: {type: "range", field: "time", lower: "2023/03/01", upper: "2023/05/01"}, query: {type: "phrase", field: "body", value: "Click my link", slop: 1} }') LIMIT 100;
若要优化搜索,仅获取名称以“q”开头的用户所写的推文:
SELECT * FROM tweets WHERE expr(tweets_index, '{ filter: [ {type: "range", field: "time", lower: "2023/03/01", upper: "2023/05/01"}, {type: "prefix", field: "user", value: "q"} ], query: {type: "phrase", field: "body", value: "Click my link", slop: 1} }') LIMIT 100;
若要获取最近筛选的 100 个结果,可以使用排序选项:
SELECT * FROM tweets WHERE expr(tweets_index, '{ filter: [ {type: "range", field: "time", lower: "2023/03/01", upper: "2023/05/01"}, {type: "prefix", field: "user", value: "q"} ], query: {type: "phrase", field: "body", value: "Click my link", slop: 1}, sort: {field: "time", reverse: true} }') limit 100;
可以将上一次搜索限制为靠近地理位置的推文:
SELECT * FROM tweets WHERE expr(tweets_index, '{ filter: [ {type: "range", field: "time", lower: "2023/03/01", upper: "2023/05/01"}, {type: "prefix", field: "user", value: "q"}, {type: "geo_distance", field: "place", latitude: 40.3930, longitude: -3.7328, max_distance: "1km"} ], query: {type: "phrase", field: "body", value: "Click my link", slop: 1}, sort: {field: "time", reverse: true} }') limit 100;
还可以按与地理位置的距离对结果进行排序:
SELECT * FROM tweets WHERE expr(tweets_index, '{ filter: [ {type: "range", field: "time", lower: "2023/03/01", upper: "2023/05/01"}, {type: "prefix", field: "user", value: "q"}, {type: "geo_distance", field: "place", latitude: 40.3930, longitude: -3.7328, max_distance: "1km"} ], query: {type: "phrase", field: "body", value: "Click my link", slop: 1}, sort: [ {field: "time", reverse: true}, {field: "place", type: "geo_distance", latitude: 40.3930, longitude: -3.7328} ] }') limit 100;
后续步骤
在本快速入门中,您学习了如何通过使用 Lucene 索引来搜索适用于 Apache Cassandra 的 Azure 托管实例集群。 现在你可开始使用该群集了: