共用方式為


快速入門:使用 Lucene Index 搜尋適用於 Apache Cassandra 的 Azure 受控實例 (預覽)

Cassandra Lucene Index 衍生自 Stratio Cassandra,是 Apache Cassandra 的外掛程式。 Lucene Index 擴充了其索引功能,提供全文搜索功能,以及多變量、地理空間和雙時間軸的靈活搜尋能力。 它是透過以Apache Lucene為基礎的Cassandra次要索引實作來達成,其中叢集的每個節點都會為其自己的數據編製索引。 本快速入門示範如何使用 Lucene Index 搜尋適用於 Apache Cassandra 的 Azure 受控實例。

重要事項

Lucene Index 處於公開預覽狀態。 此功能已推出但不提供服務等級協定。 不建議將其用於生產工作負載。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款

Lucene Index 外掛程式無法只執行索引中的跨分割區搜尋。 Cassandra 必須將查詢傳送至每個節點。 這項限制可能會在跨分割區搜尋時產生效能(記憶體和 CPU 負載)上的問題,並可能影響穩定狀態的工作負載。

如果您的搜尋需求相當重要,建議您部署專用的次要數據中心,以便只用於搜尋。 應確保最少數量的節點每個都具有高數量的核心(至少 16 個)。 然後,將主要 (作業) 資料中心中的索引鍵空間設定為將資料複寫至次要 (搜尋) 資料中心。

先決條件

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

  • 部署 Azure Managed Instance for Apache Cassandra 叢集。 您可以透過 Azure 入口網站執行此步驟。 從入口網站部署叢集時,預設會啟用 Lucene 索引。 如果您想要將 Lucene 索引新增至現有的叢集,請在入口網站 [概] 窗格上選取 [更新]。 選取 [Cassandra Lucene 索引],然後選取 [ 更新 ] 以部署。

    顯示更新 Cassandra 叢集屬性的螢幕快照。

  • Cassandra 語言查詢工具(CQLSH) 連線到叢集。

使用 Lucene Index 建立資料

  1. 在您的 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
       );
    
  2. 現在,使用 Lucene Index 在資料表上建立自定義次要索引:

       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"}
             }
          }'
       };
    
  3. 插入下列範例推文:

        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);
    

控制讀取一致性

  1. 您稍早建立的索引會使用指定的型別,為數據表中的所有數據行編製索引。 用於搜尋的讀取索引會每秒重新整理一次。 或者,您可以使用內含 ALL consistency 的空白搜尋,明確地重新整理所有索引分區:

        CONSISTENCY ALL
        SELECT * FROM tweets WHERE expr(tweets_index, '{refresh:true}');
        CONSISTENCY QUORUM
    
  2. 現在,您可以搜尋特定日期範圍內的推文:

        SELECT * FROM tweets WHERE expr(tweets_index, '{filter: {type: "range", field: "time", lower: "2023/03/01", upper: "2023/05/01"}}');
    
  3. 您也可以強制明確重新整理相關的索引分區來執行此搜尋:

        SELECT * FROM tweets WHERE expr(tweets_index, '{
           filter: {type: "range", field: "time", lower: "2023/03/01", upper: "2023/05/01"},
           refresh: true
        }') limit 100;
    

搜尋資料

  1. 要搜尋與日期範圍特別相關的前 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;
    
  2. 若要精簡搜尋,只取得名稱開頭為 "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;
    
  3. 若要取得最近篩選的 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;
    
  4. 您可以將先前的搜尋限制為接近地理位置的推文:

        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;
    
  5. 您也可以依地理位置的距離來排序結果:

        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 Index 搜尋 Apache Cassandra 叢集的 Azure 受控實例。 您現在可以開始使用叢集: