1 个不稳定版本

0.1.0 2024年2月17日

#187HTTP客户端


用于 opensearch-client

MIT/Apache

690KB
16K SLoC

用Rust编写的强类型OpenSearch DSL

Crates.io Crates.io Crates.io Docs.io

这是一个高级库,提供了一个强类型DSL,它与官方的OpenSearch查询DSL一一对应。这是基于https://github.com/vinted/elasticsearch-dsl-rs的Opensearch分支

特性

  • 强类型查询
  • 强类型聚合
  • 强类型补全
  • 响应结构
  • 自动跳过空查询,使DSL易于使用
  • Crate不依赖于opensearch-rs,可以作为独立库与任何HTTP客户端一起使用以调用OpenSearch

安装

在Cargo.toml中添加opensearch-dsl crate和版本

[dependencies]
opensearch-dsl = "0.4"

文档

库的文档可在docs.rs上找到

快速入门

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));
}
{
  "_source": false,
  "stats": ["statistics"],
  "from": 0,
  "size": 30,
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "fields": ["title", "description"],
            "query": "you know, for search"
          }
        }
      ],
      "filter": [{ "terms": { "tags": ["opensearch"] } }],
      "should": [{ "term": { "verified": { "value": true, "boost": 10.0 } } }]
    }
  },
  "aggs": {
    "country_ids": {
      "terms": { "field": "country_id" },
      "aggs": {
        "catalog_ids": { "terms": { "field": "catalog_id" } },
        "company_ids": { "terms": { "field": "company_id" } },
        "top1": {
          "top_hits": {
            "size": 1,
            "sort": [{ "user_id": { "order": "asc" } }]
          }
        }
      }
    }
  },
  "rescore": [
    {
      "query": {
        "rescore_query": { "term": { "field": { "value": 1 } } },
        "query_weight": 1.2
      }
    }
  ]
}

更多示例请参阅示例

许可证

根据您的选择,许可协议为Apache许可证版本2.0MIT许可证

依赖关系

~1.6–2.6MB
~50K SLoC