Dowser.Elasticsearch.Search (Dowser.Elasticsearch v0.1.0)

View Source

The Elasticsearch search APIs — every endpoint tagged search in the Elasticsearch OpenAPI specification.

Built on Dowser.Client. Required Elasticsearch attributes are positional arguments; everything optional lives in opts. Request bodies come first so they pipe naturally.

Shared conventions

  • :index — where the endpoint accepts an optional index target: nil/absent for all indices, a single index string, or a list of index strings (joined with ,). Endpoints that require an index take it as the first argument instead.
  • Endpoints that accept a request body take it as their first argument, required — pass %{} to send nothing. The argument is named query when the body is an Elasticsearch query DSL document, and body (or a more specific name such as template or searches) otherwise.
  • When Elasticsearch serves an operation over both GET and POST, the request uses POST whenever a body is present and GET otherwise.

All remaining options are forwarded to Dowser.Client.request/4, e.g. :config, :params (query-string parameters), :format, :http_adapter, :json_adapter and :http_opts (including :headers).

Values are cast automatically wherever :codec_adapter is set to Dowser.Elasticsearch.TypeCodec — no per-call option needed.

On a 2xx response every function returns {:ok, body} with the decoded response body. A non-2xx response returns {:error, %Dowser.Elasticsearch.Error{}}; a transport, encoding or decoding failure returns {:error, exception} from Dowser.Client. Each function has a bang variant that returns the body directly or raises the error exception.

Summary

Functions

Releases one or several scroll contexts (Clear scroll API).

Like clear_scroll/2, but returns the body directly or raises the error exception.

Like close_point_in_time/2, but returns the body directly or raises the error exception.

Counts the documents matching a query (Count API).

Like count/2, but returns the body directly or raises the error exception.

Cancels the async search id and deletes its results (Delete async search API).

Like delete_async_search/2, but returns the body directly or raises the error exception.

Explains whether and how the document id in index matches a query (Explain API).

Like explain/4, but returns the body directly or raises the error exception.

Returns the capabilities of fields across indices (Field capabilities API).

Like field_caps/2, but returns the body directly or raises the error exception.

Retrieves the results of the async search id (Get async search API).

Like get_async_search/2, but returns the body directly or raises the error exception.

Retrieves the status of the async search id, without its results (Get async search status API).

Like get_async_search_status/2, but returns the body directly or raises the error exception.

Runs several searches in one request (Multi search API).

Like msearch/2, but returns the body directly or raises the error exception.

Runs several template searches in one request (Multi search template API).

Like msearch_template/2, but returns the body directly or raises the error exception.

Opens a point in time over index for use in later searches (Open point in time API).

Like open_point_in_time/4, but returns the body directly or raises the error exception.

Evaluates the quality of ranked search results over a set of typical queries (Ranking evaluation API).

Like rank_eval/2, but returns the body directly or raises the error exception.

Renders the stored search template id into an actual search body (Render search template API).

Like render_search_template/3, but returns the body directly or raises the error exception.

Fetches the next page of a scrolling search (Scroll API).

Like scroll/2, but returns the body directly or raises the error exception.

Runs a search against one, several, or all indices (Search API).

Like search/2, but returns the body directly or raises the error exception.

Searches a vector tile for geospatial values (Vector tile search API).

Like search_mvt/7, but returns the body directly or raises the error exception.

Returns the indices and shards a search would run against (Search shards API).

Like search_shards/1, but returns the body directly or raises the error exception.

Runs a search with a stored or inline search template (Search template API).

Like search_template/2, but returns the body directly or raises the error exception.

Submits a search that runs asynchronously (Submit async search API).

Like submit_async_search/2, but returns the body directly or raises the error exception.

Enumerates the terms of a field that match a partial string (Terms enum API).

Like terms_enum/3, but returns the body directly or raises the error exception.

Types

body()

@type body() :: term()

id()

@type id() :: String.t()

index()

@type index() :: Dowser.Elasticsearch.Index.t()

query()

@type query() :: map()

result()

@type result() :: {:ok, body()} | {:error, Exception.t()}

scroll_id()

@type scroll_id() :: String.t() | [String.t()]

Functions

clear_scroll(scroll_id, opts \\ [])

@spec clear_scroll(
  scroll_id(),
  keyword()
) :: result()

Releases one or several scroll contexts (Clear scroll API).

scroll_id is a scroll id, a list of scroll ids, or "_all".

clear_scroll!(scroll_id, opts \\ [])

@spec clear_scroll!(
  scroll_id(),
  keyword()
) :: body()

Like clear_scroll/2, but returns the body directly or raises the error exception.

