awsim-opensearch 0.5.0

Amazon OpenSearch (Elasticsearch-compatible) emulator for AWSim
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
use serde_json::{Value, json};

use super::index::{index_not_found, storage_error};
use crate::state::{DocVersion, IndexMeta, OpenSearchState};

/// Auto-create an empty index if it doesn't already exist. Used by
/// `index_document` and `update_document` to mirror Elasticsearch's
/// permissive write-creates-index behaviour.
fn ensure_index(state: &OpenSearchState, index_name: &str) -> Result<(), Value> {
    if state.index_exists(index_name) {
        return Ok(());
    }
    state
        .create_index_meta(
            index_name,
            IndexMeta {
                mappings: json!({}),
                settings: json!({}),
                created_at: crate::util::now_iso8601(),
                uuid: uuid::Uuid::new_v4().to_string(),
            },
        )
        .map_err(|e| storage_error(&e))
}

/// Index (PUT/POST) a document.
pub fn index_document(
    state: &OpenSearchState,
    index_name: &str,
    doc_id: Option<&str>,
    body: &Value,
) -> (u16, Value) {
    if let Err(err) = ensure_index(state, index_name) {
        return (500, err);
    }

    let id = doc_id
        .map(String::from)
        .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

    let (created, seq) = match state.put_doc(index_name, &id, body) {
        Ok(c) => c,
        Err(e) => return (500, storage_error(&e)),
    };

    let version = if created { 1 } else { seq };
    let status = if created { 201 } else { 200 };

    (
        status,
        json!({
            "_index": index_name,
            "_id": id,
            "_version": version,
            "result": if created { "created" } else { "updated" },
            "_shards": { "total": 2, "successful": 1, "failed": 0 },
            "_seq_no": seq,
            "_primary_term": 1,
        }),
    )
}

/// Get a document by ID.
pub fn get_document(state: &OpenSearchState, index_name: &str, doc_id: &str) -> (u16, Value) {
    if !state.index_exists(index_name) {
        return (404, index_not_found(index_name));
    }

    match state.get_doc(index_name, doc_id) {
        Ok(Some(doc)) => {
            let ver = state
                .get_doc_version(index_name, doc_id)
                .ok()
                .flatten()
                .unwrap_or(DocVersion {
                    version: 1,
                    seq_no: 0,
                    primary_term: 1,
                });
            (
                200,
                json!({
                    "_index": index_name,
                    "_id": doc_id,
                    "_version": ver.version,
                    "_seq_no": ver.seq_no,
                    "_primary_term": ver.primary_term,
                    "found": true,
                    "_source": doc,
                }),
            )
        }
        Ok(None) => (
            404,
            json!({
                "_index": index_name,
                "_id": doc_id,
                "found": false,
            }),
        ),
        Err(e) => (500, storage_error(&e)),
    }
}

/// Update a document by ID (partial update with `doc` field, supports `doc_as_upsert`).
pub fn update_document(
    state: &OpenSearchState,
    index_name: &str,
    doc_id: &str,
    body: &Value,
) -> (u16, Value) {
    let partial = body.get("doc").cloned().unwrap_or(json!({}));
    let doc_as_upsert = body["doc_as_upsert"].as_bool().unwrap_or(false);

    if let Err(err) = ensure_index(state, index_name) {
        return (500, err);
    }

    let existing = match state.get_doc(index_name, doc_id) {
        Ok(d) => d,
        Err(e) => return (500, storage_error(&e)),
    };

    if let Some(mut existing) = existing {
        deep_merge(&mut existing, &partial);
        let (_, seq) = match state.put_doc(index_name, doc_id, &existing) {
            Ok(s) => s,
            Err(e) => return (500, storage_error(&e)),
        };
        (
            200,
            json!({
                "_index": index_name,
                "_id": doc_id,
                "_version": seq,
                "result": "updated",
                "_shards": { "total": 2, "successful": 1, "failed": 0 },
                "_seq_no": seq,
                "_primary_term": 1,
            }),
        )
    } else if doc_as_upsert {
        let (_, seq) = match state.put_doc(index_name, doc_id, &partial) {
            Ok(s) => s,
            Err(e) => return (500, storage_error(&e)),
        };
        (
            201,
            json!({
                "_index": index_name,
                "_id": doc_id,
                "_version": seq,
                "result": "created",
                "_shards": { "total": 2, "successful": 1, "failed": 0 },
                "_seq_no": seq,
                "_primary_term": 1,
            }),
        )
    } else {
        (
            404,
            json!({
                "_index": index_name,
                "_id": doc_id,
                "found": false,
            }),
        )
    }
}

