快速入門:使用 Lucene Index (Preview) 搜尋適用于 Apache Cassandra 的 Azure 受控執行個體

Cassandra Lucene Index 衍生自 Stratio Cassandra,是 Apache Cassandra 的外掛程式,可擴充其索引功能,以提供全文檢索搜尋功能,以及免費的多重變數、地理空間和 bitemporal 搜尋。 它是透過以 Apache Lucene 為基礎的 Cassandra 次要索引實作來達成,其中叢集的每個節點都會為自己的資料編制索引。 本快速入門示範如何使用 Lucene Index 搜尋 Azure 受控執行個體以尋找 Apache Cassandra。

重要

Lucene Index 處於公開預覽狀態。 此功能是在沒有服務等級協定的情況下提供,不建議用於生產工作負載。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款

警告

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

在搜尋需求相當重要的情況下,我們建議部署專用的次要資料中心,只用于搜尋,且節點數目最少,每個核心數目最少 (至少 16) 。 主要 (作業) 資料中心中的索引鍵空間應設定為將資料複寫至次要 (搜尋) 資料中心。

必要條件

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

  • 部署適用于 Apache Cassandra 叢集的 Azure 受控執行個體。 您可以透過 入口網站 執行此動作 - 從入口網站部署叢集時,預設會啟用 Lucene 索引。 如果您想要將 Lucene 索引新增至現有的叢集,請按一下 Update 入口網站概觀刀鋒視窗,選取 Cassandra Lucene Index ,然後按一下 [更新] 以部署。

    更新 Cassandra 叢集屬性的螢幕擷取畫面。

  • CQLSH連線到您的叢集。

使用 Lucene 索引建立資料

  1. 在您的 CQLSH 命令視窗中,建立 keyspace 和資料表,如下所示:

       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 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 個更相關的推文,其中本文欄位包含特定日期範圍內的「按一下我的連結」片語:

        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 搜尋來搜尋 Azure 受控執行個體以尋找 Apache Cassandra 叢集。 您現在可以開始使用叢集: