Skip to content

Commit

Permalink
Chore: Fulltext index example
Browse files Browse the repository at this point in the history
  • Loading branch information
mindreframer committed Nov 9, 2023
1 parent ee6c138 commit 1ddd146
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/surrealdb/statements/define_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,41 @@ defmodule DefineTest do
)
end
end

describe "Define :: Index" do
setup [:setup_surrealix]

test "define / index with SQL", %{pid: pid} do
##
sql_setup = ~S|
-- https://surrealdb.com/docs/surrealql/statements/define/analyzer
-- we create an analyzer that tokenizes by class (digit / words / punctuation),
-- and filters by stemming with german stemmer and building edgengram tokens (for partial matches)
DEFINE ANALYZER german TOKENIZERS class FILTERS snowball(german), edgengram(1,3);
-- now we use this analyzer on the book table for title columns (could be more)
DEFINE INDEX idx_book_title ON TABLE book COLUMNS title SEARCH ANALYZER german BM25 HIGHLIGHTS;
DEFINE INDEX idx_book_desc ON TABLE book COLUMNS description SEARCH ANALYZER german BM25 HIGHLIGHTS;
CREATE book:1 set title = "Rust Web Development";
CREATE book:2 set description = "Ruby Web Development";
|
sql = ~S|
SELECT * FROM book WHERE title @@ 'rust web' or description @@ 'rust web';
SELECT * FROM book WHERE title @@ 'rust dev';
SELECT * FROM book WHERE title or description @@ 'ruby dev';
|

{:ok, _} = Surrealix.query(pid, sql_setup)
parsed = Surrealix.query(pid, sql) |> extract_res_list()

auto_assert(
[
ok: [%{"id" => "book:1", "title" => "Rust Web Development"}],
ok: [%{"id" => "book:1", "title" => "Rust Web Development"}],
ok: [%{"description" => "Ruby Web Development", "id" => "book:2"}]
] <- parsed
)
end
end
end

0 comments on commit 1ddd146

Please sign in to comment.