oxsearch 0.2.0

A typed Rust client for Elasticsearch, generated from its specification, with typed AI search: retrievers, semantic and kNN queries, and inference
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
//! An index of one document type.

use std::fmt;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};

use oxsearch_admin::indices::delete as indices_delete;
use oxsearch_admin::indices::{IndexSettings, create, get_mapping, put_mapping};
use oxsearch_core::shapes::IndexMap;
use oxsearch_core::{Client, Error, Refresh, RetrieverContainer, Transport};
use oxsearch_docs::{delete, get, index, mget};
use oxsearch_mapping::{Property, TypeMapping};
use oxsearch_search::fusion::{Fused, Fusion};
use oxsearch_search::search::{self, Hit};
use serde_json::{Map, Value, json};

use super::Document;

/// Indexes of document types, from a client: `client.index::<Article>()`.
pub trait Indexes<Tr> {
    /// The index the document type's `#[index("…")]` names. A type without
    /// one doesn't compile here; use [`Indexes::index_named`].
    fn index<T: Document>(&self) -> Index<T, Tr>;

    /// An index of the document type, by name.
    fn index_named<T: Document>(&self, name: impl Into<String>) -> Index<T, Tr>;
}

impl<Tr: Transport + Clone> Indexes<Tr> for Client<Tr> {
    fn index<T: Document>(&self) -> Index<T, Tr> {
        const {
            assert!(
                T::INDEX.is_some(),
                "the document type has no #[index(\"…\")]; use index_named"
            )
        };
        Index::new(self.clone(), T::INDEX.unwrap_or_default())
    }

    fn index_named<T: Document>(&self, name: impl Into<String>) -> Index<T, Tr> {
        Index::new(self.clone(), name)
    }
}

/// An index, or an alias, holding documents of type `T`.
pub struct Index<T, Tr> {
    client: Client<Tr>,
    name: String,
    refresh: Option<Refresh>,
    document: PhantomData<fn() -> T>,
}

impl<T, Tr: Clone> Clone for Index<T, Tr> {
    fn clone(&self) -> Index<T, Tr> {
        Index {
            client: self.client.clone(),
            name: self.name.clone(),
            refresh: self.refresh.clone(),
            document: PhantomData,
        }
    }
}

impl<T, Tr> fmt::Debug for Index<T, Tr> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Index")
            .field("name", &self.name)
            .field("refresh", &self.refresh)
            .finish_non_exhaustive()
    }
}

/// A document's version, for writes conditional on it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Version {
    /// The sequence number of the document's last write.
    pub seq_no: i64,
    /// The primary term of the document's last write.
    pub primary_term: i64,
}

/// A document with its id and version. It reads as the document: change
/// it, then write it back with [`Index::put_versioned`], which fails with
/// [`Error::Conflict`] if the document changed in the meantime.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct Versioned<T> {
    /// The document's id.
    pub id: String,
    /// The version read.
    pub version: Version,
    /// The document.
    pub document: T,
}

impl<T> Deref for Versioned<T> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.document
    }
}

impl<T> DerefMut for Versioned<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.document
    }
}

/// What a write did.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Written {
    /// The document's id, Elasticsearch's when the document had none.
    pub id: String,
    /// The document's new version.
    pub version: Version,
    /// Whether the write created the document, rather than replaced it.
    pub created: bool,
}

/// How a live index's mapping compares with a document type's.
#[derive(Clone, Debug, Default, PartialEq)]
#[non_exhaustive]
#[must_use = "a mapping can drift from the document type; check `drift`"]
pub struct MappingReport {
    /// Whether the index was created, with the document type's mapping.
    pub created: bool,
    /// Fields the live mapping didn't have, which were added to it.
    pub added: Vec<String>,
    /// Fields mapped differently from the document type. Elasticsearch
    /// can't change a field's type in place, so these are left alone: a
    /// change needs a new index and a reindex.
    pub drift: Vec<Drift>,
}

impl MappingReport {
    /// Whether the live mapping now has every field as the document type
    /// maps it.
    pub fn is_current(&self) -> bool {
        self.drift.is_empty()
    }
}

/// A field mapped differently from the document type.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct Drift {
    /// The field, with its path through any objects.
    pub field: String,
    /// The document type's mapping of it.
    pub expected: Value,
    /// The live index's.
    pub actual: Value,
}

