TantivyEx.Facet (TantivyEx v0.4.1)
View SourceFaceted search functionality for TantivyEx.
Facets are hierarchical categories that allow for sophisticated navigation and filtering of search results. This module provides comprehensive faceted search capabilities including facet collection, drilling down, and hierarchical navigation.
Examples
# Basic facet collection
{:ok, facet_collector} = TantivyEx.Facet.collector_for_field("category")
:ok = TantivyEx.Facet.add_facet(facet_collector, "/electronics")
{:ok, facet_counts} = TantivyEx.Facet.search(searcher, query, facet_collector)
# Get top facets
top_facets = TantivyEx.Facet.get_top_k(facet_counts, "/electronics", 10)
# Hierarchical facet navigation
child_facets = TantivyEx.Facet.get_children(facet_counts, "/electronics/computers")
# Facet filtering with boolean queries
facet_query = TantivyEx.Facet.facet_term_query("category", "/electronics/laptops")
{:ok, filtered_results} = TantivyEx.Searcher.search(searcher, facet_query, 100, true)Facet Structure
Facets are hierarchical paths separated by forward slashes:
/electronics- Top level category/electronics/computers- Subcategory/electronics/computers/laptops- Sub-subcategory
Each level can be searched and counted independently, allowing for drill-down navigation.
Summary
Functions
Adds a facet path to the collector for counting.
Creates a new facet collector for the specified field.
Creates a term query for filtering by a specific facet.
Creates a facet from a text path.
Gets all child facets for a given parent facet path.
Gets the total count for a specific facet path.
Gets the top K facets for a given facet path.
Creates a multi-facet boolean query for filtering by multiple facets.
Performs a search with facet collection.
Gets the string representation of a facet.
Functions
Adds a facet path to the collector for counting.
Parameters
collector_ref- Reference to the facet collectorfacet_path- The hierarchical facet path (e.g., "/electronics/computers")
Returns
:okon success{:error, reason}on failure
Example
:ok = TantivyEx.Facet.add_facet(collector, "/electronics")
:ok = TantivyEx.Facet.add_facet(collector, "/electronics/computers")
Creates a new facet collector for the specified field.
Parameters
field_name- The name of the facet field to collect on
Returns
{:ok, collector_ref}on success{:error, reason}on failure
Example
{:ok, collector} = TantivyEx.Facet.collector_for_field("category")
@spec facet_term_query(reference(), String.t(), String.t()) :: {:ok, reference()} | {:error, String.t()}
Creates a term query for filtering by a specific facet.
Parameters
schema- The index schema containing the facet field definitionfield_name- The facet field namefacet_path- The facet path to filter by
Returns
{:ok, query_ref}on success{:error, reason}on failure
Example
{:ok, facet_query} = TantivyEx.Facet.facet_term_query(schema, "category", "/electronics/laptops")
{:ok, results} = TantivyEx.Searcher.search(searcher, facet_query, 100, true)
Creates a facet from a text path.
Parameters
facet_path- The facet path string
Returns
{:ok, facet_ref}on success{:error, reason}on failure
Example
{:ok, facet} = TantivyEx.Facet.from_text("/electronics/laptops")
@spec get_children(map(), String.t()) :: [{String.t(), non_neg_integer()}]
Gets all child facets for a given parent facet path.
Parameters
facet_counts- The facet counts result from search/3parent_path- The parent facet path
Returns
- List of tuples
{facet_path, count}for all children
Example
children = TantivyEx.Facet.get_children(facet_counts, "/electronics")
# Returns: [{"/electronics/computers", 150}, {"/electronics/phones", 89}, ...]
@spec get_count(map(), String.t()) :: non_neg_integer()
Gets the total count for a specific facet path.
Parameters
facet_counts- The facet counts result from search/3facet_path- The facet path to get count for
Returns
- The count as integer, or 0 if not found
Example
count = TantivyEx.Facet.get_count(facet_counts, "/electronics/laptops")
@spec get_top_k(map(), String.t(), non_neg_integer()) :: [ {String.t(), non_neg_integer()} ]
Gets the top K facets for a given facet path.
Parameters
facet_counts- The facet counts result from search/3facet_path- The parent facet path to get children fork- Number of top facets to return
Returns
- List of tuples
{facet_path, count}sorted by count descending
Example
top_categories = TantivyEx.Facet.get_top_k(facet_counts, "/electronics", 5)
# Returns: [{"/electronics/computers", 150}, {"/electronics/phones", 89}, ...]
@spec multi_facet_query(String.t(), [String.t()], atom()) :: {:ok, reference()} | {:error, String.t()}
Creates a multi-facet boolean query for filtering by multiple facets.
Parameters
field_name- The facet field namefacet_paths- List of facet paths to filter byoccur- How to combine the facets (:should, :must, :must_not)
Returns
{:ok, query_ref}on success{:error, reason}on failure
Example
facets = ["/electronics/laptops", "/electronics/tablets"]
{:ok, multi_query} = TantivyEx.Facet.multi_facet_query("category", facets, :should)
Performs a search with facet collection.
Parameters
searcher_ref- Reference to the searcherquery_ref- Reference to the querycollector_ref- Reference to the facet collector
Returns
{:ok, facet_counts}on success where facet_counts is a map{:error, reason}on failure
Example
{:ok, facet_counts} = TantivyEx.Facet.search(searcher, query, collector)
Gets the string representation of a facet.
Parameters
facet_ref- Reference to the facet
Returns
{:ok, facet_string}on success{:error, reason}on failure
Example
{:ok, path} = TantivyEx.Facet.to_string(facet_ref)