flusso-query 0.9.2

Backend-neutral OpenSearch/Elasticsearch query client for flusso indexes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! Standalone query types that aren't tied to one field handle: document-id
//! lookup ([`ids`]), the user-facing full-text strings ([`query_string`],
//! [`simple_query_string`], [`combined_fields`]), and the advanced-relevance
//! queries ([`script`], [`script_score`], [`distance_feature`], [`rank_feature`],
//! [`more_like_this`]). Each returns a builder implementing [`AsQuery`].

use std::marker::PhantomData;

use serde_json::{Map, Value};

use super::{Common, MinimumShouldMatch, Operator, Text, common_opts, wrap};
use crate::query::{AsQuery, Query, Root};

fn string_array(values: impl IntoIterator<Item = impl Into<String>>) -> Vec<Value> {
    values
        .into_iter()
        .map(|v| Value::String(v.into()))
        .collect()
}

fn field_specs<S, Sub>(fields: impl IntoIterator<Item = Text<S, Sub>>) -> Vec<Value> {
    fields
        .into_iter()
        .map(|f| Value::String(f.field_spec()))
        .collect()
}

/// Match documents by a list of `_id` values — typed get-many-by-id.
pub fn ids<S>(values: impl IntoIterator<Item = impl Into<String>>) -> IdsQuery<S> {
    IdsQuery {
        values: string_array(values),
        common: Common::default(),
        _scope: PhantomData,
    }
}

/// An `ids` clause.
#[derive(Debug, Clone)]
pub struct IdsQuery<S = Root> {
    values: Vec<Value>,
    common: Common,
    _scope: PhantomData<fn() -> S>,
}

impl<S> IdsQuery<S> {
    common_opts!(common);
}

impl<S> AsQuery<S> for IdsQuery<S> {
    fn into_query(self) -> Option<Query<S>> {
        let mut body = Map::new();
        body.insert("values".to_string(), Value::Array(self.values));
        self.common.write(&mut body);
        Some(wrap("ids", body))
    }
}

/// Full Lucene query-string syntax (power users; can error on malformed input).
/// Target fields with [`default_field`](QueryStringQuery::default_field) or
/// [`fields`](QueryStringQuery::fields).
pub fn query_string<S>(query: impl Into<String>) -> QueryStringQuery<S> {
    QueryStringQuery {
        query: query.into(),
        opts: Map::new(),
        common: Common::default(),
        _scope: PhantomData,
    }
}

/// A `query_string` clause.
#[derive(Debug, Clone)]
pub struct QueryStringQuery<S = Root> {
    query: String,
    opts: Map<String, Value>,
    common: Common,
    _scope: PhantomData<fn() -> S>,
}

impl<S> QueryStringQuery<S> {
    /// The single field searched when the query text names none.
    #[must_use]
    pub fn default_field(mut self, field: impl Into<String>) -> Self {
        self.opts
            .insert("default_field".to_string(), Value::String(field.into()));
        self
    }

    /// The fields searched (each may carry a `^weight` via [`Text::boosted`]).
    #[must_use]
    pub fn fields<Sub>(mut self, fields: impl IntoIterator<Item = Text<S, Sub>>) -> Self {
        self.opts
            .insert("fields".to_string(), Value::Array(field_specs(fields)));
        self
    }

    /// Default boolean operator between terms ([`Operator::Or`] default).
    #[must_use]
    pub fn default_operator(mut self, operator: Operator) -> Self {
        self.opts.insert(
            "default_operator".to_string(),
            Value::String(operator.as_str().to_string()),
        );
        self
    }

    /// Override the analyzer applied to the query text.
    #[must_use]
    pub fn analyzer(mut self, analyzer: impl Into<String>) -> Self {
        self.opts
            .insert("analyzer".to_string(), Value::String(analyzer.into()));
        self
    }

    /// Ignore format errors (e.g. text against a numeric field).
    #[must_use]
    pub fn lenient(mut self, lenient: bool) -> Self {
        self.opts
            .insert("lenient".to_string(), Value::Bool(lenient));
        self
    }

    /// Set any other `query_string` parameter verbatim.
    #[must_use]
    pub fn param(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
        self.opts.insert(key.into(), value.into());
        self
    }

    common_opts!(common);
}

impl<S> AsQuery<S> for QueryStringQuery<S> {
    fn into_query(self) -> Option<Query<S>> {
        let mut body = self.opts;
        body.insert("query".to_string(), Value::String(self.query));
        self.common.write(&mut body);
        Some(wrap("query_string", body))
    }
}