/// The parameters that make a field what it is, beside its type: a change
/// to one of these is drift too.
const IDENTITY: &[&str] = &["dims", "similarity", "element_type", "inference_id"];

/// Mapped types Elasticsearch leaves out of the `_source` it returns unless
/// the index says otherwise.
const VECTORS: &[&str] = &["dense_vector", "sparse_vector", "rank_vectors"];

impl<T: Document, Tr: Transport> Index<T, Tr> {
    /// An index of documents of type `T`, by name.
    pub fn new(client: Client<Tr>, name: impl Into<String>) -> Index<T, Tr> {
        Index {
            client,
            name: name.into(),
            refresh: None,
            document: PhantomData,
        }
    }

    /// The index's name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The client.
    pub fn client(&self) -> &Client<Tr> {
        &self.client
    }

    /// Writes refresh the index this way, to make them visible to searches
    /// sooner: `Refresh::WaitFor` waits for the next refresh. By default
    /// they don't.
    pub fn refresh(mut self, refresh: Refresh) -> Index<T, Tr> {
        self.refresh = Some(refresh);
        self
    }

    /// Makes the index's mapping the document type's: creates the index if
    /// it doesn't exist, adds the fields it lacks, and reports fields mapped
    /// differently, which it never changes.
    ///
    /// A new index for a type with vector fields keeps vectors in the
    /// `_source` it returns (`index.mapping.exclude_source_vectors: false`),
    /// so documents read back whole.
    pub async fn ensure_mapping(&self) -> Result<MappingReport, Error> {
        let properties = T::properties();
        let expected = serde_json::to_value(&properties).map_err(Error::Encode)?;
        let expected = expected.as_object().cloned().unwrap_or_default();
        let live = match self
            .client
            .send(&get_mapping::Request::new().index(self.name.as_str()))
            .await
        {
            Ok(live) => live,
            Err(Error::Elasticsearch(e)) if e.error_type() == Some("index_not_found_exception") => {
                let mut body =
                    create::RequestBody::new().mappings(TypeMapping::new().properties(properties));
                if has_vectors(&expected) {
                    let mut settings = IndexSettings::new();
                    settings.other_settings.insert(
                        "index.mapping.exclude_source_vectors".to_owned(),
                        Value::Bool(false),
                    );
                    body = body.settings(settings);
                }
                self.client
                    .send(&create::Request::new(self.name.as_str()).body(body))
                    .await?;
                return Ok(MappingReport {
                    created: true,
                    ..MappingReport::default()
                });
            }
            Err(e) => return Err(e),
        };
        let mut report = MappingReport::default();
        // The mapping is keyed by the index, which an alias names otherwise:
        // the first index's is compared, and every index an alias names gets
        // the missing fields at once.
        let live_properties = live
            .0
            .values()
            .next()
            .and_then(|record| record.mappings.as_ref().or(record.item.as_ref()))
            .and_then(|mapping| mapping.properties.as_ref());
        let live_properties = match live_properties {
            Some(properties) => serde_json::to_value(properties).map_err(Error::Encode)?,
            None => Value::Object(Map::new()),
        };
        let live_properties = live_properties.as_object().cloned().unwrap_or_default();
        let missing = compare("", &expected, &live_properties, &mut report);
        if !missing.is_empty() {
            let missing: IndexMap<String, Property> =
                serde_json::from_value(Value::Object(missing)).map_err(Error::Encode)?;
            self.client
                .send(
                    &put_mapping::Request::new(self.name.as_str())
                        .body(put_mapping::RequestBody::new().properties(missing)),
                )
                .await?;
        }
        Ok(report)
    }

    /// Deletes the index, and every document in it.
    pub async fn delete_index(&self) -> Result<(), Error> {
        self.client
            .send(&indices_delete::Request::new(self.name.as_str()))
            .await?;
        Ok(())
    }

    /// Writes a document, with the id from its `#[id]` field, replacing any
    /// with the same id.
    pub async fn put(&self, document: &T) -> Result<Written, Error> {
        let mut request = index::Request::new(self.name.as_str()).body(document);
        if let Some(id) = document.id() {
            request = request.id(id);
        }
        self.write(request, document).await
    }

    /// Writes a document back if it hasn't changed since it was read, and
    /// fails with [`Error::Conflict`] if it has.
    pub async fn put_versioned(&self, document: &Versioned<T>) -> Result<Written, Error> {
        let request = index::Request::new(self.name.as_str())
            .id(document.id.as_str())
            .if_seq_no(document.version.seq_no)
            .if_primary_term(document.version.primary_term)
            .body(&document.document);
        self.write(request, &document.document).await
    }

