dream_opensearch/client
OpenSearch HTTP client
This module provides a simple HTTP client for interacting with OpenSearch (or Elasticsearch) clusters. It handles the low-level HTTP communication needed for document operations, searches, and index management.
Quick Start
import dream_opensearch/client
import dream_opensearch/document
let client = client.new("http://localhost:9200")
// Index a document
document.index(client, "users", "123", json_body)
Base URL
The base URL should point to your OpenSearch cluster. It can include authentication if needed:
http://localhost:9200https://search.example.com:9200https://user:pass@search.example.com:9200
Types
OpenSearch client configuration
Represents a connection to an OpenSearch cluster. The client stores the base URL and handles HTTP communication with the cluster.
Fields
base_url: The base URL of the OpenSearch cluster (without trailing slash)
pub type Client {
Client(base_url: String)
}
Constructors
-
Client(base_url: String)
Values
pub fn new(base_url: String) -> Client
Create a new OpenSearch client
Creates a client configured to connect to the specified OpenSearch cluster.
The base URL should include the protocol and port (e.g., http://localhost:9200).
Any trailing slash in the URL is automatically removed.
Parameters
base_url: The base URL of the OpenSearch cluster
Returns
A new Client configured for the specified cluster.
Example
import dream_opensearch/client
let client = client.new("http://localhost:9200")
// Or with authentication
let client = client.new("https://user:pass@search.example.com:9200")
pub fn send_request(
client: Client,
method: http.Method,
path: String,
body: String,
) -> Result(String, String)
Send HTTP request to OpenSearch
Sends a raw HTTP request to the OpenSearch cluster. This is used internally by the document and query modules. Most applications should use those higher-level modules instead.
The request automatically includes the Content-Type: application/json header.
Parameters
client: The OpenSearch clientmethod: The HTTP method (GET, POST, PUT, DELETE, etc.)path: The API path (e.g., “/users/_doc/123” or “/users/_search”)body: The request body as a JSON string
Returns
Ok(String): The response body as a stringError(String): An error message if the request failed
Example
import dream_opensearch/client
import gleam/http
let result = client.send_request(
client,
http.Get,
"/users/_doc/123",
""
)