Current section
Files
Jump to
Current section
Files
src/dream_opensearch@document.erl
-module(dream_opensearch@document).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/dream_opensearch/document.gleam").
-export([index/4, search/3, delete/3, create_index/3, delete_index/2, bulk/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" OpenSearch document operations\n"
"\n"
" This module provides high-level functions for common OpenSearch operations:\n"
" indexing documents, searching, deleting, and managing indices.\n"
"\n"
" ## Quick Start\n"
"\n"
" ```gleam\n"
" import dream_opensearch/client\n"
" import dream_opensearch/document\n"
" import gleam/json\n"
"\n"
" let client = client.new(\"http://localhost:9200\")\n"
"\n"
" // Index a document\n"
" let doc = json.object([\n"
" #(\"name\", json.string(\"Alice\")),\n"
" #(\"email\", json.string(\"alice@example.com\")),\n"
" ])\n"
" document.index(client, \"users\", \"123\", json.to_string(doc))\n"
" ```\n"
"\n"
" ## JSON Format\n"
"\n"
" All document operations expect JSON strings. Use `gleam/json` to build\n"
" JSON objects and convert them to strings with `json.to_string()`.\n"
).
-file("src/dream_opensearch/document.gleam", 64).
?DOC(
" Index a document in OpenSearch\n"
"\n"
" Creates or updates a document in the specified index. If a document with\n"
" the same ID already exists, it will be replaced.\n"
"\n"
" ## Parameters\n"
"\n"
" - `client`: The OpenSearch client\n"
" - `index_name`: The name of the index (e.g., \"users\", \"products\")\n"
" - `document_id`: The unique document ID\n"
" - `document_json`: The document as a JSON string\n"
"\n"
" ## Returns\n"
"\n"
" - `Ok(String)`: The OpenSearch response as JSON string\n"
" - `Error(String)`: An error message if the operation failed\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_opensearch/document\n"
" import gleam/json\n"
"\n"
" let doc = json.object([\n"
" #(\"name\", json.string(\"Alice\")),\n"
" #(\"email\", json.string(\"alice@example.com\")),\n"
" ])\n"
"\n"
" case document.index(client, \"users\", \"123\", json.to_string(doc)) {\n"
" Ok(response) -> io.println(\"Indexed successfully\")\n"
" Error(msg) -> io.println(\"Error: \" <> msg)\n"
" }\n"
" ```\n"
).
-spec index(dream_opensearch@client:client(), binary(), binary(), binary()) -> {ok,
binary()} |
{error, binary()}.
index(Client, Index_name, Document_id, Document_json) ->
Path = <<<<<<"/"/utf8, Index_name/binary>>/binary, "/_doc/"/utf8>>/binary,
Document_id/binary>>,
dream_opensearch@client:send_request(Client, put, Path, Document_json).
-file("src/dream_opensearch/document.gleam", 103).
?DOC(
" Search documents in an index\n"
"\n"
" Performs a search query against the specified index. The query should be\n"
" a valid OpenSearch query JSON string. Use `dream_opensearch/query` helpers\n"
" to build queries, or construct the JSON manually.\n"
"\n"
" ## Parameters\n"
"\n"
" - `client`: The OpenSearch client\n"
" - `index_name`: The name of the index to search\n"
" - `query_json`: The search query as a JSON string\n"
"\n"
" ## Returns\n"
"\n"
" - `Ok(String)`: The search results as JSON string\n"
" - `Error(String)`: An error message if the search failed\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_opensearch/document\n"
" import dream_opensearch/query\n"
"\n"
" let query = query.match(\"name\", \"Alice\")\n"
" case document.search(client, \"users\", query) {\n"
" Ok(results) -> process_results(results)\n"
" Error(msg) -> handle_error(msg)\n"
" }\n"
" ```\n"
).
-spec search(dream_opensearch@client:client(), binary(), binary()) -> {ok,
binary()} |
{error, binary()}.
search(Client, Index_name, Query_json) ->
Path = <<<<"/"/utf8, Index_name/binary>>/binary, "/_search"/utf8>>,
dream_opensearch@client:send_request(Client, post, Path, Query_json).
-file("src/dream_opensearch/document.gleam", 137).
?DOC(
" Delete a document from an index\n"
"\n"
" Removes a document from the specified index by its ID.\n"
"\n"
" ## Parameters\n"
"\n"
" - `client`: The OpenSearch client\n"
" - `index_name`: The name of the index\n"
" - `document_id`: The ID of the document to delete\n"
"\n"
" ## Returns\n"
"\n"
" - `Ok(String)`: The OpenSearch response as JSON string\n"
" - `Error(String)`: An error message if the deletion failed\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_opensearch/document\n"
"\n"
" case document.delete(client, \"users\", \"123\") {\n"
" Ok(_) -> io.println(\"Document deleted\")\n"
" Error(msg) -> io.println(\"Error: \" <> msg)\n"
" }\n"
" ```\n"
).
-spec delete(dream_opensearch@client:client(), binary(), binary()) -> {ok,
binary()} |
{error, binary()}.
delete(Client, Index_name, Document_id) ->
Path = <<<<<<"/"/utf8, Index_name/binary>>/binary, "/_doc/"/utf8>>/binary,
Document_id/binary>>,
dream_opensearch@client:send_request(Client, delete, Path, <<""/utf8>>).
-file("src/dream_opensearch/document.gleam", 179).
?DOC(
" Create an index with mapping\n"
"\n"
" Creates a new index with the specified mapping (schema). The mapping\n"
" defines the fields and their types for documents in the index.\n"
"\n"
" ## Parameters\n"
"\n"
" - `client`: The OpenSearch client\n"
" - `index_name`: The name of the index to create\n"
" - `mapping_json`: The index mapping as a JSON string\n"
"\n"
" ## Returns\n"
"\n"
" - `Ok(String)`: The OpenSearch response as JSON string\n"
" - `Error(String)`: An error message if creation failed (e.g., index already exists)\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_opensearch/document\n"
" import gleam/json\n"
"\n"
" let mapping = json.object([\n"
" #(\"mappings\", json.object([\n"
" #(\"properties\", json.object([\n"
" #(\"name\", json.object([#(\"type\", json.string(\"text\"))])),\n"
" #(\"email\", json.object([#(\"type\", json.string(\"keyword\"))])),\n"
" ])),\n"
" ])),\n"
" ])\n"
"\n"
" document.create_index(client, \"users\", json.to_string(mapping))\n"
" ```\n"
).
-spec create_index(dream_opensearch@client:client(), binary(), binary()) -> {ok,
binary()} |
{error, binary()}.
create_index(Client, Index_name, Mapping_json) ->
Path = <<"/"/utf8, Index_name/binary>>,
dream_opensearch@client:send_request(Client, put, Path, Mapping_json).
-file("src/dream_opensearch/document.gleam", 213).
?DOC(
" Delete an index\n"
"\n"
" Permanently deletes an index and all its documents. This operation cannot\n"
" be undone.\n"
"\n"
" ## Parameters\n"
"\n"
" - `client`: The OpenSearch client\n"
" - `index_name`: The name of the index to delete\n"
"\n"
" ## Returns\n"
"\n"
" - `Ok(String)`: The OpenSearch response as JSON string\n"
" - `Error(String)`: An error message if deletion failed (e.g., index doesn't exist)\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_opensearch/document\n"
"\n"
" case document.delete_index(client, \"old_index\") {\n"
" Ok(_) -> io.println(\"Index deleted\")\n"
" Error(msg) -> io.println(\"Error: \" <> msg)\n"
" }\n"
" ```\n"
).
-spec delete_index(dream_opensearch@client:client(), binary()) -> {ok, binary()} |
{error, binary()}.
delete_index(Client, Index_name) ->
Path = <<"/"/utf8, Index_name/binary>>,
dream_opensearch@client:send_request(Client, delete, Path, <<""/utf8>>).
-file("src/dream_opensearch/document.gleam", 250).
?DOC(
" Bulk index documents\n"
"\n"
" Indexes multiple documents in a single request using OpenSearch's bulk API.\n"
" The body must be in NDJSON (newline-delimited JSON) format, where each line\n"
" is a JSON object.\n"
"\n"
" ## Parameters\n"
"\n"
" - `client`: The OpenSearch client\n"
" - `ndjson`: The bulk request body in NDJSON format\n"
"\n"
" ## Returns\n"
"\n"
" - `Ok(String)`: The OpenSearch response as JSON string\n"
" - `Error(String)`: An error message if the bulk operation failed\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_opensearch/document\n"
"\n"
" // NDJSON format: action and document on alternating lines\n"
" let ndjson = \"{ \\\"index\\\": { \\\"_index\\\": \\\"users\\\", \\\"_id\\\": \\\"1\\\" } }\\n\"\n"
" <> \"{ \\\"name\\\": \\\"Alice\\\", \\\"email\\\": \\\"alice@example.com\\\" }\\n\"\n"
" <> \"{ \\\"index\\\": { \\\"_index\\\": \\\"users\\\", \\\"_id\\\": \\\"2\\\" } }\\n\"\n"
" <> \"{ \\\"name\\\": \\\"Bob\\\", \\\"email\\\": \\\"bob@example.com\\\" }\\n\"\n"
"\n"
" document.bulk(client, ndjson)\n"
" ```\n"
).
-spec bulk(dream_opensearch@client:client(), binary()) -> {ok, binary()} |
{error, binary()}.
bulk(Client, Ndjson) ->
Path = <<"/_bulk"/utf8>>,
dream_opensearch@client:send_request(Client, post, Path, Ndjson).