Expand description
§OpenSearch DSL for Rust
A high-level, strongly typed Domain Specific Language (DSL) for building OpenSearch queries in Rust. This library provides a complete mapping to the OpenSearch Query DSL with compile-time type safety.
Based on the excellent elasticsearch-dsl-rs project, adapted for OpenSearch.
§Features
- 🔒 Type Safety: Strongly typed queries, aggregations, and responses with compile-time validation
- 🎯 Complete Coverage: Full support for OpenSearch Query DSL including complex nested queries
- 📊 Rich Aggregations: Support for all aggregation types with proper result parsing
- 🧩 Composable: Build complex queries by composing smaller query components
- ⚡ Zero-Cost Abstractions: Compiles to efficient JSON with no runtime overhead
- 🔌 Client Agnostic: Works with any HTTP client, not tied to specific OpenSearch client libraries
- 📝 Auto-Generated JSON: Automatically produces valid OpenSearch JSON from Rust code
- 🎨 Fluent API: Chainable method calls for intuitive query building
§Installation
Add to your Cargo.toml:
[dependencies]
opensearch-dsl = "0.3"§Quick Start
§Basic Search Query
use opensearch_dsl::*;
let search = Search::new()
.source(false)
.from(0)
.size(10)
.query(Query::match_all())
.sort(vec![Sort::field("timestamp").desc()]);
// Generates valid OpenSearch JSON
let json = serde_json::to_string(&search)?;§Complex Boolean Query
let search = Search::new()
.query(
Query::bool()
.must(vec![
Query::match_("title", "OpenSearch"),
Query::range("date").gte("2023-01-01")
])
.should(vec![
Query::term("category", "tutorial"),
Query::term("featured", true)
])
.filter(vec![
Query::term("status", "published")
])
.minimum_should_match(1)
);§Aggregations
let search = Search::new()
.size(0)
.aggregations(vec![
("categories", Aggregation::terms("category")),
("monthly_sales",
Aggregation::date_histogram("date", "month")
.sub_aggregation("total_revenue", Aggregation::sum("price"))
)
]);§Module Organization
search- Search queries and request building- [
query] - All query types (term, match, bool, etc.) - [
aggregation] - Aggregation types and builders sort- Sorting options and configurations- [
types] - Common types and utilities
§Integration with OpenSearch Client
This DSL works seamlessly with the opensearch-client:
use opensearch_client::*;
use opensearch_dsl::*;
let client = OsClient::new(/* configuration */);
let search = Search::new().query(Query::match_("title", "rust"));
let response = client.search(&search).index("articles").await?;§Examples
For comprehensive examples including e-commerce search, log analytics, and time series analysis, see the examples directory.
use opensearch_dsl::*;
fn main() { let query = Search::new() .source(false) .stats(“statistics”) .from(0) .size(30) .query( Query::bool() .must(Query::multi_match( [“title”, “description”], “you know, for search”, )) .filter(Query::terms(“tags”, [“opensearch”])) .should(Query::term(“verified”, true).boost(10)), ) .aggregate( “country_ids”, Aggregation::terms(“country_id”) .aggregate(“catalog_ids”, Aggregation::terms(“catalog_id”)) .aggregate(“company_ids”, Aggregation::terms(“company_id”)) .aggregate( “top1”, Aggregation::top_hits() .size(1) .sort(FieldSort::ascending(“user_id”)), ), ) .rescore(Rescore::new(Query::term(“field”, 1)).query_weight(1.2)); }
See examples for more.
### License
<sup>
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
</sup>Re-exports§
Modules§
- analyze
- Performs analysis on a text string and returns the resulting tokens.
- search
- Search APIs are used to search and aggregate data stored in OpenSearch indices and data streams. For an overview and related tutorials, see Search your data.
- util
- Module containing helpers and util functions that are not specific to any DSL