使用 Spanner Graph 進行全文搜尋

本頁面說明如何在 Spanner Graph 中使用全文搜尋

Spanner Graph 將圖形和全文搜尋功能整合在單一系統中。這兩者結合後,您就能根據圖表中的關聯,從非結構化資料中擷取洞察資料。

事前準備

如要執行本頁的範例,您必須執行使用 Google Cloud 控制台程序設定及查詢 Spanner 圖表。這些程序會執行以下操作:

  1. 建立執行個體
  2. 建立資料庫
  3. 為 Spanner 圖資料庫建立結構定義
  4. 插入圖表資料

建立符記和搜尋索引

使用全文搜尋的第一個步驟,就是將要搜尋的內容切割成符號,並建立搜尋索引。全文搜尋會針對搜尋索引執行查詢。

以下範例會新增 nick_name_token 欄,並使用 TOKENIZE_FULLTEXT 函式將 Account.nick_name 欄中的文字切割為子字串。接著,系統會在 nick_name_token 欄上建立搜尋索引。

ALTER TABLE Account
ADD COLUMN nick_name_token TOKENLIST
AS (TOKENIZE_FULLTEXT(nick_name)) STORED HIDDEN;

CREATE SEARCH INDEX AccountTextSearchIndex
ON Account(nick_name_token) STORING (nick_name);

以下範例使用 TOKENIZE_FULLTEXT 函式將 Account.nick_name 中的文字切割為子字串,並在包含子字串的 nick_name_token 欄上建立搜尋索引。

ALTER TABLE AccountTransferAccount
ADD COLUMN notes STRING(MAX);
ALTER TABLE AccountTransferAccount
ADD COLUMN notes_token TOKENLIST AS (TOKENIZE_FULLTEXT(notes)) STORED HIDDEN;

CREATE SEARCH INDEX TransferTextSearchIndex
ON AccountTransferAccount(notes_token) STORING (notes);

由於 AccountAccountTransferAccount 新增了一些資料欄,且您會在搜尋函式中以新圖表屬性存取這些資料欄,因此您必須使用下列陳述式更新屬性圖表定義 (詳情請參閱「更新現有節點或邊定義」)。

CREATE OR REPLACE PROPERTY GRAPH FinGraph
  NODE TABLES (Account, Person)
  EDGE TABLES (
    PersonOwnAccount
      SOURCE KEY (id) REFERENCES Person (id)
      DESTINATION KEY (account_id) REFERENCES Account (id)
      LABEL Owns,
    AccountTransferAccount
      SOURCE KEY (id) REFERENCES Account (id)
      DESTINATION KEY (to_id) REFERENCES Account (id)
      LABEL Transfers
  );

您現在可以對圖表資料使用全文搜尋功能。

搜尋圖節點屬性

本例說明如何在圖表中搜尋節點,並探索其關係。

  1. 使用一些文字訊息更新 Account.nick_name

    UPDATE Account SET nick_name = "Fund for vacation at the north pole" WHERE id = 7;
    UPDATE Account SET nick_name = "Fund -- thrill rides!" WHERE id = 16;
    UPDATE Account SET nick_name = "Rainy day fund for the things I still want to do" WHERE id = 20;
    
  2. 使用 SEARCH 函式,找出圖表中 nick_name 有「rainy day」或「vacation」的 Account 節點。使用圖表檢索功能找出匯入這些帳戶的金額。依據搜尋關聯性評分相符項目。依相關性遞減順序排序並傳回結果。請注意,您可以在相同的搜尋函式呼叫中尋找符記的析取。

    GRAPH FinGraph
    MATCH (n:Account)<-[e:Transfers]-(:Account)
    WHERE SEARCH(n.nick_name_token, '"rainy day" | vacation')
    RETURN n.nick_name, e.amount AS amount_added
    ORDER BY SCORE(n.nick_name_token, '"rainy day" | vacation') DESC
    

    結果:

    nick_name                                             amount_added
    Rainy day fund for the things I still want to do      300
    Fund for vacation at the north pole                   500
    

搜尋圖表邊緣屬性

本範例說明如何在圖表中搜尋特定邊

  1. 透過簡訊更新 AccountTransferAccount.notes

    UPDATE AccountTransferAccount SET notes = 'for trip fund'
    WHERE id = 16 AND to_id = 20;
    UPDATE AccountTransferAccount SET notes = '&lt;trip&#39;s very fun!&gt;'
    WHERE id = 20 AND to_id = 7;
    UPDATE AccountTransferAccount SET notes = 'book fee'
    WHERE id = 20 AND to_id = 16;
    
  2. 使用全文搜尋功能,找出含有「trip」的 Transfers 邊。使用圖表檢索功能找出這些轉移作業的來源和目的地節點。

    GRAPH FinGraph
    MATCH (a:Account)-[e:Transfers WHERE SEARCH(e.notes_token, 'trip')]->(b:Account)
    RETURN a.id AS src_id, b.id AS dst_id, e.notes
    

    結果:

    src_id  dst_id  notes
    
    20      7      &lt;trip&#39;s very fun!&gt;
    16      20     for trip fund
    

儘管文字中含有 HTML 標記,搜尋功能仍可正確喚回第一個結果。

後續步驟