close_point_in_time(id, opts \\ [])

@spec close_point_in_time(
  id(),
  keyword()
) :: result()

Closes the point in time id (Close point in time API).

close_point_in_time!(id, opts \\ [])

@spec close_point_in_time!(
  id(),
  keyword()
) :: body()

Like close_point_in_time/2, but returns the body directly or raises the error exception.

count(query, opts \\ [])

@spec count(
  query(),
  keyword()
) :: result()

Counts the documents matching a query (Count API).

query is the count body (query DSL map); pass %{} to count everything.

Options

  • :index — index target; absent for all indices.

count!(query, opts \\ [])

@spec count!(
  query(),
  keyword()
) :: body()

Like count/2, but returns the body directly or raises the error exception.

delete_async_search(id, opts \\ [])

@spec delete_async_search(
  id(),
  keyword()
) :: result()

Cancels the async search id and deletes its results (Delete async search API).

delete_async_search!(id, opts \\ [])

@spec delete_async_search!(
  id(),
  keyword()
) :: body()

Like delete_async_search/2, but returns the body directly or raises the error exception.

explain(query, index, id, opts \\ [])

@spec explain(query(), index(), id(), keyword()) :: result()

Explains whether and how the document id in index matches a query (Explain API).

query is the explain body (query DSL map).

explain!(query, index, id, opts \\ [])

@spec explain!(query(), index(), id(), keyword()) :: body()

Like explain/4, but returns the body directly or raises the error exception.

field_caps(body, opts \\ [])

@spec field_caps(
  map(),
  keyword()
) :: result()

Returns the capabilities of fields across indices (Field capabilities API).

body is the request body, e.g. %{fields: ["title"], index_filter: %{...}}.

Options

  • :index — index target; absent for all indices.

field_caps!(body, opts \\ [])

@spec field_caps!(
  map(),
  keyword()
) :: body()

Like field_caps/2, but returns the body directly or raises the error exception.

get_async_search(id, opts \\ [])

@spec get_async_search(
  id(),
  keyword()
) :: result()

Retrieves the results of the async search id (Get async search API).

get_async_search!(id, opts \\ [])

@spec get_async_search!(
  id(),
  keyword()
) :: body()

Like get_async_search/2, but returns the body directly or raises the error exception.

get_async_search_status(id, opts \\ [])

@spec get_async_search_status(
  id(),
  keyword()
) :: result()

Retrieves the status of the async search id, without its results (Get async search status API).

get_async_search_status!(id, opts \\ [])

@spec get_async_search_status!(
  id(),
  keyword()
) :: body()

Like get_async_search_status/2, but returns the body directly or raises the error exception.

msearch(searches, opts \\ [])

@spec msearch(
  [map()],
  keyword()
) :: result()

Runs several searches in one request (Multi search API).

searches is a flat list alternating header and body maps, encoded as NDJSON:

Dowser.Elasticsearch.Search.msearch([
  %{},
  %{query: %{match_all: %{}}},
  %{index: "comments"},
  %{query: %{match: %{body: "hello"}}}
])

Options

  • :index — default index target for searches whose header has none.

msearch!(searches, opts \\ [])

@spec msearch!(
  [map()],
  keyword()
) :: body()

Like msearch/2, but returns the body directly or raises the error exception.

msearch_template(searches, opts \\ [])

@spec msearch_template(
  [map()],
  keyword()
) :: result()

Runs several template searches in one request (Multi search template API).

searches is a flat list alternating header and template-body maps, encoded as NDJSON (see msearch/2).

Options

  • :index — default index target for searches whose header has none.

msearch_template!(searches, opts \\ [])

@spec msearch_template!(
  [map()],
  keyword()
) :: body()

Like msearch_template/2, but returns the body directly or raises the error exception.

open_point_in_time(body, index, keep_alive, opts \\ [])

@spec open_point_in_time(map(), index(), String.t(), keyword()) :: result()

Opens a point in time over index for use in later searches (Open point in time API).

body is the request body (e.g. index_filter); pass %{} to send nothing. keep_alive is how long the point in time is kept alive, e.g. "1m"; it is sent as the required keep_alive query-string parameter.

open_point_in_time!(body, index, keep_alive, opts \\ [])

@spec open_point_in_time!(map(), index(), String.t(), keyword()) :: body()

Like open_point_in_time/4, but returns the body directly or raises the error exception.

rank_eval(requests, opts \\ [])

@spec rank_eval(
  [map()],
  keyword()
) :: result()

Evaluates the quality of ranked search results over a set of typical queries (Ranking evaluation API).

