10 releases
0.1.9 | Aug 2, 2024 |
---|---|
0.1.8 | Jul 9, 2024 |
0.1.7 | Jun 27, 2024 |
0.1.5 | Oct 30, 2023 |
#5 in #search-query
63 downloads per month
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"
}
}
}
Term-level
Terms query
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 lookup
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"
}
}
}
}
Compound-query
Boolean
Простой запрос
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": "Деталь"
}
}
}
]
}
}
]
}
}
}
Планы развития
- Compound queries
- Boosting (https://opensearch.org/docs/latest/query-dsl/compound/boosting/)
- Constant score (https://opensearch.org/docs/latest/query-dsl/compound/constant-score/)
- Disjunction max (https://opensearch.org/docs/latest/query-dsl/compound/disjunction-max/)
- Function score (https://opensearch.org/docs/latest/query-dsl/compound/function-score/)
- Hybrid (https://opensearch.org/docs/latest/query-dsl/compound/hybrid/)
- Aggregations (https://opensearch.org/docs/latest/aggregations/index/)
Dependencies
~0.7–1.6MB
~35K SLoC