Dowser.Opensearch.Document (Dowser.Opensearch v0.1.0)

View Source

The OpenSearch document APIs — the endpoints tagged Document in the OpenSearch OpenAPI specification (single-document CRUD, bulk, multi-get, term vectors).

Built on Dowser.Client. Required OpenSearch attributes are positional arguments; everything optional lives in opts.

Note that the by-query operations live in Dowser.Opensearch.Reindex, not here: OpenSearch tags _delete_by_query, _update_by_query and _reindex as Reindex, where Elasticsearch groups them with the document APIs.

Shared conventions

  • 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 document or operations) otherwise.
  • :index — optional index target where the endpoint accepts one; endpoints that require an index take it as an argument.
  • HEAD existence checks come as a pair where the ? variant plays the bang role: exists/3 returns {:ok, boolean()} or {:error, exception}, exists?/3 returns the bare boolean (404 → false) and raises on genuine errors.

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).

Values are cast automatically wherever Dowser.Opensearch.Codec is configured as :decoder/:encoder — no per-call option needed. A response carries each document's own _index, so reads need telling nothing; the writing functions name the index each source is going to and where in the request body it sits, which is the mapping the encoder needs.

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

Performs several index/create/update/delete operations in one request (Bulk API).

Like bulk/2, but returns the body directly or raises the error exception — including the Dowser.Opensearch.BulkError a partial failure returns, so it raises unless every item was applied.

Creates the document id, failing if it already exists (Create document API).

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

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

Checks whether the document id exists (Document exists API).

Like exists/3, but returns the boolean directly (404 → false) or raises the error exception.

Retrieves the document id (Get document API).

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

Retrieves the source of the document id, without metadata (Get document source API).

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

Indexes (creates or replaces) a document (Index document API).

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

Retrieves several documents in one request (Multi-get API).

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

Returns term vectors for several documents in one request (Multi term vectors API).

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

Checks whether the document id exists and has a source (Source exists API).

Like source_exists/3, but returns the boolean directly (404 → false) or raises the error exception.

Returns term and field statistics for a stored or artificial document (Term vectors API).

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

Updates the document id with a script or a partial document (Update document API).

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

Types

body()

@type body() :: term()

exists_result()

@type exists_result() :: {:ok, boolean()} | {:error, Exception.t()}

id()

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

index()

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

result()

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

Functions

bulk(operations, opts \\ [])

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

Performs several index/create/update/delete operations in one request (Bulk API).

operations is a flat list alternating action and payload maps, encoded as NDJSON:

Dowser.Opensearch.Document.bulk([
  %{index: %{_id: "1"}},
  %{title: "hello"},
  %{delete: %{_id: "2"}}
])

Options

  • :index — default index target for actions that name none.

Per-item failures

A bulk request is not all-or-nothing. OpenSearch answers 200 OK with "errors" => true and an items entry per action, so a request whose documents were half rejected — a per-item 429 under load, a mapping failure — looks like a successful one at the HTTP level.

So {:ok, body} here means every item was applied. As soon as one failed, the result is {:error, %Dowser.Opensearch.BulkError{}}, which reports what failed, what succeeded, and which operations are worth resubmitting:

{:error, %BulkError{failed: failed, succeeded: 488, retryable: operations}} =
  Dowser.Opensearch.Document.bulk(operations, index: "posts")

Only the rejected items (429/503) are listed in :retryable; resubmitting those writes nothing twice, where resending the whole payload would index the successful items a second time.

bulk!(operations, opts \\ [])

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

Like bulk/2, but returns the body directly or raises the error exception — including the Dowser.Opensearch.BulkError a partial failure returns, so it raises unless every item was applied.

create(document, index, id, opts \\ [])

@spec create(map(), index(), id(), keyword()) :: result()

Creates the document id, failing if it already exists (Create document API).

document is the document body.

create!(document, index, id, opts \\ [])

@spec create!(map(), index(), id(), keyword()) :: body()

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

delete(index, id, opts \\ [])

@spec delete(index(), id(), keyword()) :: result()

Deletes the document id (Delete document API).

delete!(index, id, opts \\ [])

@spec delete!(index(), id(), keyword()) :: body()

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

exists(index, id, opts \\ [])

@spec exists(index(), id(), keyword()) :: exists_result()

Checks whether the document id exists (Document exists API).

Returns {:ok, true}, {:ok, false} or {:error, exception}.

exists?(index, id, opts \\ [])

@spec exists?(index(), id(), keyword()) :: boolean()

Like exists/3, but returns the boolean directly (404 → false) or raises the error exception.

get(index, id, opts \\ [])

@spec get(index(), id(), keyword()) :: result()

Retrieves the document id (Get document API).

get!(index, id, opts \\ [])

@spec get!(index(), id(), keyword()) :: body()

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

get_source(index, id, opts \\ [])

@spec get_source(index(), id(), keyword()) :: result()

Retrieves the source of the document id, without metadata (Get document source API).

get_source!(index, id, opts \\ [])

@spec get_source!(index(), id(), keyword()) :: body()

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

index(document, index, opts \\ [])

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

Indexes (creates or replaces) a document (Index document API).

document is the document body.

Options

  • :id — document id; absent to let OpenSearch generate one.

index!(document, index, opts \\ [])

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

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

mget(body, opts \\ [])

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

Retrieves several documents in one request (Multi-get API).

body is the request body, e.g. %{ids: ["1", "2"]} or %{docs: [...]}.

Options

  • :index — index target; absent when each doc names its own.

mget!(body, opts \\ [])

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

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

mtermvectors(body, opts \\ [])

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

Returns term vectors for several documents in one request (Multi term vectors API).

body is the request body, e.g. %{docs: [...]} or %{ids: [...]}.

Options

  • :index — index target; absent when each doc names its own.

mtermvectors!(body, opts \\ [])

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

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

source_exists(index, id, opts \\ [])

@spec source_exists(index(), id(), keyword()) :: exists_result()

Checks whether the document id exists and has a source (Source exists API).

Returns {:ok, true}, {:ok, false} or {:error, exception}.

source_exists?(index, id, opts \\ [])

@spec source_exists?(index(), id(), keyword()) :: boolean()

Like source_exists/3, but returns the boolean directly (404 → false) or raises the error exception.

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

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

Returns term and field statistics for a stored or artificial document (Term vectors API).

body is the request body (e.g. doc, fields, filter); pass %{} to send nothing.

Options

  • :id — stored document id; absent when analyzing a doc from the body.

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

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

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

update(body, index, id, opts \\ [])

@spec update(map(), index(), id(), keyword()) :: result()

Updates the document id with a script or a partial document (Update document API).

body is the update body, e.g. %{doc: %{title: "hi"}} or %{script: %{...}}.

update!(body, index, id, opts \\ [])

@spec update!(map(), index(), id(), keyword()) :: body()

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