solrstice 0.15.1

A Solr 8+ client
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use std::collections::{BTreeMap, HashMap};

use serde::{Deserialize, Serialize, Serializer};

/// Get self defined facets.
/// # Examples
/// ```no_run
/// use solrstice::{AsyncSolrCloudClient, JsonFacetComponent, JsonQueryFacet, SelectQuery, SolrSingleServerHost};
/// # use solrstice::SolrServerContextBuilder;
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let context = SolrServerContextBuilder::new(SolrSingleServerHost::new("http://localhost:8983")).build();
/// let client = AsyncSolrCloudClient::new(context);
///  let query = SelectQuery::new().json_facet(
///     JsonFacetComponent::new().facets([("below_60", JsonQueryFacet::new().q("age:[0 TO 59]"))]),
/// );
/// let response = client
///     .select(&query, "collection_name")
///     .await?;
/// let facets = response.get_json_facets().ok_or("No facets")?;
/// let below_60 = facets
///     .get_nested_facets()
///     .get("below_60")
///     .ok_or("No below_60 facet")?;
/// assert_eq!(below_60.get_count().ok_or("No count")?, 4);
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct JsonFacetComponent {
    #[serde(rename = "json.facet", serialize_with = "json_facet_as_string")]
    facet: HashMap<String, JsonFacetType>,
}

impl AsRef<JsonFacetComponent> for JsonFacetComponent {
    fn as_ref(&self) -> &JsonFacetComponent {
        self
    }
}

impl From<&JsonFacetComponent> for JsonFacetComponent {
    fn from(component: &JsonFacetComponent) -> Self {
        component.clone()
    }
}

impl JsonFacetComponent {
    /// Create a new instance of [JsonFacetComponent].
    /// # Examples
    /// ```no_run
    /// use solrstice::{AsyncSolrCloudClient, JsonFacetComponent, JsonQueryFacet, SelectQuery, SolrSingleServerHost};
    /// # use solrstice::SolrServerContextBuilder;
    /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
    /// # let context = SolrServerContextBuilder::new(SolrSingleServerHost::new("http://localhost:8983")).build();
    /// let client = AsyncSolrCloudClient::new(context);
    ///  let query = SelectQuery::new().json_facet(
    ///     JsonFacetComponent::new().facets([("below_60", JsonQueryFacet::new().q("age:[0 TO 59]"))]),
    /// );
    /// let response = client
    ///     .select(&query, "collection_name")
    ///     .await?;
    /// let facets = response.get_json_facets().ok_or("No facets")?;
    /// let below_60 = facets
    ///     .get_nested_facets()
    ///     .get("below_60")
    ///     .ok_or("No below_60 facet")?;
    /// assert_eq!(below_60.get_count().ok_or("No count")?, 4);
    /// # Ok(())
    /// # }
    /// ```
    pub fn new() -> Self {
        JsonFacetComponent {
            facet: Default::default(),
        }
    }

    /// Set the facets
    pub fn facets<K: Into<String>, V: Into<JsonFacetType>, I: IntoIterator<Item = (K, V)>>(
        mut self,
        facets: I,
    ) -> Self {
        self.facet = facets
            .into_iter()
            .map(|(k, v)| (k.into(), v.into()))
            .collect();
        self
    }
}

impl Default for JsonFacetComponent {
    fn default() -> Self {
        JsonFacetComponent::new()
    }
}

fn json_facet_as_string<S>(
    facet: &HashMap<String, JsonFacetType>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let json_string = serde_json::to_string(facet).map_err(serde::ser::Error::custom)?;
    serializer.serialize_str(&json_string)
}

/// The different types of facets supported by JsonFacet
///
/// [JsonTermsFacet] [JsonQueryFacet] [JsonStatFacet]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum JsonFacetType {
    Terms(Box<JsonTermsFacet>),
    Query(Box<JsonQueryFacet>),
    Stat(JsonStatFacet),
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum JsonFacetSortDirection {
    #[serde(rename = "asc")]
    Asc,
    #[serde(rename = "desc")]
    Desc,
}

