MoonBit client for Elasticsearch v9
Dependencies
.
|-- client.mbt # Client lifecycle, headers, HTTP execution, JSON decoding
|-- encoding.mbt # URL path/query rendering and NDJSON encoding
|-- types.mbt # Public core types: ClientConfig, Auth, EsRequest, EsResponse
|-- generated_endpoints.mbt # Root-package generated Client endpoint methods
|-- v9/
| |-- generated.mbt # v9 package function wrappers and type aliases
| `-- moon.pkg
|-- cmd/codegen/
| `-- main.mbt # Schema-to-MoonBit generator
`-- spec/elasticsearch/v9/
|-- schema.json # Vendored official Elasticsearch API schema
|-- METADATA.md
|-- LICENSE
|-- validation-errors.json
|-- dangling-types.csv
`-- fixture.jsonimport {
"moonbit-community/elasticsearch" @es,
"moonbitlang/core/json",
}
supported_targets = "+native"import {
"moonbit-community/elasticsearch/v9" @esv9,
}///|
async fn main {
let client = @es.new(
"http://localhost:9200",
auth=@es.Auth::basic(username="elastic", password="secret"),
headers={ "X-Client": "my-moonbit-app" },
)
defer client.close()
let info = client.info(@es.InfoRequest::new())
println(info.body.tagline)
}///|
async fn main {
let config = @es.ClientConfig::new(
"http://localhost:9200",
auth=@es.Auth::api_key(id="elastic", api_key="secret"),
compatibility=@es.ApiCompatibility::CompatibleWith8,
headers={ "X-Client": "my-moonbit-app" },
)
let client = @es.Client::new(config)
defer client.close()
let response = client.info(@es.InfoRequest::new())
println(response.status.to_string())
}///|
async fn search_logs(client : @es.Client) -> Unit {
let response = client.search(
@es.SearchRequest::new(
index="logs-2026",
query=@es.SearchQuery::new(q="status:200", typed_keys=true, stats=[
"latency", "errors",
]),
body=@es.SearchBody::new(
query=Json::object({ "match_all": Json::empty_object() }),
size=10,
),
),
)
println(response.status.to_string())
println(response.body.stringify())
}///|
async fn index_document(client : @es.Client) -> Unit {
let document = Json::object({
"message": Json::string("hello from MoonBit"),
"service": Json::string("example"),
})
let response = client.index(
@es.IndexRequest::new(
"logs-2026",
document,
id="doc-1",
query=@es.IndexQuery::new(refresh="wait_for"),
),
)
println(response.body.stringify())
}///|
async fn bulk_index(client : @es.Client) -> Unit {
let response = client.bulk(
@es.BulkRequest::new(
[
Json::object({ "index": Json::object({ "_id": Json::string("1") }) }),
Json::object({ "message": Json::string("first document") }),
],
index="logs-2026",
query=@es.BulkQuery::new(refresh="wait_for"),
),
)
println(response.body.errors.to_string())
}///|
async fn call_v9(client : @es.Client) -> Unit {
let info = @esv9.info(client, @esv9.InfoRequest::new())
println(info.body.tagline)
let search = @esv9.search(
client,
@esv9.SearchRequest::new(
index="logs-2026",
body=@esv9.SearchBody::new(
query=Json::object({ "match_all": Json::empty_object() }),
),
),
)
println(search.body.stringify())
}///|
async fn raw_request(client : @es.Client) -> Unit {
let response = client.perform_json(
@es.EsRequest::new(@es.HttpMethod::Get, "/_cluster/health", query=[
("pretty", "true"),
]),
)
println(response.body.stringify())
}///|
async fn typed_request(client : @es.Client) -> Unit {
let response : @es.EsResponse[@es.InfoResponse] = client.perform(
@es.EsRequest::new(@es.HttpMethod::Get, "/"),
)
println(response.body.tagline)
}This package contains over 2049 items.
MoonBit client for Elasticsearch v9
Dependencies