View Source ElasticsearchEx.Api.Document.Single (Elasticsearch_ex v0.5.0)

Provides the APIs for the single document operations.

Summary

Functions

Adds a JSON document to the specified data stream or index and makes it searchable.

Removes a JSON document from the specified index.

Checks if the specified JSON document from an index exists.

Retrieves the specified JSON document from an index.

Retrieves the specified JSON source from an index.

Adds a JSON document to the specified data stream or index and makes it searchable. If the target is an index and the document already exists, the request updates the document and increments its version.

Checks if the specified JSON source from an index exists.

Updates a document using the specified script.

Functions

Link to this function

create(document, opts \\ [])

View Source
@spec create(
  map(),
  keyword()
) :: {:ok, term()} | {:error, ElasticsearchEx.Error.t()}

Adds a JSON document to the specified data stream or index and makes it searchable.

Query parameters

Refer to the official documentation for a detailed list of the parameters.

Request body

Refer to the official documentation for a detailed list of the body values.

Examples

iex> ElasticsearchEx.Api.Document.Single.create(
...>   %{
...>     "@timestamp": "2099-11-15T13:12:00",
...>     message: "GET /search HTTP/1.1 200 1070000",
...>     user: %{id: "kimchy"}
...>   },
...>   index: "my-index-000001",
...>   id: "W0tpsmIBdwcYyG50zbta"
...> )
{:ok,
 %{
   "_id" => "W0tpsmIBdwcYyG50zbta",
   "_index" => "my-index-000001",
   "_primary_term" => 1,
   "_seq_no" => 0,
   "_shards" => %{"failed" => 0, "successful" => 2, "total" => 2},
   "_version" => 1,
   "result" => "created"
 }}
@spec delete(keyword()) :: {:ok, term()} | {:error, ElasticsearchEx.Error.t()}

Removes a JSON document from the specified index.

Query parameters

Refer to the official documentation for a detailed list of the parameters.

Examples

iex> ElasticsearchEx.Api.Document.Single.delete(index: "my-index-000001", id: "0")
{:ok,
 %{
   "_id" => "0",
   "_index" => "my-index-000001",
   "_primary_term" => 3,
   "_seq_no" => 6,
   "_shards" => %{"failed" => 0, "successful" => 1, "total" => 2},
   "_version" => 2,
   "result" => "deleted"
 }}

iex> ElasticsearchEx.Api.Document.Single.delete(index: "my-index-000001", id: "1")
{:error,
 %ElasticsearchEx.Error{
   reason: "Document with ID: `1` not found",
   root_cause: nil,
   status: 404,
   type: "not_found",
   ...
 }}
Link to this function

document_exists?(opts \\ [])

View Source
@spec document_exists?(keyword()) :: boolean()

Checks if the specified JSON document from an index exists.

Query parameters

Refer to the official documentation for a detailed list of the parameters.

Examples

iex> ElasticsearchEx.Api.Document.Single.document_exists?(index: "my-index-000001", id: "0")
true
Link to this function

get_document(opts \\ [])

View Source
@spec get_document(keyword()) :: {:ok, term()} | {:error, ElasticsearchEx.Error.t()}

Retrieves the specified JSON document from an index.

Query parameters

Refer to the official documentation for a detailed list of the parameters.

Examples

iex> ElasticsearchEx.Api.Document.Single.get_document(index: "my-index-000001", id: "0")
{:ok,
 %{
   "_id" => "0",
   "_index" => "my-index-000001",
   "_primary_term" => 1,
   "_seq_no" => 0,
   "_source" => %{
     "@timestamp" => "2099-11-15T14:12:12",
     "http" => %{
       "request" => %{"method" => "get"},
       "response" => %{"bytes" => 1070000, "status_code" => 200},
       "version" => "1.1"
     },
     "message" => "GET /search HTTP/1.1 200 1070000",
     "source" => %{"ip" => "127.0.0.1"},
     "user" => %{"id" => "kimchy"}
   },
   "_version" => 1,
   "found" => true
 }}
@spec get_source(keyword()) :: {:ok, term()} | {:error, ElasticsearchEx.Error.t()}

Retrieves the specified JSON source from an index.

Query parameters

Refer to the official documentation for a detailed list of the parameters.

Examples

iex> ElasticsearchEx.Api.Document.Single.get_source(index: "my-index-000001", id: "0")
{:ok,
 %{
   "@timestamp" => "2099-11-15T14:12:12",
   "http" => %{
     "request" => %{"method" => "get"},
     "response" => %{"bytes" => 1_070_000, "status_code" => 200},
     "version" => "1.1"
   },
   "message" => "GET /search HTTP/1.1 200 1070000",
   "source" => %{"ip" => "127.0.0.1"},
   "user" => %{"id" => "kimchy"}
 }}
Link to this function

index(document, opts \\ [])

View Source
@spec index(
  map(),
  keyword()
) :: {:ok, term()} | {:error, ElasticsearchEx.Error.t()}

Adds a JSON document to the specified data stream or index and makes it searchable. If the target is an index and the document already exists, the request updates the document and increments its version.

Query parameters

Refer to the official documentation for a detailed list of the parameters.

Request body

Refer to the official documentation for a detailed list of the body values.

Examples

Without a specific document ID:

iex> ElasticsearchEx.Api.Document.Single.index(
...>   %{
...>     "@timestamp": "2099-11-15T13:12:00",
...>     message: "GET /search HTTP/1.1 200 1070000",
...>     user: %{id: "kimchy"}
...>   },
...>   index: "my-index-000001"
...> )
{:ok,
 %{
   "_id" => "W0tpsmIBdwcYyG50zbta",
   "_index" => "my-index-000001",
   "_primary_term" => 1,
   "_seq_no" => 0,
   "_shards" => %{"failed" => 0, "successful" => 2, "total" => 2},
   "_version" => 1,
   "result" => "created"
 }}

With a specific document ID:

iex> ElasticsearchEx.Api.Document.Single.index(
...>   %{
...>     "@timestamp": "2099-11-15T13:12:00",
...>     message: "GET /search HTTP/1.1 200 1070000",
...>     user: %{id: "kimchy"}
...>   },
...>   index: "my-index-000001",
...>   id: "W0tpsmIBdwcYyG50zbta"
...> )
{:ok,
 %{
   "_id" => "W0tpsmIBdwcYyG50zbta",
   "_index" => "my-index-000001",
   "_primary_term" => 1,
   "_seq_no" => 0,
   "_shards" => %{"failed" => 0, "successful" => 2, "total" => 2},
   "_version" => 1,
   "result" => "created"
 }}
Link to this function

source_exists?(opts \\ [])

View Source
@spec source_exists?(keyword()) :: boolean()

Checks if the specified JSON source from an index exists.

Query parameters

Refer to the official documentation for a detailed list of the parameters.

Examples

iex> ElasticsearchEx.Api.Document.Single.source_exists?(index: "my-index-000001", id: "0")
true
Link to this function

update(document, opts \\ [])

View Source
@spec update(
  map(),
  keyword()
) :: {:ok, term()} | {:error, ElasticsearchEx.Error.t()}

Updates a document using the specified script.

Query parameters

Refer to the official documentation for a detailed list of the parameters.

Request body

Refer to the official documentation for a detailed list of the body values.