Dowser.Opensearch.Search (Dowser.Opensearch v0.1.0)

View Source

The OpenSearch search APIs — the endpoints tagged Search in the OpenSearch OpenAPI specification.

Built on Dowser.Client. Required OpenSearch 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 OpenSearch query DSL document, and body (or a more specific name such as template or searches) otherwise.
  • When OpenSearch 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. :context, :params (query-string parameters), :format, :keys and :http_opts (including :headers) — plus :codec, this package's own, which picks the per-field codec for this one request (see Dowser.Opensearch.Codec).

Response values are cast automatically wherever Dowser.Opensearch.Codec is configured as :decoder — no per-call option needed. A query is never cast: build it in the shape OpenSearch expects.

On a 2xx response every function returns {:ok, body} with the decoded response body. A non-2xx response returns {:error, %Dowser.Opensearch.Error{}}; a transport, encoding or decoding failure returns {:error, exception} from Dowser.Client. A required argument that is missing or empty is reported the same way, before any request is made: {:error, %ArgumentError{}}. 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.

Counts the documents matching a query (Count API).

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

Creates a point in time over index, to search a fixed view of the data (Create PIT API).

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

Deletes every point in time (Delete all PITs API).

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

Deletes one or several points in time (Delete PIT API).

Like delete_pit/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.

Lists every point in time currently open (List all PITs API).

Like get_all_pits/1, 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.

Renders a search template into the actual search body it would run (Render search template API).

Like render_search_template/2, 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.

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.

Validates a query without running it (Validate query API).

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

Types

body()

@type body() :: term()

id()

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

index()

@type index() :: Dowser.Opensearch.Target.t()

pit_id()

@type pit_id() :: String.t() | [String.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.

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.

create_pit(index, keep_alive, opts \\ [])

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

Creates a point in time over index, to search a fixed view of the data (Create PIT API).

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. The endpoint takes no request body.

Note that this is not Elasticsearch's /_pit: OpenSearch serves the whole point-in-time API under /_search/point_in_time, and the id it returns is named pit_id rather than id.

{:ok, %{"pit_id" => pit_id}} =
  Dowser.Opensearch.Search.create_pit("posts", "5m")

Dowser.Opensearch.Search.search!(%{pit: %{id: pit_id}})

create_pit!(index, keep_alive, opts \\ [])

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

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

delete_all_pits(opts \\ [])

@spec delete_all_pits(keyword()) :: result()

Deletes every point in time (Delete all PITs API).

delete_all_pits!(opts \\ [])

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

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

delete_pit(pit_id, opts \\ [])

@spec delete_pit(pit_id(), keyword()) :: result()

Deletes one or several points in time (Delete PIT API).

pit_id is one id or a list of them; a single id is wrapped, since OpenSearch requires the body's pit_id to be an array.

delete_pit!(pit_id, opts \\ [])

@spec delete_pit!(pit_id(), keyword()) :: body()

Like delete_pit/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_all_pits(opts \\ [])

@spec get_all_pits(keyword()) :: result()

Lists every point in time currently open (List all PITs API).

get_all_pits!(opts \\ [])

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

Like get_all_pits/1, 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.Opensearch.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.

render_search_template(template, opts \\ [])

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

Renders a search template into the actual search body it would run (Render search template API).

template is the request body, typically %{params: %{...}} alongside an id or a source.

Options

  • :id — the stored template to render, appended to the path. Omit it for an inline source given in the body.

render_search_template!(template, opts \\ [])

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

Like render_search_template/2, 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 OpenSearch recommends over the deprecated path parameter.

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 OpenSearch query DSL as a map); pass %{} to match everything. It comes first so it can be piped:

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

Every key in the response is cast per :keys. Wherever Dowser.Opensearch.Codec is configured as :decoder, 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

  • :index — nil/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_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.

validate_query(query, opts \\ [])

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

Validates a query without running it (Validate query API).

query is the query body to check; pass %{} to validate nothing in particular. Use params: [explain: true] to get the reason a query is rejected rather than just valid: false.

Options

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

validate_query!(query, opts \\ [])

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

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