Snap.Search (Snap v0.16.0)

Copy Markdown View Source

Performs searches against an ElasticSearch cluster.

Summary

Functions

Releases a scroll cursor on the server, freeing the resources it holds.

Runs a count of the documents in an index, using an optional query.

Continues a scroll search, retrieving the next batch of hits.

Makes a search against an ElasticSearch cluster and parses the response.

Functions

clear_scroll(cluster, scroll_id, params \\ [], headers \\ [], opts \\ [])

@spec clear_scroll(
  cluster :: module(),
  scroll_id :: String.t(),
  params :: Keyword.t(),
  headers :: Keyword.t(),
  opts :: Keyword.t()
) :: {:ok, map()} | Snap.Cluster.error()

Releases a scroll cursor on the server, freeing the resources it holds.

Once a scroll is exhausted (or no longer needed), call this to clear the underlying search context. Scrolls also expire naturally based on the TTL passed to search/6 or scroll/6, but explicit cleanup is preferred.

count(cluster, index_or_alias, query \\ %{}, params \\ [], headers \\ [], opts \\ [])

Runs a count of the documents in an index, using an optional query.

delete_by_query(cluster, index_or_alias, query, params \\ [], headers \\ [], opts \\ [])

Runs a delete operation on an index, given a query.

scroll(cluster, scroll_id, scroll \\ "1m", params \\ [], headers \\ [], opts \\ [])

@spec scroll(
  cluster :: module(),
  scroll_id :: String.t(),
  scroll :: String.t(),
  params :: Keyword.t(),
  headers :: Keyword.t(),
  opts :: Keyword.t()
) :: {:ok, Snap.SearchResponse.t()} | Snap.Cluster.error()

Continues a scroll search, retrieving the next batch of hits.

Given a scroll_id returned from a previous search/6 (with a scroll parameter) or scroll/6 call, fetches the next batch of hits and returns a fresh Snap.SearchResponse with an updated scroll_id.

The scroll argument is the lifetime of the scroll cursor on the server, refreshed on each call. Defaults to "1m".

This endpoint is global on the cluster — index namespaces do not apply.

search(cluster, index_or_alias, query, params \\ [], headers \\ [], opts \\ [])

@spec search(
  cluster :: module(),
  index_or_alias :: String.t(),
  query :: map(),
  params :: Keyword.t(),
  headers :: Keyword.t(),
  opts :: Keyword.t()
) :: {:ok, Snap.SearchResponse.t()} | Snap.Cluster.error()

Makes a search against an ElasticSearch cluster and parses the response.

Performs a search against an index using a POST request, and parses the response into a Snap.SearchResponse.

Snap.SearchResponse implements Enumerable, so you can count and iterate directly on the struct.

Examples

query = %{query: %{match_all: %{}}}
{:ok, response} = Snap.Search.search(Cluster, "index", query)

IO.inspect(response.took)
Enum.each(response, fn hit -> IO.inspect(hit.score) end)