requests is the list of rated requests.

Options

  • :index — index target; absent for all indices.
  • :metric — the evaluation metric, merged into the body alongside requests.

rank_eval!(requests, opts \\ [])

@spec rank_eval!(
  [map()],
  keyword()
) :: body()

Like rank_eval/2, but returns the body directly or raises the error exception.

render_search_template(template, id, opts \\ [])

@spec render_search_template(map(), id(), keyword()) :: result()

Renders the stored search template id into an actual search body (Render search template API).

template is the request body, typically %{params: %{...}}.

render_search_template!(template, id, opts \\ [])

@spec render_search_template!(map(), id(), keyword()) :: body()

Like render_search_template/3, but returns the body directly or raises the error exception.

scroll(scroll_id, opts \\ [])

@spec scroll(
  id(),
  keyword()
) :: result()

Fetches the next page of a scrolling search (Scroll API).

Sends scroll_id in the request body, the form Elasticsearch recommends.

Options

  • :scroll — how long to keep the scroll context alive, e.g. "1m"; merged into the body.

scroll!(scroll_id, opts \\ [])

@spec scroll!(
  id(),
  keyword()
) :: body()

Like scroll/2, but returns the body directly or raises the error exception.

search(query, opts \\ [])

@spec search(
  query(),
  keyword()
) :: result()

Runs a search against one, several, or all indices (Search API).

query is the search body (the Elasticsearch query DSL as a map); pass %{} to match everything. It comes first so it can be piped:

%{query: %{match: %{title: "hello"}}}
|> Dowser.Elasticsearch.Search.search(index: "posts")

Every key in the response is cast per :keys. Wherever :type_codec is configured (see Dowser.Elasticsearch.TypeCodec), each hit's _source is additionally cast against its own index mapping (dates become DateTime, IPs become :inet tuples, and so on) — automatically, at any nesting depth, so msearch/2, search_template/2, scroll/2 and the rest get the same treatment with no extra options.

Options

  • :indexnil/absent for all indices, a single index string, or a list of index strings (joined with ,).

search!(query, opts \\ [])

@spec search!(
  query(),
  keyword()
) :: body()

Like search/2, but returns the body directly or raises the error exception.

search_mvt(body, index, field, zoom, x, y, opts \\ [])

@spec search_mvt(
  map(),
  index(),
  String.t(),
  integer(),
  integer(),
  integer(),
  keyword()
) :: result()

Searches a vector tile for geospatial values (Vector tile search API).

body is the request body (e.g. query, fields, grid_agg); pass %{} to send nothing. Returns the binary Mapbox vector tile, so the response format defaults to :raw (override with :resp_format/:format).

search_mvt!(body, index, field, zoom, x, y, opts \\ [])

@spec search_mvt!(
  map(),
  index(),
  String.t(),
  integer(),
  integer(),
  integer(),
  keyword()
) :: body()

Like search_mvt/7, but returns the body directly or raises the error exception.

search_shards(opts \\ [])

@spec search_shards(keyword()) :: result()

Returns the indices and shards a search would run against (Search shards API).

Options

  • :index — index target; absent for all indices.

search_shards!(opts \\ [])

@spec search_shards!(keyword()) :: body()

Like search_shards/1, but returns the body directly or raises the error exception.

search_template(template, opts \\ [])

@spec search_template(
  map(),
  keyword()
) :: result()

Runs a search with a stored or inline search template (Search template API).

template is the request body, e.g. %{id: "my-template", params: %{...}} or %{source: %{...}, params: %{...}}.

Options

  • :index — index target; absent for all indices.

search_template!(template, opts \\ [])

@spec search_template!(
  map(),
  keyword()
) :: body()

Like search_template/2, but returns the body directly or raises the error exception.

submit_async_search(query, opts \\ [])

@spec submit_async_search(
  query(),
  keyword()
) :: result()

Submits a search that runs asynchronously (Submit async search API).

query is the search body, exactly as in search/2.

Options

  • :index — index target; absent for all indices.

submit_async_search!(query, opts \\ [])

@spec submit_async_search!(
  query(),
  keyword()
) :: body()

Like submit_async_search/2, but returns the body directly or raises the error exception.

terms_enum(body, index, opts \\ [])

@spec terms_enum(map(), index(), keyword()) :: result()

Enumerates the terms of a field that match a partial string (Terms enum API).

body is the request body and must include field, e.g. %{field: "title", string: "he"}.

terms_enum!(body, index, opts \\ [])

@spec terms_enum!(map(), index(), keyword()) :: body()

Like terms_enum/3, but returns the body directly or raises the error exception.