10 个版本
0.1.9 | 2024年8月2日 |
---|---|
0.1.8 | 2024年7月9日 |
0.1.7 | 2024年6月27日 |
0.1.5 | 2023年10月30日 |
6 在 #search-query 中排名
每月下载 149 次
43KB
1K SLoC
os-query-builder-rs
用于生成 Open Search 查询的库。
安装
[dependencies]
os-query-builder-rs = "0.1.9"
使用示例
MultiMatch
let multi_match = MultiMatch::new()
.fields(vec!["brands", "articles"])
.value("oc47")
.operator(Operator::And)
.query_type(Type::BestFields)
.boost(2)
.minimum_should_match("90%");
let query = Query::new()
.source(vec!["test"])
.query(multi_match);
生成以下查询
{
"_source": [
"test"
],
"query": {
"multi_match": {
"boost": 2,
"fields": [
"brands",
"articles"
],
"minimum_should_match": 2,
"operator": "and",
"query": "oc47",
"type": "best_fields"
}
}
}
词级别
Terms 查询
let terms = Terms::new_with_terms_query("product_id", vec!["128660147","127875288",]).boost(0.7);
let query = Query::new().query(terms)
.source(vec!["product_id", "brand", "article", "name",]);
生成以下查询
{
"_source": [
"product_id",
"brand",
"article",
"name"
],
"query": {
"terms": {
"boost": 0.7,
"product_id": [
"128660147",
"127875288"
]
}
}
}
Terms 查找
let terms_lookup = TermsLookup::new("sku", "131356111", "brand_id")
.routing("brand_id");
let terms = Terms::new_with_terms_lookup("brand_id", terms_lookup)
.boost(0.7);
let query = Query::new().query(terms);
生成以下查询
{
"query": {
"terms": {
"boost": 0.7,
"brand_id": {
"id": "131356111",
"index": "sku",
"path": "brand_id",
"routing": "brand_id"
}
}
}
}
Term
let term = Term::new("arcticle", "43935055")
.case_insensitive(true)
.boost(0.6);
let query = Query::new().query(term);
生成以下查询
{
"query": {
"term": {
"arcticle": {
"boost":0.6,
"case_insensitive":true,
"value":"43935055"
}
}
}
}
复合查询
布尔查询
简单查询
let match_value = Match::new().field("brand").value("FIAT");
let boolean = Bool::new().must(vec![CompoundQueryBoolean::Match(match_value)]);
let query = Query::new().query(boolean);
生成以下查询
{
"query": {
"bool": {
"must": [
{
"match": {
"brand": {
"query": "FIAT"
}
}
}
]
}
}
}
嵌套 bool-query 查询
let term = Term::new("name", "Деталь");
let must_boolean = Bool::new().must_not(vec![term]);
let finish_boolean = Bool::new().should(vec![must_boolean]);
let query = Query::new().query(finish_boolean);
生成以下查询
{
"query": {
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"term": {
"name": {
"value": "Деталь"
}
}
}
]
}
}
]
}
}
}
开发计划
- 复合查询
- 提升 (https://opensearch.org/docs/latest/query-dsl/compound/boosting/)
- 常量得分 (https://opensearch.org/docs/latest/query-dsl/compound/constant-score/)
- 析取最大值 (https://opensearch.org/docs/latest/query-dsl/compound/disjunction-max/)
- 函数得分 (https://opensearch.org/docs/latest/query-dsl/compound/function-score/)
- 混合 (https://opensearch.org/docs/latest/query-dsl/compound/hybrid/)
- 聚合 (https://opensearch.org/docs/latest/aggregations/index/)
依赖项
~0.7–1.6MB
~35K SLoC