View Source LadybugEx.FTS (LadybugEx v0.2.0)
Interface for full-text search using the fts extension.
This module provides high-level functions for creating and querying BM25-based full-text search indexes in LadybugDB.
Prerequisites
The fts extension must be installed and loaded before using this module:
# Install once
LadybugEx.Extensions.install(conn, "fts")
# Load for each connection
LadybugEx.Extensions.load(conn, "fts")This module will automatically ensure the extension is loaded when calling its functions.
Supported Stemmers
:arabic,:danish,:dutch,:english,:finnish,:french,:german,:greek,:hungarian,:indonesian,:irish,:italian,:lithuanian,:nepali,:norwegian,:porter,:portuguese,:romanian,:russian,:spanish,:swedish,:tamil,:turkish
Usage Examples
# Create a full-text index on a single property
{:ok, _} = FTS.create_index(conn, "Article",
properties: ["content"],
stemmer: :english
)
# Create index on multiple properties
{:ok, _} = FTS.create_index(conn, "Product",
properties: ["name", "description"],
stemmer: :english,
stopwords: ["the", "a", "an"]
)
# Query the index
{:ok, results} = FTS.query_index(conn, "Article", "article_fts_idx",
query: "machine learning algorithms",
limit: 20
)
# Drop the index
{:ok, _} = FTS.drop_index(conn, "Article", "article_fts_idx")
Summary
Functions
Analyze text using the FTS stemmer.
Analyze text using the FTS stemmer.
Create a full-text search index on a node or relationship table.
Create a full-text search index on a node or relationship table.
Drop a full-text search index.
Drop a full-text search index.
List all full-text search indexes in the database.
List all full-text search indexes in the database.
Query a full-text search index.
Query a full-text search index.
Types
Functions
@spec analyze_text(LadybugEx.Connection.t(), String.t(), stemmer()) :: {:ok, [String.t()]} | {:error, String.t()}
Analyze text using the FTS stemmer.
Useful for debugging and understanding how text is processed.
Examples
{:ok, tokens} = FTS.analyze_text(conn, "running quickly", :english)
# Returns: ["run", "quick"]
@spec analyze_text!(LadybugEx.Connection.t(), String.t(), stemmer()) :: [String.t()]
Analyze text using the FTS stemmer.
Raises an error if the analysis fails.
@spec create_index(LadybugEx.Connection.t(), String.t(), keyword()) :: {:ok, any()} | {:error, String.t()}
Create a full-text search index on a node or relationship table.
Options
:properties- (required) List of property names to index:stemmer- Language stemmer to use (default::english):stopwords- List of stopwords to exclude (default: language-specific):index_name- Custom index name (default: auto-generated)
Examples
# Index a single property
{:ok, _} = FTS.create_index(conn, "Article",
properties: ["content"]
)
# Index multiple properties with custom stemmer
{:ok, _} = FTS.create_index(conn, "Document",
properties: ["title", "abstract", "body"],
stemmer: :french
)
# With custom stopwords
{:ok, _} = FTS.create_index(conn, "Product",
properties: ["description"],
stemmer: :english,
stopwords: ["the", "a", "an", "and", "or", "but"]
)
@spec create_index!(LadybugEx.Connection.t(), String.t(), keyword()) :: any()
Create a full-text search index on a node or relationship table.
Raises an error if the index creation fails.
Examples
FTS.create_index!(conn, "Article",
properties: ["content"]
)
@spec drop_index(LadybugEx.Connection.t(), String.t(), String.t()) :: {:ok, any()} | {:error, String.t()}
Drop a full-text search index.
Examples
{:ok, _} = FTS.drop_index(conn, "Article", "article_fts_idx")
@spec drop_index!(LadybugEx.Connection.t(), String.t(), String.t()) :: any()
Drop a full-text search index.
Raises an error if dropping the index fails.
Examples
FTS.drop_index!(conn, "Article", "article_fts_idx")
@spec list_indexes(LadybugEx.Connection.t()) :: {:ok, [map()]} | {:error, String.t()}
List all full-text search indexes in the database.
Examples
{:ok, indexes} = FTS.list_indexes(conn)
Enum.each(indexes, fn idx ->
IO.inspect({idx["table"], idx["index_name"], idx["properties"]})
end)
@spec list_indexes!(LadybugEx.Connection.t()) :: [map()]
List all full-text search indexes in the database.
Raises an error if the listing fails.
@spec query_index(LadybugEx.Connection.t(), String.t(), String.t(), keyword()) :: {:ok, [search_result()]} | {:error, String.t()}
Query a full-text search index.
Options
:query- (required) Search query string:conjunctive- Use AND logic for multi-term queries (default: true):k- BM25 k1 parameter (default: 1.2):b- BM25 b parameter (default: 0.75):limit- Maximum number of results (default: 10)
Return Format
Returns a list of results, each containing:
"node"- The matched node"score"- BM25 relevance score
Examples
# Basic query
{:ok, results} = FTS.query_index(conn, "Article", "article_fts_idx",
query: "machine learning"
)
# With OR logic and custom limit
{:ok, results} = FTS.query_index(conn, "Product", "product_fts_idx",
query: "laptop computer notebook",
conjunctive: false,
limit: 50
)
# With custom BM25 parameters
{:ok, results} = FTS.query_index(conn, "Document", "doc_fts_idx",
query: "data science",
k: 1.5,
b: 0.8,
limit: 100
)
@spec query_index!(LadybugEx.Connection.t(), String.t(), String.t(), keyword()) :: [ search_result() ]
Query a full-text search index.
Raises an error if the query fails.
Examples
results = FTS.query_index!(conn, "Article", "article_fts_idx",
query: "machine learning"
)