24 个版本
使用旧版 Rust 2015
0.21.0-pre.5 | 2019 年 7 月 7 日 |
---|---|
0.21.0-pre.2 | 2019 年 2 月 23 日 |
0.20.10 | 2018 年 5 月 7 日 |
0.20.9 | 2018 年 2 月 6 日 |
0.1.0 | 2017 年 2 月 20 日 |
#30 在 #elastic
319 每月下载量
用于 2 crates
31KB
792 行
elastic

elastic
是一个高效、模块化的 API 客户端,用于 Elasticsearch,由 Rust 编写。API 面向 Elastic Stack 7.x
。
elastic
提供强类型文档和弱类型查询。
快速参考
还可以查看官方的 elasticsearch crate!
稳定性
这个 crate 仍然相当不稳定,预计在未来的一段时间内将继续发布破坏性的版本,更改日志不太详细。
如果您在自己的开源项目中遇到升级问题,请随时 创建一个 issue,我们将为您提供帮助。我们的目标是最终提供一个稳定的 API。
构建状态
平台 | 渠道 | 状态 (master ) |
---|---|---|
Linux / macOS | 稳定/夜间 | |
Windows | 夜间 |
文档
版本 | 文档 |
---|---|
当前 (master ) |
示例
将 elastic
添加到您的 Cargo.toml
[dependencies]
elastic = "0.21.0-pre.5"
elastic_derive = "0.21.0-pre.5"
serde_json = "1"
创建一个 SyncClient
并开始发送请求
#[macro_use]
extern crate elastic_derive;
#[macro_use]
extern crate serde_json;
extern crate elastic;
use serde_json::Value;
use elastic::prelude::*;
// A reqwest HTTP client and default parameters.
// The builder includes the base node url (https://127.0.0.1:9200).
let client = SyncClient::builder().build()?;
let query = "some query string";
// A search request with a freeform body.
let res = client.search::<Value>()
.index("_all")
.body(json!({
"query": {
"query_string": {
"query": query
}
}
}))
.send()?;
// Iterate through the hits in the response.
for hit in res.hits() {
println!("{:?}", hit);
}
elastic
还提供了一个用于与 tokio
异步 I/O 堆栈一起使用的 AsyncClient
。请参阅 示例 文件夹以获取完整的示例。
构建文档
文档映射是在编译时从您的普通Rust结构推导出来的。只需添加一个#[derive(ElasticType)]
属性即可。
#[derive(ElasticType, Serialize, Deserialize)]
struct MyDocument {
#[elastic(id)]
pub id: String,
pub title: String,
pub timestamp: Date<DefaultDateMapping<EpochMillis>>,
pub content: Text<DefaultTextMapping>,
}
您可以在Client
请求方法中使用MyDocument
。
有关详细信息,请参阅文档。
替代方案
elastic.co 已经发布了官方客户端,elasticsearch。尽管它目前仍处于alpha阶段(截至2020-02-10),但它非常全面,并且大部分代码都是从官方REST API规范生成的。
此外,如果您想使用强类型Query DSL构建器,请参阅rs-es
。这个客户端做了大量工作,为与Elasticsearch交互提供了一个符合Rust语法的API。它的优点是让您知道查询将在编译时而不是运行时解析。
目标
为了提供异步io上的完整功能和高效的Elasticsearch客户端,Rust为我们提供了构建高性能且易于访问的库的大量工具,这是我们旨在继续的方向。《code>elastic旨在为需要使用Elasticsearch并考虑使用Rust的人,以及已经使用Rust的用户。我们希望提供一个从Rust生态系统内部和外部与Elasticsearch交互的解决方案。
REST API由类似于serde_json
的简单内联JSON宏json!
进行覆盖,因此始终可以构建任何查询。这意味着您不需要学习另一个用于与Elasticsearch交互的API;在Dev Tools中模拟的查询可以直接复制粘贴到您的Rust源代码中。
此项目的核心重点是在Elasticsearch中的文档类型和查询响应上实现强类型,而不是试图映射整个Query DSL。
根据需要,可以将Elastic的插件产品(如watcher
和graph
)作为功能门控模块添加。
许可证
受以下任一许可证的许可
- Apache许可证版本2.0(《LICENSE-APACHE》或https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证(《LICENSE-MIT》或http://opensource.org/licenses/MIT)
lib.rs
:
Elasticsearch代码生成
Elasticsearch类型实现的编译时代码生成。此crate为elastic
crate中的数据类型提供自定义derive
属性。
依赖关系
~4.5MB
~96K SLoC