TantivyEx.Searcher (TantivyEx v0.4.1)
View SourceSearcher for querying a TantivyEx index.
The Searcher provides functionality to search documents in an index and retrieve search results using various query types including:
- Simple string queries (parsed automatically)
- Complex query objects built with TantivyEx.Query
- Boolean combinations of multiple queries
Summary
Functions
Creates a new Searcher for the given index.
Searches the index with the given query.
Performs a search and returns full documents with metadata.
Searches the index and returns only document IDs.
Performs a search with a query parser for Lucene-style queries.
Types
@type search_result() :: %{score: float(), doc_id: pos_integer(), document: map()}
@type t() :: reference()
Functions
@spec new(TantivyEx.Index.t()) :: {:ok, t()} | {:error, String.t()}
Creates a new Searcher for the given index.
Parameters
index: The index to search
Examples
iex> {:ok, searcher} = TantivyEx.Searcher.new(index)
iex> is_reference(searcher)
true
@spec search(t(), String.t() | TantivyEx.Query.t(), pos_integer(), boolean()) :: {:ok, [search_result()]} | {:error, String.t()}
Searches the index with the given query.
This function supports both string queries (which are parsed using AllQuery for now) and Query objects created with TantivyEx.Query functions.
Parameters
searcher: The Searcherquery: The search query (string or Query.t())limit: Maximum number of results to return (default: 10)include_docs: Whether to include full document content (default: true)
Examples
# Simple string search (legacy - uses AllQuery)
iex> {:ok, results} = TantivyEx.Searcher.search(searcher, "hello world", 10)
# Using Query objects for precise control
iex> {:ok, query} = TantivyEx.Query.term(schema, "title", "hello")
iex> {:ok, results} = TantivyEx.Searcher.search(searcher, query, 10)
# Boolean query example
iex> {:ok, term1} = TantivyEx.Query.term(schema, "title", "hello")
iex> {:ok, term2} = TantivyEx.Query.term(schema, "body", "world")
iex> {:ok, boolean_query} = TantivyEx.Query.boolean([term1], [term2], [])
iex> {:ok, results} = TantivyEx.Searcher.search(searcher, boolean_query, 10)
@spec search_documents(t(), TantivyEx.Query.t(), pos_integer()) :: {:ok, [search_result()]} | {:error, String.t()}
Performs a search and returns full documents with metadata.
This function always includes full document content and is optimized for cases where you need the complete document data.
Parameters
searcher: The Searcherquery: The search query (Query.t() only - no string queries)limit: Maximum number of results to return (default: 10)
Examples
iex> {:ok, query} = TantivyEx.Query.phrase(schema, "title", ["hello", "world"])
iex> {:ok, results} = TantivyEx.Searcher.search_documents(searcher, query, 10)
iex> [%{"score" => 1.0, "doc_id" => 1, "title" => "hello world", "body" => "..."}] = results
@spec search_ids(t(), String.t() | TantivyEx.Query.t(), pos_integer()) :: {:ok, [pos_integer()]} | {:error, String.t()}
Searches the index and returns only document IDs.
This is more efficient when you only need document IDs, not the full documents or scores.
Parameters
searcher: The Searcherquery: The search query (string or Query.t())limit: Maximum number of results to return (default: 10)
Examples
iex> {:ok, query} = TantivyEx.Query.term(schema, "title", "hello")
iex> {:ok, doc_ids} = TantivyEx.Searcher.search_ids(searcher, query, 10)
iex> [1, 5, 3] = doc_ids
@spec search_with_parser( t(), Schema.t(), [String.t()], String.t(), pos_integer(), boolean() ) :: {:ok, [search_result()]} | {:error, String.t()}
Performs a search with a query parser for Lucene-style queries.
This is a convenience function that creates a parser, parses the query string, and performs the search in one call.
Parameters
searcher: The Searcherschema: The schema for field resolutiondefault_fields: List of fields to search by defaultquery_str: The Lucene-style query stringlimit: Maximum number of results to return (default: 10)include_docs: Whether to include full document content (default: true)
Examples
iex> {:ok, results} = TantivyEx.Searcher.search_with_parser(
...> searcher, schema, ["title", "body"],
...> "title:hello AND body:world", 10
...> )