/// Lenient search-bar syntax over chosen fields — never errors on bad input.
pub fn simple_query_string<S>(query: impl Into<String>) -> SimpleQueryStringQuery<S> {
    SimpleQueryStringQuery {
        query: query.into(),
        opts: Map::new(),
        common: Common::default(),
        _scope: PhantomData,
    }
}

/// A `simple_query_string` clause.
#[derive(Debug, Clone)]
pub struct SimpleQueryStringQuery<S = Root> {
    query: String,
    opts: Map<String, Value>,
    common: Common,
    _scope: PhantomData<fn() -> S>,
}

impl<S> SimpleQueryStringQuery<S> {
    /// The fields searched (each may carry a `^weight` via [`Text::boosted`]).
    #[must_use]
    pub fn fields<Sub>(mut self, fields: impl IntoIterator<Item = Text<S, Sub>>) -> Self {
        self.opts
            .insert("fields".to_string(), Value::Array(field_specs(fields)));
        self
    }

    /// Default boolean operator between terms ([`Operator::Or`] default).
    #[must_use]
    pub fn default_operator(mut self, operator: Operator) -> Self {
        self.opts.insert(
            "default_operator".to_string(),
            Value::String(operator.as_str().to_string()),
        );
        self
    }

    /// Override the analyzer applied to the query text.
    #[must_use]
    pub fn analyzer(mut self, analyzer: impl Into<String>) -> Self {
        self.opts
            .insert("analyzer".to_string(), Value::String(analyzer.into()));
        self
    }

    /// Enabled syntax features (e.g. `"AND|OR|PREFIX"`).
    #[must_use]
    pub fn flags(mut self, flags: impl Into<String>) -> Self {
        self.opts
            .insert("flags".to_string(), Value::String(flags.into()));
        self
    }

    /// How many terms must match (e.g. `2`, `MinimumShouldMatch::percent(75)`).
    #[must_use]
    pub fn minimum_should_match(mut self, value: impl Into<MinimumShouldMatch>) -> Self {
        self.opts
            .insert("minimum_should_match".to_string(), value.into().to_value());
        self
    }

    common_opts!(common);
}

impl<S> AsQuery<S> for SimpleQueryStringQuery<S> {
    fn into_query(self) -> Option<Query<S>> {
        let mut body = self.opts;
        body.insert("query".to_string(), Value::String(self.query));
        self.common.write(&mut body);
        Some(wrap("simple_query_string", body))
    }
}

/// Term-centric full-text across several fields, treating them as one combined
/// field (`combined_fields`).
pub fn combined_fields<S, Sub>(
    query: impl Into<String>,
    fields: impl IntoIterator<Item = Text<S, Sub>>,
) -> CombinedFieldsQuery<S> {
    CombinedFieldsQuery {
        query: query.into(),
        fields: field_specs(fields),
        opts: Map::new(),
        common: Common::default(),
        _scope: PhantomData,
    }
}

/// A `combined_fields` clause.
#[derive(Debug, Clone)]
pub struct CombinedFieldsQuery<S = Root> {
    query: String,
    fields: Vec<Value>,
    opts: Map<String, Value>,
    common: Common,
    _scope: PhantomData<fn() -> S>,
}

impl<S> CombinedFieldsQuery<S> {
    /// Combine analyzed terms with [`Operator::And`] or [`Operator::Or`].
    #[must_use]
    pub fn operator(mut self, operator: Operator) -> Self {
        self.opts.insert(
            "operator".to_string(),
            Value::String(operator.as_str().to_string()),
        );
        self
    }

    /// How many terms must match (e.g. `2`, `MinimumShouldMatch::percent(75)`).
    #[must_use]
    pub fn minimum_should_match(mut self, value: impl Into<MinimumShouldMatch>) -> Self {
        self.opts
            .insert("minimum_should_match".to_string(), value.into().to_value());
        self
    }

    common_opts!(common);
}

impl<S> AsQuery<S> for CombinedFieldsQuery<S> {
    fn into_query(self) -> Option<Query<S>> {
        let mut body = self.opts;
        body.insert("query".to_string(), Value::String(self.query));
        body.insert("fields".to_string(), Value::Array(self.fields));
        self.common.write(&mut body);
        Some(wrap("combined_fields", body))
    }
}