    async fn write(&self, mut request: index::Request<&T>, document: &T) -> Result<Written, Error> {
        if let Some(routing) = document.routing() {
            request = request.routing(routing);
        }
        if let Some(refresh) = &self.refresh {
            request = request.refresh(refresh.clone());
        }
        let response = self.client.send(&request).await?;
        let response = response.0;
        Ok(Written {
            id: response.id.unwrap_or_default(),
            version: Version {
                seq_no: response.seq_no.unwrap_or_default(),
                primary_term: response.primary_term.unwrap_or_default(),
            },
            created: matches!(response.result, Some(oxsearch_core::Result::Created)),
        })
    }

    /// A document by id, or `None` if there's none.
    pub async fn get(&self, id: &str) -> Result<Option<Versioned<T>>, Error> {
        let request = get::Request::new(id, self.name.as_str());
        match self
            .client
            .send_as::<_, get::Response<Value>>(&request)
            .await
        {
            Ok(response) => versioned(response.0),
            Err(Error::Elasticsearch(e))
                if e.status == 404 && e.body.get("found") == Some(&Value::Bool(false)) =>
            {
                Ok(None)
            }
            Err(e) => Err(e),
        }
    }

    /// Documents by id, in the same order, with `None` for each that
    /// doesn't exist.
    pub async fn get_many<I: Into<String>>(
        &self,
        ids: impl IntoIterator<Item = I>,
    ) -> Result<Vec<Option<Versioned<T>>>, Error> {
        let ids: Vec<String> = ids.into_iter().map(Into::into).collect();
        let request = mget::Request::new()
            .index(self.name.as_str())
            .body(mget::RequestBody::new().ids(ids));
        let response: mget::Response<Value> = self.client.send_as(&request).await?;
        response
            .docs
            .into_iter()
            .map(|item| match item {
                mget::ResponseItem::Result(result) => versioned(result),
                mget::ResponseItem::Failure(failure) => {
                    let body = json!({ "error": failure.error });
                    Err(Error::from_response(500, body.to_string().as_bytes()))
                }
                // Responses gain no other kind of item.
                _ => Ok(None),
            })
            .collect()
    }

    /// Deletes a document by id; `false` if there was none.
    pub async fn delete(&self, id: &str) -> Result<bool, Error> {
        let mut request = delete::Request::new(id, self.name.as_str());
        if let Some(refresh) = &self.refresh {
            request = request.refresh(refresh.clone());
        }
        match self.client.send(&request).await {
            Ok(_) => Ok(true),
            Err(Error::Elasticsearch(e))
                if e.status == 404 && e.body.get("result") == Some(&json!("not_found")) =>
            {
                Ok(false)
            }
            Err(e) => Err(e),
        }
    }

    /// The best ten documents a retriever finds. A query is a retriever
    /// too: `Retriever::standard(Article::TITLE.r#match("hnsw"))`.
    pub async fn search(
        &self,
        retriever: impl Into<RetrieverContainer>,
    ) -> Result<Vec<Hit<T>>, Error> {
        let body = search::RequestBody::new().retriever(retriever);
        Ok(self.search_request(body).await?.into_results())
    }

    /// Any search of the index, with documents read as `T`.
    pub async fn search_request(
        &self,
        body: search::RequestBody,
    ) -> Result<search::Response<T>, Error> {
        let request = search::Request::new().index(self.name.as_str()).body(body);
        self.client.send_as(&request).await
    }

    /// Fuses retrievers' results, on the server if its license allows,
    /// else on the client.
    pub async fn fuse(&self, fusion: &Fusion) -> Result<Fused<T>, Error> {
        fusion.search(&self.client, self.name.as_str()).await
    }
}

#[cfg(feature = "esql")]
impl<T: Document, Tr: Transport> Index<T, Tr> {
    /// An ES|QL query `FROM` the index, to build on and run with
    /// [`Esql::fetch`](oxsearch_esql::Esql::fetch), which reads the rows as
    /// the documents, or as any other type, by column name:
    ///
    /// ```ignore
    /// let popular: Vec<Note> = notes
    ///     .esql()
    ///     .where_(col("views").gte(100))
    ///     .sort([col("views").desc()])
    ///     .limit(10)
    ///     .fetch(notes.client())
    ///     .await?;
    /// ```
    pub fn esql(&self) -> oxsearch_esql::Esql {
        oxsearch_esql::Esql::from_index(&self.name)
    }
}