/// A facet that counts the number of documents that match a query
/// # Examples
/// ```no_run
/// # use solrstice::{AsyncSolrCloudClient, JsonFacetComponent, JsonQueryFacet, JsonStatFacet, JsonTermsFacet, SelectQuery, SolrSingleServerHost};
/// # use solrstice::SolrServerContextBuilder;
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let context = SolrServerContextBuilder::new(SolrSingleServerHost::new("http://localhost:8983")).build();
/// let client = AsyncSolrCloudClient::new(context);
/// let query = SelectQuery::new()
///     .json_facet(JsonFacetComponent::new().facets([("age", JsonTermsFacet::new("age"))]));
/// let response = client
///     .select(&query, "collection_name")
///     .await?;
/// let facets = response.get_json_facets().ok_or("No facets")?;
/// let age = facets
///     .get_nested_facets()
///     .get("age")
///     .ok_or("No age facet")?;
/// let buckets = age
///     .get_buckets()
///     .collect::<Vec<_>>();
/// assert_eq!(buckets.len(), 3);
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct JsonTermsFacet {
    #[serde(rename = "type")]
    type_: String,
    field: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    offset: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    limit: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    sort: Option<BTreeMap<String, JsonFacetSortDirection>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    prelim_sort: Option<BTreeMap<String, JsonFacetSortDirection>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    overrequest: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    refine: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    overrefine: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    mincount: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    missing: Option<bool>,
    #[serde(rename = "numBuckets", skip_serializing_if = "Option::is_none")]
    num_buckets: Option<bool>,
    #[serde(rename = "allBuckets", skip_serializing_if = "Option::is_none")]
    all_buckets: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    facet: Option<HashMap<String, JsonFacetType>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    method: Option<JsonTermsFacetMethod>,
}

impl From<JsonTermsFacet> for JsonFacetType {
    fn from(facet: JsonTermsFacet) -> Self {
        JsonFacetType::Terms(Box::new(facet))
    }
}

impl JsonTermsFacet {
    /// Create a new terms facet
    /// A facet that counts the number of documents that match a query
    /// # Examples
    /// ```no_run
    /// use solrstice::{AsyncSolrCloudClient, JsonFacetComponent, JsonQueryFacet, JsonStatFacet, JsonTermsFacet, SelectQuery, SolrSingleServerHost};
    /// # use solrstice::SolrServerContextBuilder;
    /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
    /// # let context = SolrServerContextBuilder::new(SolrSingleServerHost::new("http://localhost:8983")).build();
    /// let client = AsyncSolrCloudClient::new(context);
    /// let query = SelectQuery::new()
    ///     .json_facet(JsonFacetComponent::new().facets([("age", JsonTermsFacet::new("age"))]));
    /// let response = client
    ///     .select(&query, "collection_name")
    ///     .await?;
    /// let facets = response.get_json_facets().ok_or("No facets")?;
    /// let age = facets
    ///     .get_nested_facets()
    ///     .get("age")
    ///     .ok_or("No age facet")?;
    /// let buckets = age
    ///     .get_buckets()
    ///     .collect::<Vec<_>>();
    /// assert_eq!(buckets.len(), 3);
    /// # Ok(())
    /// # }
    /// ```
    pub fn new<S: Into<String>>(field: S) -> Self {
        JsonTermsFacet {
            type_: "terms".to_string(),
            field: field.into(),
            offset: None,
            limit: None,
            sort: None,
            prelim_sort: None,
            overrequest: None,
            refine: None,
            overrefine: None,
            mincount: None,
            missing: None,
            num_buckets: None,
            all_buckets: None,
            prefix: None,
            facet: None,
            method: None,
        }
    }

    /// Set the offset of the facet
    pub fn offset<O: Into<Option<usize>>>(mut self, offset: O) -> Self {
        self.offset = offset.into();
        self
    }

    /// Limit the number of facet results
    pub fn limit<O: Into<Option<usize>>>(mut self, limit: O) -> Self {
        self.limit = limit.into();
        self
    }

    /// Sort the facet results
    pub fn sort<
        K: Into<String>,
        V: Into<JsonFacetSortDirection>,
        I: IntoIterator<Item = (K, V)>,
        O: Into<Option<I>>,
    >(
        mut self,
        sort: O,
    ) -> Self {
        self.sort = sort.into().map(|sort| {
            sort.into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect()
        });
        self
    }

    /// Prelim sort the facet results
    pub fn prelim_sort<
        K: Into<String>,
        V: Into<JsonFacetSortDirection>,
        I: IntoIterator<Item = (K, V)>,
        O: Into<Option<I>>,
    >(
        mut self,
        sort: O,
    ) -> Self {
        self.prelim_sort = sort.into().map(|sort| {
            sort.into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect()
        });
        self
    }

    pub fn overrequest<O: Into<Option<usize>>>(mut self, overrequest: O) -> Self {
        self.overrequest = overrequest.into();
        self
    }