/// Recursively merge `partial` into `target`. Nested objects are
/// merged rather than replaced.
fn deep_merge(target: &mut Value, partial: &Value) {
    if let (Some(target_obj), Some(partial_obj)) = (target.as_object_mut(), partial.as_object()) {
        for (k, v) in partial_obj {
            let entry = target_obj.entry(k.clone());
            if v.is_object() {
                let existing = entry.or_insert_with(|| json!({}));
                deep_merge(existing, v);
            } else {
                entry.and_modify(|e| *e = v.clone()).or_insert(v.clone());
            }
        }
    }
}

/// Update documents matching a query using a simple script.
///
/// Supported script patterns:
/// - `ctx._source.remove('fieldName')` — remove a field
/// - `ctx._source.fieldName = params.value` — set a field from params
pub fn update_by_query(state: &OpenSearchState, index_name: &str, body: &Value) -> (u16, Value) {
    let query = body
        .get("query")
        .cloned()
        .unwrap_or(json!({"match_all": {}}));
    let script_source = body
        .pointer("/script/source")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();
    let params = body.pointer("/script/params").cloned().unwrap_or(json!({}));

    let resolved = state.resolve_alias(index_name);
    let mut updated: usize = 0;

    for name in &resolved {
        if !state.index_exists(name) {
            continue;
        }
        // Collect the doc snapshot first, then mutate. Holding the
        // read transaction open while issuing writes would deadlock.
        let mut matching: Vec<(String, Value)> = Vec::new();
        let _ = state.for_each_doc(name, |id, doc| {
            if super::search::match_score(&query, doc) > 0.0 {
                matching.push((id.to_string(), doc.clone()));
            }
            true
        });

        for (id, mut doc) in matching {
            apply_script(&mut doc, &script_source, &params);
            if state.put_doc(name, &id, &doc).is_ok() {
                updated += 1;
            }
        }
    }

    (
        200,
        json!({
            "updated": updated,
            "failures": [],
            "_shards": { "total": 1, "successful": 1, "failed": 0 },
        }),
    )
}

/// Apply a simple Painless-style script to a document source.
///
/// Supported patterns (one per semicolon-separated statement):
/// - `ctx._source.remove('fieldName')` / `ctx._source.remove("fieldName")`
/// - `ctx._source.fieldName = params.paramName`
/// - `ctx._source.nested.field = params.paramName` (dot-notation paths)
/// - `ctx._source.fieldName = 'literal'` / `ctx._source.fieldName = "literal"`
fn apply_script(doc: &mut Value, source: &str, params: &Value) {
    for stmt in source.split(';') {
        let stmt = stmt.trim();
        if stmt.is_empty() {
            continue;
        }

        // ctx._source.remove('field') or ctx._source.remove("field")
        if let Some(rest) = stmt.strip_prefix("ctx._source.remove(") {
            let rest = rest.trim_end_matches(')');
            let field = rest.trim().trim_matches('\'').trim_matches('"');
            remove_nested_field(doc, field);
            continue;
        }

        // ctx._source.field = ...
        if let Some(rest) = stmt.strip_prefix("ctx._source.")
            && let Some(eq_pos) = rest.find('=')
        {
            let field_path = rest[..eq_pos].trim().to_string();
            let rhs = rest[eq_pos + 1..].trim();

            // params.paramName
            if let Some(param_path) = rhs.strip_prefix("params.") {
                let param_name = param_path.trim();
                if let Some(val) = params.get(param_name) {
                    set_nested_field(doc, &field_path, val.clone());
                }
                continue;
            }

            // String literal 'value' or "value"
            if (rhs.starts_with('\'') && rhs.ends_with('\''))
                || (rhs.starts_with('"') && rhs.ends_with('"'))
            {
                let literal = &rhs[1..rhs.len() - 1];
                set_nested_field(doc, &field_path, json!(literal));
                continue;
            }

            // Numeric literal
            if let Ok(n) = rhs.parse::<i64>() {
                set_nested_field(doc, &field_path, json!(n));
                continue;
            }
            if let Ok(f) = rhs.parse::<f64>() {
                set_nested_field(doc, &field_path, json!(f));
            }
        }
    }
}

/// Set a nested field using dot-notation (e.g. `"nested.field"`).
fn set_nested_field(doc: &mut Value, path: &str, value: Value) {
    let parts: Vec<&str> = path.split('.').collect();
    if parts.is_empty() {
        return;
    }
    let mut current = doc;
    for (i, part) in parts.iter().enumerate() {
        if i == parts.len() - 1 {
            if let Some(obj) = current.as_object_mut() {
                obj.insert(part.to_string(), value.clone());
            }
        } else {
            if (current.get(*part).is_none() || !current[*part].is_object())
                && let Some(obj) = current.as_object_mut()
            {
                obj.insert(part.to_string(), json!({}));
            }
            current = current.get_mut(*part).unwrap();
        }
    }
}

