dimamik
Torus - Integrate PostgreSQL's search into Ecto queries
Torus is a plug-and-play Elixir library that seamlessly integrates PostgreSQL’s search into Ecto, streamlining the construction of advanced search queries.
The goal is to help developers create Ecto queries that return relevant results with optimal performance, and it’s best to show how on an example:
-
Pattern matching: Searches for a specific pattern in a string.
iex> insert_posts!(["Wand", "Magic wand", "Owl"]) ...> Post ...> |> Torus.ilike([p], [p.title], "wan%") ...> |> select([p], p.title) ...> |> Repo.all() ["Wand"]See
like/5,ilike/5, andsimilar_to/5for more details. -
Similarity: Searches for items that are closely alike based on attributes, often using measures like cosine similarity or Euclidean distance. Is great for fuzzy searching and ignoring typos in short texts.
iex> insert_posts!(["Hogwarts Secrets", "Quidditch Fever", "Hogwart’s Secret"]) ...> Post ...> |> Torus.similarity([p], [p.title], "hoggwarrds") ...> |> limit(2) ...> |> select([p], p.title) ...> |> Repo.all() ["Hogwarts Secrets", "Hogwart’s Secret"]See
similarity/5for more details. -
Text Search Vectors: Uses term-document matrix vectors for full-text search, enabling efficient querying and ranking based on term frequency. - PostgreSQL: Full Text Search. Is great for large datasets to quickly return relevant results.
iex> insert_post!(title: "Hogwarts Shocker", body: "A spell disrupts the Quidditch Cup.") ...> insert_post!(title: "Diagon Bombshell", body: "Secrets uncovered in the heart of Hogwarts.") ...> insert_post!(title: "Completely unrelated", body: "No magic here!") ...> Post ...> |> Torus.full_text([p], [p.title, p.body], "uncov hogwar") ...> |> select([p], p.title) ...> |> Repo.all() ["Diagon Bombshell"]See
full_text/5for more details. -
Semantic Search: Understands the contextual meaning of queries to match and retrieve related content utilizing natural language processing. Read more about semantic search in Semantic search with Torus guide.
insert_post!(title: "Hogwarts Shocker", body: "A spell disrupts the Quidditch Cup.") insert_post!(title: "Diagon Bombshell", body: "Secrets uncovered in the heart of Hogwarts.") insert_post!(title: "Completely unrelated", body: "No magic here!") embedding_vector = Torus.to_vector("A magic school in the UK") Post |> Torus.semantic([p], p.embedding, embedding_vector) |> select([p], p.title) |> Repo.all() ["Diagon Bombshell"]See
semantic/5for more details.
The above macros accept a list of options to customize their behavior. See function docs for examples. Most functions have an optimization section that might help you boost the performance of these search queries.
In upcoming plans, we’ll add support for highlighting search results and extend the search with hybrid search. Please let me know what do you think of it, and I’ll gladly hear any suggestions on how to make it better!
Links
- Torus — Torus v0.6.0
- GitHub:
Most Liked
dimamik
Hey!
Semantic search is finally here! Read more in Semantic search with Torus guide.
Shortly - it allows you to generate embeddings using a configurable (and chainable) adapters and use them to compare against the ones stored in your database.
Supported adapters (for now):
-
Torus.Embeddings.OpenAI- uses OpenAI’s API to generate embeddings. -
Torus.Embeddings.HuggingFace- uses HuggingFace’s API to generate embeddings. -
Torus.Embeddings.LocalNxServing- generate embeddings on your local machine using a variety of models available on Hugging Face -
Torus.Embeddings.PostgresML- uses PostgreSQL PostgresML extension to generate embeddings -
Torus.Embeddings.Batcher- a long‑running GenServer that collects individual embedding calls, groups them into a single batch, and forwards the batch to the configuredembedding_module(any from the above or your custom one). -
Torus.Embeddings.NebulexCache- a wrapper around Nebulex cache, allowing you to cache the embedding calls in memory, so you save the resources/cost of calling the embedding module multiple times for the same input.
And you can easily create your own adapter by implementing the Torus.Embedding behaviour.
So after you’ll pick your favorite embedding adapter, you can add semantic search:
def search(term) do
search_vector = Torus.to_vector(term)
Post
|> Torus.semantic([p], p.embedding, search_vector, distance: :l2_distance, pre_filter: 0.7)
|> Repo.all()
end
Future plans:
- Release
torus_exampleapp that would allow experimenting with options and different search types to pick the best one - Add support for highlighting search results. (Base off of a
ts_headlinefunction) - Extend similarity search to support
fuzzystrmatchextension distance options.
Links
dimamik
Torus v0.5.2 is released!
New 
- New demo page where you can explore different search types and their options. It also includes semantic search, so if you’re hesitant - go check it out!
- Other documentation improvements
Fixes
- Correctly handles
order: :noneinTorus.semantic/5search. - Updates
Torus.Embeddings.HuggingFaceto point to the updated feature extraction endpoint. - Suppresses warnings for missing
ecto_sqldependency by adding it to the required dependencies. Most of us already had it, but now it’ll be explicit. - Correctly parses an array of integers in
Torus.QueryInspector.substituted_sql/3andTorus.QueryInspector.tap_substituted_sql/3. Now we should be able to handle all possible query variations.
Torus search demo · Phoenix Framework
D4no0
What is the difference between the ecto like/ilike and the one provided by your library?
dimamik
There are a few options, either in postgresql.conf or per-database in a migration:
defmodule YourApp.Repo.Migrations.SetWordSimilarityThreshold do
use Ecto.Migration
@database Application.compile_env!(:your_app_name, YourApp.Repo)[:database]
def up do
execute("""
ALTER DATABASE #{@database} SET pg_trgm.word_similarity_threshold = 0.5;
""")
end
def down do
execute("""
ALTER DATABASE #{@database} RESET pg_trgm.word_similarity_threshold;
""")
end
end
gushonorato
Looks interesting! I saw that all the examples were using “function-style queries.” Does Torus support “macro-style” queries? If not, are you planning to add support for them?