    pub fn refine<O: Into<Option<bool>>>(mut self, refine: O) -> Self {
        self.refine = refine.into();
        self
    }

    pub fn overrefine<O: Into<Option<usize>>>(mut self, overrefine: O) -> Self {
        self.overrefine = overrefine.into();
        self
    }

    /// The minimum count for a bucket to be included
    pub fn mincount<O: Into<Option<usize>>>(mut self, mincount: O) -> Self {
        self.mincount = mincount.into();
        self
    }

    /// Add a special bucket that contains the number of documents that were not counted
    pub fn missing<O: Into<Option<bool>>>(mut self, missing: O) -> Self {
        self.missing = missing.into();
        self
    }

    /// Add a special bucket that contains the number of buckets for the facet
    pub fn num_buckets<O: Into<Option<bool>>>(mut self, num_buckets: O) -> Self {
        self.num_buckets = num_buckets.into();
        self
    }

    /// Add a special bucket
    pub fn all_buckets<O: Into<Option<bool>>>(mut self, all_buckets: O) -> Self {
        self.all_buckets = all_buckets.into();
        self
    }

    /// Only return buckets with the following prefix
    pub fn prefix<S: Into<String>, O: Into<Option<S>>>(mut self, prefix: O) -> Self {
        self.prefix = prefix.into().map(|s| s.into());
        self
    }

    /// Add sub-facets to the facet
    pub fn facets<
        K: Into<String>,
        V: Into<JsonFacetType>,
        I: IntoIterator<Item = (K, V)>,
        O: Into<Option<I>>,
    >(
        mut self,
        facets: O,
    ) -> Self {
        self.facet = facets.into().map(|facets| {
            facets
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect()
        });
        self
    }

    /// Which facet algorithm to use
    pub fn method<O: Into<Option<JsonTermsFacetMethod>>>(mut self, method: O) -> Self {
        self.method = method.into();
        self
    }
}

/// The facet algorithm to use
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq)]
pub enum JsonTermsFacetMethod {
    #[serde(rename = "dv")]
    DocValues,
    #[serde(rename = "uif")]
    UnInvertedField,
    #[serde(rename = "dvhash")]
    DocValuesHash,
    #[serde(rename = "enum")]
    Enum,
    #[serde(rename = "stream")]
    Stream,
    #[serde(rename = "smart")]
    Smart,
}

/// A facet that does a query and returns the number of documents that match
/// # Examples
/// ```no_run
/// use solrstice::{AsyncSolrCloudClient, JsonFacetComponent, JsonQueryFacet, JsonStatFacet, SelectQuery, SolrSingleServerHost};
/// # use solrstice::SolrServerContextBuilder;
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let context = SolrServerContextBuilder::new(SolrSingleServerHost::new("http://localhost:8983")).build();
/// let client = AsyncSolrCloudClient::new(context);
/// let query = SelectQuery::new().json_facet(
///     JsonFacetComponent::new().facets([("below_60", JsonQueryFacet::new().q("age:[0 TO 59]"))]),
/// );
/// let response = client
///     .select(&query, "collection_name")
///     .await?;
/// let facets = response.get_json_facets().ok_or("No facets")?;
/// let below_60 = facets
///     .get_nested_facets()
///     .get("below_60")
///     .ok_or("No below_60 facet")?;
/// assert_eq!(below_60.get_count().ok_or("No count")?, 4);
/// # Ok(())
/// # }
/// ```

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct JsonQueryFacet {
    #[serde(rename = "type")]
    type_: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    q: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    limit: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    offset: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    sort: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    fq: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    facet: Option<HashMap<String, JsonFacetType>>,
}

impl From<JsonQueryFacet> for JsonFacetType {
    fn from(facet: JsonQueryFacet) -> Self {
        JsonFacetType::Query(Box::new(facet))
    }
}

impl JsonQueryFacet {
    /// Create a new query facet
    /// # Examples
    /// ```no_run
    /// use solrstice::{AsyncSolrCloudClient, JsonFacetComponent, JsonQueryFacet, JsonStatFacet, SelectQuery, SolrSingleServerHost};
    /// # use solrstice::SolrServerContextBuilder;
    /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
    /// # let context = SolrServerContextBuilder::new(SolrSingleServerHost::new("http://localhost:8983")).build();
    /// let client = AsyncSolrCloudClient::new(context);
    /// let query = SelectQuery::new().json_facet(
    ///     JsonFacetComponent::new().facets([("below_60", JsonQueryFacet::new().q("age:[0 TO 59]"))]),
    /// );
    /// let response = client
    ///     .select(&query, "collection_name")
    ///     .await?;
    /// let facets = response.get_json_facets().ok_or("No facets")?;
    /// let below_60 = facets
    ///     .get_nested_facets()
    ///     .get("below_60")
    ///     .ok_or("No below_60 facet")?;
    /// assert_eq!(below_60.get_count().ok_or("No count")?, 4);
    /// # Ok(())
    /// # }
    /// ```
    pub fn new() -> Self {
        JsonQueryFacet {
            type_: "query".to_string(),
            q: None,
            limit: None,
            offset: None,
            sort: None,
            fq: None,
            facet: None,
        }
    }

