AshScylla.Search.Query.BooleanEngine (AshScylla v1.6.1)

Copy Markdown View Source

Boolean operations on posting lists.

Implements efficient set operations using a two-pointer merge algorithm with O(n + m) complexity — identical to how Lucene performs intersections and unions.

Supports:

  • AND — intersection of posting lists
  • OR — union of posting lists
  • NOT — difference of posting lists

Summary

Functions

Computes the AND intersection of multiple posting lists.

Computes the difference: posts in include that are NOT in exclude.

Computes the OR union of multiple posting lists.

Types

posting_list()

@type posting_list() :: [{String.t(), non_neg_integer(), non_neg_integer()}]

scored_post()

@type scored_post() :: {String.t(), float()}

Functions

and_intersect(lists)

@spec and_intersect([posting_list()]) :: [{String.t(), non_neg_integer()}]

Computes the AND intersection of multiple posting lists.

Uses a two-pointer merge algorithm for each pair of lists.

Example

iex> BooleanEngine.and_intersect([
...>   [{"a", 1, 0}, {"b", 1, 1}, {"c", 1, 2}],
...>   [{"b", 1, 0}, {"c", 1, 1}, {"d", 1, 2}]
...> ])
[{"b", 1}, {"c", 1}]

not_difference(include, exclude)

@spec not_difference(posting_list(), posting_list()) :: [
  {String.t(), non_neg_integer()}
]

Computes the difference: posts in include that are NOT in exclude.

Example

iex> BooleanEngine.not_difference(
...>   [{"a", 1, 0}, {"b", 1, 1}, {"c", 1, 2}],
...>   [{"b", 1, 0}]
...> )
[{"a", 1}, {"c", 1}]

or_union(lists)

@spec or_union([posting_list()]) :: [{String.t(), non_neg_integer()}]

Computes the OR union of multiple posting lists.

Example

iex> BooleanEngine.or_union([
...>   [{"a", 1, 0}, {"b", 1, 1}],
...>   [{"b", 1, 0}, {"c", 1, 1}]
...> ])
[{"a", 1}, {"b", 1}, {"c", 1}]