/// Keep documents for which a painless `source` returns true (a non-scoring
/// filter).
pub fn script<S>(source: impl Into<String>) -> ScriptQuery<S> {
    ScriptQuery {
        source: source.into(),
        params: None,
        lang: None,
        common: Common::default(),
        _scope: PhantomData,
    }
}

/// A `script` clause.
#[derive(Debug, Clone)]
pub struct ScriptQuery<S = Root> {
    source: String,
    params: Option<Value>,
    lang: Option<String>,
    common: Common,
    _scope: PhantomData<fn() -> S>,
}

impl<S> ScriptQuery<S> {
    /// Bound parameters available to the script as `params`.
    #[must_use]
    pub fn params(mut self, params: Value) -> Self {
        self.params = Some(params);
        self
    }

    /// Scripting language (default `"painless"`).
    #[must_use]
    pub fn lang(mut self, lang: impl Into<String>) -> Self {
        self.lang = Some(lang.into());
        self
    }

    common_opts!(common);
}

fn script_object(source: String, params: Option<Value>, lang: Option<String>) -> Value {
    let mut script = Map::new();
    script.insert("source".to_string(), Value::String(source));
    if let Some(params) = params {
        script.insert("params".to_string(), params);
    }
    if let Some(lang) = lang {
        script.insert("lang".to_string(), Value::String(lang));
    }
    Value::Object(script)
}

impl<S> AsQuery<S> for ScriptQuery<S> {
    fn into_query(self) -> Option<Query<S>> {
        let mut body = Map::new();
        body.insert(
            "script".to_string(),
            script_object(self.source, self.params, self.lang),
        );
        self.common.write(&mut body);
        Some(wrap("script", body))
    }
}

/// Recompute `query`'s score with a painless `source` (`script_score`).
pub fn script_score<S>(query: impl AsQuery<S>, source: impl Into<String>) -> ScriptScoreQuery<S> {
    ScriptScoreQuery {
        query: query
            .into_query()
            .map_or_else(super::match_all_value, |q| q.to_value()),
        source: source.into(),
        params: None,
        min_score: None,
        common: Common::default(),
        _scope: PhantomData,
    }
}

/// A `script_score` clause.
#[derive(Debug, Clone)]
pub struct ScriptScoreQuery<S = Root> {
    query: Value,
    source: String,
    params: Option<Value>,
    min_score: Option<f32>,
    common: Common,
    _scope: PhantomData<fn() -> S>,
}

impl<S> ScriptScoreQuery<S> {
    /// Bound parameters available to the script as `params`.
    #[must_use]
    pub fn params(mut self, params: Value) -> Self {
        self.params = Some(params);
        self
    }

    /// Exclude hits whose recomputed score is below this.
    #[must_use]
    pub fn min_score(mut self, min_score: f32) -> Self {
        self.min_score = Some(min_score);
        self
    }

    common_opts!(common);
}

impl<S> AsQuery<S> for ScriptScoreQuery<S> {
    fn into_query(self) -> Option<Query<S>> {
        let mut body = Map::new();
        body.insert("query".to_string(), self.query);
        body.insert(
            "script".to_string(),
            script_object(self.source, self.params, None),
        );
        if let Some(min_score) = self.min_score {
            body.insert("min_score".to_string(), Value::from(min_score));
        }
        self.common.write(&mut body);
        Some(wrap("script_score", body))
    }
}

/// Boost by proximity of a `field` (date or geo) to `origin`, decaying over
/// `pivot` (`distance_feature`).
pub fn distance_feature<S>(
    field: impl Into<String>,
    origin: impl Into<Value>,
    pivot: impl Into<String>,
) -> DistanceFeatureQuery<S> {
    DistanceFeatureQuery {
        field: field.into(),
        origin: origin.into(),
        pivot: pivot.into(),
        common: Common::default(),
        _scope: PhantomData,
    }
}

/// A `distance_feature` clause.
#[derive(Debug, Clone)]
pub struct DistanceFeatureQuery<S = Root> {
    field: String,
    origin: Value,
    pivot: String,
    common: Common,
    _scope: PhantomData<fn() -> S>,
}

impl<S> DistanceFeatureQuery<S> {
    common_opts!(common);
}