/// Remove a nested field using dot-notation.
fn remove_nested_field(doc: &mut Value, path: &str) {
    let parts: Vec<&str> = path.split('.').collect();
    if parts.is_empty() {
        return;
    }
    let mut current = doc;
    for (i, part) in parts.iter().enumerate() {
        if i == parts.len() - 1 {
            if let Some(obj) = current.as_object_mut() {
                obj.remove(*part);
            }
        } else {
            if current.get(*part).is_none() {
                return;
            }
            current = current.get_mut(*part).unwrap();
        }
    }
}

/// Delete a document by ID.
pub fn delete_document(state: &OpenSearchState, index_name: &str, doc_id: &str) -> (u16, Value) {
    if !state.index_exists(index_name) {
        return (404, index_not_found(index_name));
    }

    let ver = state
        .get_doc_version(index_name, doc_id)
        .ok()
        .flatten()
        .unwrap_or(DocVersion {
            version: 1,
            seq_no: 0,
            primary_term: 1,
        });

    let found = match state.delete_doc(index_name, doc_id) {
        Ok(b) => b,
        Err(e) => return (500, storage_error(&e)),
    };

    let seq = state.global_seq_no();
    (
        if found { 200 } else { 404 },
        json!({
            "_index": index_name,
            "_id": doc_id,
            "_version": ver.version + if found { 1 } else { 0 },
            "result": if found { "deleted" } else { "not_found" },
            "_shards": { "total": 2, "successful": 1, "failed": 0 },
            "_seq_no": if found { seq } else { ver.seq_no },
            "_primary_term": 1,
        }),
    )
}

/// Multi-get: retrieve multiple documents by ID.
pub fn mget(state: &OpenSearchState, index_name: &str, body: &Value) -> (u16, Value) {
    if !state.index_exists(index_name) {
        return (404, index_not_found(index_name));
    }

    let ids: Vec<String> = body
        .get("ids")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default();

    let docs: Vec<String> = body
        .get("docs")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|d| d.get("_id").and_then(|id| id.as_str()).map(String::from))
                .collect()
        })
        .unwrap_or_default();

    let all_ids: Vec<&str> = ids
        .iter()
        .map(|s| s.as_str())
        .chain(docs.iter().map(|s| s.as_str()))
        .collect();

    let mut results: Vec<Value> = Vec::new();
    for id in &all_ids {
        match state.get_doc(index_name, id) {
            Ok(Some(doc)) => {
                let ver = state
                    .get_doc_version(index_name, id)
                    .ok()
                    .flatten()
                    .unwrap_or(DocVersion {
                        version: 1,
                        seq_no: 0,
                        primary_term: 1,
                    });
                results.push(json!({
                    "_index": index_name,
                    "_id": id,
                    "_version": ver.version,
                    "_seq_no": ver.seq_no,
                    "_primary_term": ver.primary_term,
                    "found": true,
                    "_source": doc,
                }));
            }
            _ => {
                results.push(json!({
                    "_index": index_name,
                    "_id": id,
                    "found": false,
                }));
            }
        }
    }

    (200, json!({ "docs": results }))
}

/// Delete documents matching a query.
pub fn delete_by_query(state: &OpenSearchState, index_name: &str, body: &Value) -> (u16, Value) {
    if !state.index_exists(index_name) {
        return (404, index_not_found(index_name));
    }

    let query = body
        .get("query")
        .cloned()
        .unwrap_or(json!({"match_all": {}}));

    let mut matching: Vec<String> = Vec::new();
    let _ = state.for_each_doc(index_name, |id, doc| {
        if super::search::match_score(&query, doc) > 0.0 {
            matching.push(id.to_string());
        }
        true
    });

    let deleted = matching.len();
    for id in &matching {
        let _ = state.delete_doc(index_name, id);
    }

    let failures: Vec<Value> = Vec::new();

    (
        200,
        json!({
            "took": 1,
            "timed_out": false,
            "total": deleted,
            "deleted": deleted,
            "batches": 1,
            "version_conflicts": 0,
            "noops": 0,
            "retries": { "bulk": 0, "search": 0 },
            "throttled_millis": 0,
            "requests_per_second": -1.0,
            "throttled_until_millis": 0,
            "failures": failures,
        }),
    )
}