    /// Set the query for the facet
    pub fn q<S: Into<String>>(mut self, q: S) -> Self {
        self.q = Some(q.into());
        self
    }

    /// Limit the number of facet results
    pub fn limit<O: Into<Option<usize>>>(mut self, limit: O) -> Self {
        self.limit = limit.into();
        self
    }

    /// Offset the facet results
    pub fn offset<O: Into<Option<usize>>>(mut self, offset: O) -> Self {
        self.offset = offset.into();
        self
    }

    /// Sort the query facet results
    pub fn sort<S: Into<String>, O: Into<Option<S>>>(mut self, sort: O) -> Self {
        self.sort = sort.into().map(|s| s.into());
        self
    }

    /// Use filter queries to filter the query facet
    pub fn fq<S: Into<String>, I: IntoIterator<Item = S>, O: Into<Option<I>>>(
        mut self,
        fq: O,
    ) -> Self {
        self.fq = fq
            .into()
            .map(|fq| fq.into_iter().map(|s| s.into()).collect());
        self
    }

    /// Add nested facets to the query facet
    pub fn facets<
        K: Into<String>,
        V: Into<JsonFacetType>,
        I: IntoIterator<Item = (K, V)>,
        O: Into<Option<I>>,
    >(
        mut self,
        facets: O,
    ) -> Self {
        self.facet = facets.into().map(|facets| {
            facets
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect()
        });
        self
    }
}

/// A facet that does a query and gets the number of results
/// # Examples
/// ```no_run
/// use solrstice::{AsyncSolrCloudClient, JsonFacetComponent, JsonQueryFacet, JsonStatFacet, SelectQuery, SolrSingleServerHost};
/// # use solrstice::SolrServerContextBuilder;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let context = SolrServerContextBuilder::new(SolrSingleServerHost::new("http://localhost:8983")).build();
/// let client = AsyncSolrCloudClient::new(context);
/// let query = SelectQuery::new().json_facet(
///     JsonFacetComponent::new().facets([("total_people", JsonStatFacet::new("sum(count)"))]),
/// );
/// let response = client
///     .select(&query, "collection_name")
///     .await?;
/// let facets = response.get_json_facets().ok_or("No facets")?;
/// let total_people = facets
///     .get_flat_facets()
///     .get("total_people")
///     .ok_or("No total_people facet")?;
/// assert_eq!(total_people.as_f64().ok_or("Not a number")?, 1000.0);
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct JsonStatFacet(String);

impl From<JsonStatFacet> for JsonFacetType {
    fn from(facet: JsonStatFacet) -> Self {
        JsonFacetType::Stat(facet)
    }
}

impl JsonStatFacet {
    /// Create a new JsonStatFacet
    /// # Examples
    /// ```no_run
    /// use solrstice::{AsyncSolrCloudClient, JsonFacetComponent, JsonQueryFacet, JsonStatFacet, SelectQuery, SolrSingleServerHost};
    /// # use solrstice::SolrServerContextBuilder;
    /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
    /// # let context = SolrServerContextBuilder::new(SolrSingleServerHost::new("http://localhost:8983")).build();
    /// let client = AsyncSolrCloudClient::new(context);
    /// let query = SelectQuery::new().json_facet(
    ///     JsonFacetComponent::new().facets([("total_people", JsonStatFacet::new("sum(count)"))]),
    /// );
    /// let response = client
    ///     .select(&query, "collection_name")
    ///     .await?;
    /// let facets = response.get_json_facets().ok_or("No facets")?;
    /// let total_people = facets
    ///     .get_flat_facets()
    ///     .get("total_people")
    ///     .ok_or("No total_people facet")?;
    /// assert_eq!(total_people.as_f64().ok_or("Not a number")?, 1000.0);
    /// # Ok(())
    /// # }
    /// ```
    pub fn new<S: Into<String>>(stat: S) -> Self {
        JsonStatFacet(stat.into())
    }
}