/// A document read with its version, or `None` if it wasn't found.
fn versioned<T: Document>(result: get::GetResult<Value>) -> Result<Option<Versioned<T>>, Error> {
    if result.found == Some(false) {
        return Ok(None);
    }
    let source = result.source.unwrap_or(Value::Null);
    let document = serde_json::from_value(source.clone()).map_err(|e| {
        let body = source.to_string();
        Error::Decode {
            status: 200,
            source: e,
            body: body.chars().take(512).collect(),
        }
    })?;
    Ok(Some(Versioned {
        id: result.id.unwrap_or_default(),
        version: Version {
            seq_no: result.seq_no.unwrap_or_default(),
            primary_term: result.primary_term.unwrap_or_default(),
        },
        document,
    }))
}

fn has_vectors(properties: &Map<String, Value>) -> bool {
    properties.values().any(|property| {
        property
            .get("type")
            .and_then(Value::as_str)
            .is_some_and(|kind| VECTORS.contains(&kind))
            || property
                .get("properties")
                .and_then(Value::as_object)
                .is_some_and(has_vectors)
    })
}

/// A property's type: an object's may be left out.
fn kind(property: &Value) -> &str {
    property
        .get("type")
        .and_then(Value::as_str)
        .unwrap_or("object")
}

/// Compares the document type's properties with the live mapping's at one
/// level, recording drift and the fields the live mapping lacks, and
/// returns the properties to add there.
fn compare(
    path: &str,
    expected: &Map<String, Value>,
    live: &Map<String, Value>,
    report: &mut MappingReport,
) -> Map<String, Value> {
    let mut add = Map::new();
    for (name, wanted) in expected {
        let field = if path.is_empty() {
            name.clone()
        } else {
            format!("{path}.{name}")
        };
        let Some(actual) = live.get(name) else {
            report.added.push(field);
            add.insert(name.clone(), wanted.clone());
            continue;
        };
        let differs = kind(wanted) != kind(actual)
            || IDENTITY
                .iter()
                .any(|key| wanted.get(key).is_some_and(|v| actual.get(key) != Some(v)));
        if differs {
            report.drift.push(Drift {
                field,
                expected: wanted.clone(),
                actual: actual.clone(),
            });
            continue;
        }
        if let Some(fields) = wanted.get("properties").and_then(Value::as_object) {
            let empty = Map::new();
            let live_fields = actual
                .get("properties")
                .and_then(Value::as_object)
                .unwrap_or(&empty);
            let missing = compare(&field, fields, live_fields, report);
            if !missing.is_empty() {
                add.insert(name.clone(), json!({ "properties": missing }));
            }
        }
    }
    add
}

#[cfg(test)]
mod tests {
    use super::*;

    fn map(value: Value) -> Map<String, Value> {
        value.as_object().cloned().unwrap()
    }

    #[test]
    fn drift_and_missing_fields() {
        let expected = map(json!({
            "title": {"type": "text"},
            "tags": {"type": "keyword"},
            "embedding": {"type": "dense_vector", "dims": 3, "similarity": "cosine"},
            "author": {"type": "object", "properties": {
                "name": {"type": "keyword"},
                "email": {"type": "keyword"}
            }}
        }));
        let live = map(json!({
            "title": {"type": "text"},
            "tags": {"type": "text"},
            "embedding": {"type": "dense_vector", "dims": 4, "similarity": "cosine", "index": true},
            "author": {"properties": {"name": {"type": "keyword"}}},
            "extra": {"type": "long"}
        }));
        let mut report = MappingReport::default();
        let add = compare("", &expected, &live, &mut report);
        assert_eq!(report.added, ["author.email"]);
        // In the expected mapping's order, which serde_json's
        // `preserve_order` feature changes: sorted to compare.
        let mut drifted: Vec<&str> = report.drift.iter().map(|d| d.field.as_str()).collect();
        drifted.sort();
        assert_eq!(drifted, ["embedding", "tags"]);
        assert!(!report.is_current());
        assert_eq!(
            Value::Object(add),
            json!({"author": {"properties": {"email": {"type": "keyword"}}}})
        );
        assert!(has_vectors(&expected));
        assert!(!has_vectors(&map(json!({"author": expected["author"]}))));
    }
}