impl<S> AsQuery<S> for DistanceFeatureQuery<S> {
    fn into_query(self) -> Option<Query<S>> {
        let mut body = Map::new();
        body.insert("field".to_string(), Value::String(self.field));
        body.insert("origin".to_string(), self.origin);
        body.insert("pivot".to_string(), Value::String(self.pivot));
        self.common.write(&mut body);
        Some(wrap("distance_feature", body))
    }
}

/// Boost by a `rank_feature` / `rank_features` field's stored value. The
/// default saturation function applies unless one is chosen.
pub fn rank_feature<S>(field: impl Into<String>) -> RankFeatureQuery<S> {
    RankFeatureQuery {
        field: field.into(),
        function: None,
        common: Common::default(),
        _scope: PhantomData,
    }
}

/// A `rank_feature` clause.
#[derive(Debug, Clone)]
pub struct RankFeatureQuery<S = Root> {
    field: String,
    function: Option<(&'static str, Value)>,
    common: Common,
    _scope: PhantomData<fn() -> S>,
}

impl<S> RankFeatureQuery<S> {
    /// The `saturation` function with an explicit `pivot`.
    #[must_use]
    pub fn saturation(mut self, pivot: f32) -> Self {
        let mut body = Map::new();
        body.insert("pivot".to_string(), Value::from(pivot));
        self.function = Some(("saturation", Value::Object(body)));
        self
    }

    /// The `log` function with a `scaling_factor`.
    #[must_use]
    pub fn log(mut self, scaling_factor: f32) -> Self {
        let mut body = Map::new();
        body.insert("scaling_factor".to_string(), Value::from(scaling_factor));
        self.function = Some(("log", Value::Object(body)));
        self
    }

    /// The `sigmoid` function with `pivot` and `exponent`.
    #[must_use]
    pub fn sigmoid(mut self, pivot: f32, exponent: f32) -> Self {
        let mut body = Map::new();
        body.insert("pivot".to_string(), Value::from(pivot));
        body.insert("exponent".to_string(), Value::from(exponent));
        self.function = Some(("sigmoid", Value::Object(body)));
        self
    }

    common_opts!(common);
}

impl<S> AsQuery<S> for RankFeatureQuery<S> {
    fn into_query(self) -> Option<Query<S>> {
        let mut body = Map::new();
        body.insert("field".to_string(), Value::String(self.field));
        if let Some((name, function)) = self.function {
            body.insert(name.to_string(), function);
        }
        self.common.write(&mut body);
        Some(wrap("rank_feature", body))
    }
}

/// Find documents similar to `like` text(s) across `fields` (`more_like_this`).
pub fn more_like_this<S, Sub>(
    fields: impl IntoIterator<Item = Text<S, Sub>>,
    like: impl IntoIterator<Item = impl Into<String>>,
) -> MoreLikeThisQuery<S> {
    MoreLikeThisQuery {
        fields: field_specs(fields),
        like: string_array(like),
        opts: Map::new(),
        common: Common::default(),
        _scope: PhantomData,
    }
}

/// A `more_like_this` clause.
#[derive(Debug, Clone)]
pub struct MoreLikeThisQuery<S = Root> {
    fields: Vec<Value>,
    like: Vec<Value>,
    opts: Map<String, Value>,
    common: Common,
    _scope: PhantomData<fn() -> S>,
}

impl<S> MoreLikeThisQuery<S> {
    /// Ignore source terms occurring fewer than this many times.
    #[must_use]
    pub fn min_term_freq(mut self, min_term_freq: u32) -> Self {
        self.opts
            .insert("min_term_freq".to_string(), Value::from(min_term_freq));
        self
    }

    /// Cap on the terms selected from the source text.
    #[must_use]
    pub fn max_query_terms(mut self, max_query_terms: u32) -> Self {
        self.opts
            .insert("max_query_terms".to_string(), Value::from(max_query_terms));
        self
    }

    /// How many selected terms must match (e.g. `MinimumShouldMatch::percent(30)`).
    #[must_use]
    pub fn minimum_should_match(mut self, value: impl Into<MinimumShouldMatch>) -> Self {
        self.opts
            .insert("minimum_should_match".to_string(), value.into().to_value());
        self
    }

    common_opts!(common);
}

impl<S> AsQuery<S> for MoreLikeThisQuery<S> {
    fn into_query(self) -> Option<Query<S>> {
        let mut body = self.opts;
        body.insert("fields".to_string(), Value::Array(self.fields));
        body.insert("like".to_string(), Value::Array(self.like));
        self.common.write(&mut body);
        Some(wrap("more_like_this", body))
    }
}