rust-scout 0.7.0

Scout-style full-text search engine abstraction for Rust
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#![cfg(feature = "meilisearch")]

use serde_json::{Map, Value};

use crate::config::percent_encode;
use crate::engine::{Engine, EngineFuture};
use crate::{SearchBuilder, SearchDocument, SearchHit, SearchResult, TrashedFilter};

/// 分页模式。**两种模式的总数语义不同**,这是选它的唯一理由:
///
/// - `Page`:`page`/`hitsPerPage`。Meilisearch 只在这个模式下返回**穷尽**的
///   `totalHits`,与 Collection/ES/Typesense/Algolia 的 `total` 一致。
/// - `Offset`:`offset`/`limit`。能表达任意起点(页对齐表达不了 `skip % take`
///   的余数),但 Meilisearch 此时只回 `estimatedTotalHits` —— 官方文档明确说它
///   不适合算精确页数,且受索引 `maxTotalHits`(默认 1000)封顶。
///
/// 所以:窗口页对齐时走 `Page`(`paginate` 永远对齐,普通 `search` 在
/// `skip` 是 `take` 整数倍时也对齐),只有真正页对不齐时才退回 `Offset`。
/// 两者互斥,Meilisearch 拒绝同时出现。
enum PageMode {
    Page(usize, usize),
    Offset(usize, usize),
}

/// Meilisearch 引擎。文档主键固定为 `id` 字段(add-or-replace 语义);
/// 软删除标记为 `__soft_deleted` 布尔字段。
pub struct MeilisearchEngine {
    host: String,
    api_key: Option<String>,
    client: reqwest::Client,
}

impl MeilisearchEngine {
    pub fn new(host: String, api_key: Option<String>) -> Self {
        Self {
            host: host.trim_end_matches('/').to_string(),
            api_key,
            client: reqwest::Client::new(),
        }
    }

    /// 发送请求,返回状态码 + body 文本;网络错误经 `?` 转 `ScoutError::Http`。
    async fn raw(
        &self,
        method: reqwest::Method,
        path: &str,
        body: Option<String>,
        content_type: Option<&str>,
    ) -> crate::Result<(reqwest::StatusCode, String)> {
        let mut request = self
            .client
            .request(method.clone(), format!("{}{}", self.host, path));
        if let Some(api_key) = &self.api_key {
            request = request.header("Authorization", format!("Bearer {}", api_key));
        }
        if let Some(body) = body {
            request = request.body(body);
            if let Some(content_type) = content_type {
                request = request.header(reqwest::header::CONTENT_TYPE, content_type);
            }
        }
        let response = request.send().await?;
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        Ok((status, body))
    }

    /// JSON 请求;非 2xx 返回 `ScoutError::Backend("METHOD path -> status: body")`。
    async fn request(
        &self,
        method: reqwest::Method,
        path: &str,
        body: Option<Value>,
    ) -> crate::Result<Value> {
        let (status, body) = self
            .raw(
                method.clone(),
                path,
                body.map(|b| b.to_string()),
                Some("application/json"),
            )
            .await?;
        if !status.is_success() {
            return Err(crate::ScoutError::Backend(format!(
                "{} {} -> {}: {}",
                method, path, status, body
            )));
        }
        Ok(serde_json::from_str(&body)?)
    }

    /// filter 值:字符串加双引号(转义 `\` 与 `"`),数字/bool 裸值。
    fn filter_value(v: &Value) -> String {
        match v {
            Value::String(s) => format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")),
            Value::Bool(b) => b.to_string(),
            Value::Number(n) => n.to_string(),
            other => other.to_string(),
        }
    }

    /// 等值/IN/软删除 → Meilisearch filter 表达式;无任何条件时返回 None。
    fn build_filter(builder: &SearchBuilder) -> Option<String> {
        let mut parts: Vec<String> = Vec::new();
        for w in &builder.wheres {
            parts.push(format!("{}={}", w.field, Self::filter_value(&w.value)));
        }
        for (field, values) in &builder.where_ins {
            let list = values.iter().map(Self::filter_value).collect::<Vec<_>>().join(", ");
            parts.push(format!("{} IN [{}]", field, list));
        }
        for (field, values) in &builder.where_not_ins {
            if values.is_empty() {
                continue; // 空 NOT IN 集合 = 无过滤(Collection 语义,与 Algolia 一致)
            }
            let list = values.iter().map(Self::filter_value).collect::<Vec<_>>().join(", ");
            parts.push(format!("{} NOT IN [{}]", field, list));
        }
        // `=`/`IS NULL` 只匹配「存在且相等」/「显式 null」,未软删文档从未写入
        // 该字段,会被全部隐藏;`NOT __soft_deleted = true` 对缺失/false/null
        // 均匹配,与 Collection 的 as_bool().unwrap_or(false) 语义对齐。
        match builder.trashed {
            TrashedFilter::Exclude => parts.push("NOT __soft_deleted = true".to_string()),
            TrashedFilter::OnlyTrashed => parts.push("__soft_deleted=true".to_string()),
            TrashedFilter::WithTrashed => {}
        }
        if parts.is_empty() {
            None
        } else {
            Some(parts.join(" AND "))
        }
    }

    fn sort_array(builder: &SearchBuilder) -> Value {
        let parts: Vec<String> = builder
            .orders
            .iter()
            .map(|o| format!("{}:{}", o.field, if o.desc { "desc" } else { "asc" }))
            .collect();
        Value::Array(parts.into_iter().map(Value::String).collect())
    }

    fn page_mode(builder: &SearchBuilder) -> PageMode {
        let take = builder.take.unwrap_or(10).max(1);
        let skip = builder.skip.unwrap_or(0);
        if skip.is_multiple_of(take) {
            PageMode::Page(skip / take + 1, take)
        } else {
            PageMode::Offset(skip, take)
        }
    }

    /// `page`/`per_page` → 页模式;page N 与 `(N-1)*per_page` 的偏移等价。
    fn page_mode_of(page: usize, per_page: usize) -> PageMode {
        PageMode::Page(page.max(1), per_page.max(1))
    }

    /// 请求体。按 [`PageMode`] 二选一,理由见那里的注释。
    fn search_body(builder: &SearchBuilder, mode: &PageMode) -> Value {
        let mut body = Map::new();
        body.insert("q".into(), Value::String(builder.query.clone()));
        if let Some(filter) = Self::build_filter(builder) {
            body.insert("filter".into(), Value::String(filter));
        }
        match *mode {
            PageMode::Page(page, per_page) => {
                body.insert("page".into(), Value::from(page));
                body.insert("hitsPerPage".into(), Value::from(per_page));
            }
            PageMode::Offset(offset, limit) => {
                body.insert("offset".into(), Value::from(offset));
                body.insert("limit".into(), Value::from(limit));
            }
        }
        if !builder.orders.is_empty() {
            body.insert("sort".into(), Self::sort_array(builder));
        }
        Value::Object(body)
    }

    /// 文档 → Meilisearch 文档对象;`id` 键统一注入/覆盖为 doc.id。
    fn doc_to_document(doc: &SearchDocument) -> Value {
        let mut fields = doc.fields.clone();
        fields.insert("id".into(), Value::String(doc.id.clone()));
        Value::Object(fields)
    }

    /// 响应解析:id 取 `hits[i].id`,source 取整个 hit 对象,total 取 `totalHits`。
    ///
    /// 用 `offset`/`limit` 搜索时 Meilisearch 只回 `estimatedTotalHits`(穷尽计数
    /// `totalHits` 需要 `page`/`hitsPerPage`,见 paginate 分支),故回退读它;
    /// 两者都缺失才回退 hits.len(),避免 total 退化成页大小。
    fn parse_search_response(raw: &Value) -> SearchResult {
        let hits = raw.get("hits").and_then(Value::as_array);
        let total = ["totalHits", "estimatedTotalHits"]
            .iter()
            .find_map(|key| raw.get(*key).and_then(Value::as_u64))
            .map(|n| n as usize)
            .unwrap_or_else(|| hits.map_or(0, Vec::len));
        let hits = hits
            .map(|arr| arr.iter().filter_map(Self::hit_from_response).collect())
            .unwrap_or_default();
        SearchResult {
            hits,
            total,
            ..Default::default()
        }
    }

    fn hit_from_response(hit: &Value) -> Option<SearchHit> {
        let id = hit.get("id")?;
        let id = id.as_str().map(str::to_string).unwrap_or_else(|| id.to_string());
        Some(SearchHit {
            id,
            // `_rankingScore` 需在索引设置中启用;缺失则无分。`_matchesPosition`
            // 是位置信息而非分数,忽略。
            score: hit.get("_rankingScore").and_then(Value::as_f64),
            source: hit.clone(),
            highlight: None,
        })
    }
}

impl Engine for MeilisearchEngine {
    fn update<'a>(&'a self, docs: &'a [SearchDocument]) -> EngineFuture<'a, ()> {
        // 与 update_bulk 相同:按索引分组一次 POST documents。
        self.update_bulk(docs)
    }

    fn delete<'a>(&'a self, ids: &'a [String]) -> EngineFuture<'a, ()> {
        // 无索引信息:仅作用于 default 索引(同 ElasticsearchEngine 语义)。
        self.delete_in("default", ids)
    }

    fn delete_in<'a>(&'a self, index: &'a str, ids: &'a [String]) -> EngineFuture<'a, ()> {
        Box::pin(async move {
            if ids.is_empty() {
                return Ok(());
            }
            crate::validate_index_name(index)?;
            let ids: Vec<Value> = ids.iter().map(|id| Value::String(id.clone())).collect();
            let path = format!("/indexes/{}/documents/delete-batch", percent_encode(index));
            let _ = self
                .request(reqwest::Method::POST, &path, Some(Value::Array(ids)))
                .await?;
            Ok(())
        })
    }

    fn search<'a>(&'a self, builder: &'a SearchBuilder) -> EngineFuture<'a, SearchResult> {
        // 索引名校验必须在短路之前:否则 within("_all") 配一个空 where_in 会返回
        // Ok(空结果),而其它驱动返回 InvalidIndexName —— 边界行为必须一致。
        let index = builder.index.as_deref().unwrap_or("default");
        if let Err(e) = crate::validate_index_name(index) {
            return Box::pin(async move { Err(e) });
        }
        // 空 where_in 集合 = 不匹配任何(Collection 语义):短路空结果。
        if builder.where_ins.iter().any(|(_, v)| v.is_empty()) {
            return Box::pin(async move { Ok(SearchResult::default()) });
        }
        // take(0):Collection/ES 的 total 是「取之前」的全量匹配数,take(0) 于是
        // 返回「命中总数 + 空 hits」。total 只能从后端拿,所以请求照发,但只取 1 条
        // (limit=0 在 Meilisearch 上语义不明),拿到 total 后把 hits 清空。
        let mode = Self::page_mode(builder);
        let want_none = builder.take == Some(0);
        Box::pin(async move {
            let body = Self::search_body(builder, &mode);
            let path = format!("/indexes/{}/search", percent_encode(index));
            let raw = self.request(reqwest::Method::POST, &path, Some(body)).await?;
            let result = Self::parse_search_response(&raw);
            Ok(if want_none { result.without_hits() } else { result })
        })
    }

    fn paginate<'a>(
        &'a self,
        builder: &'a SearchBuilder,
        page: usize,
        per_page: usize,
    ) -> EngineFuture<'a, SearchResult> {
        // 空 where_in 集合 = 不匹配任何(Collection 语义):短路空结果。
        if builder.where_ins.iter().any(|(_, v)| v.is_empty()) {
            return Box::pin(async move { Ok(SearchResult::default()) });
        }
        // 页语义在本地折算成 offset:page N 与 Collection 的 (N-1)*per_page 对齐,
        // 所以 paginate 的结果与改 offset 之前逐条相同。
        // paginate 天然页对齐:走 Page 模式,total 是穷尽的 totalHits
        let mode = Self::page_mode_of(page, per_page);
        Box::pin(async move {
            let index = builder.index.as_deref().unwrap_or("default");
            crate::validate_index_name(index)?;
            let body = Self::search_body(builder, &mode);
            let path = format!("/indexes/{}/search", percent_encode(index));
            let raw = self.request(reqwest::Method::POST, &path, Some(body)).await?;
            Ok(Self::parse_search_response(&raw))
        })
    }

    fn create_index<'a>(
        &'a self,
        index: &'a str,
        settings: serde_json::Value,
    ) -> EngineFuture<'a, ()> {
        Box::pin(async move {
            crate::validate_index_name(index)?;
            let body = serde_json::json!({"uid": index, "primaryKey": "id"});
            let path = "/indexes";
            let (status, text) = self
                .raw(
                    reqwest::Method::POST,
                    path,
                    Some(body.to_string()),
                    Some("application/json"),
                )
                .await?;
            if status == reqwest::StatusCode::CONFLICT {
                return Ok(()); // 已存在
            }
            if !status.is_success() {
                return Err(crate::ScoutError::Backend(format!(
                    "{} {} -> {}: {}",
                    reqwest::Method::POST, path, status, text
                )));
            }
            // 过滤/排序/软删请求要求字段已配置 filterable/sortable,否则一律
            // 400(attribute not filterable)。settings 可提供这两个数组,
            // 强制并入 id 与 __soft_deleted(软删内部过滤依赖)。
            let mut filterable: Vec<&str> = vec!["id", "__soft_deleted"];
            let mut sortable: Vec<&str> = Vec::new();
            for (key, list) in [("filterableAttributes", &mut filterable), ("sortableAttributes", &mut sortable)] {
                if let Some(values) = settings.get(key).and_then(Value::as_array) {
                    for v in values {
                        if let Some(s) = v.as_str() {
                            if !list.contains(&s) {
                                list.push(s);
                            }
                        }
                    }
                }
            }
            let settings_body = serde_json::json!({
                "filterableAttributes": filterable,
                "sortableAttributes": sortable,
            });
            let _ = self
                .request(
                    reqwest::Method::PATCH,
                    &format!("/indexes/{}/settings", percent_encode(index)),
                    Some(settings_body),
                )
                .await?;
            Ok(())
        })
    }

    fn delete_index<'a>(&'a self, index: &'a str) -> EngineFuture<'a, ()> {
        Box::pin(async move {
            crate::validate_index_name(index)?;
            let path = format!("/indexes/{}", percent_encode(index));
            let (status, text) = self.raw(reqwest::Method::DELETE, &path, None, None).await?;
            if status == reqwest::StatusCode::NOT_FOUND {
                return Ok(()); // 不存在视为成功
            }
            if !status.is_success() {
                return Err(crate::ScoutError::Backend(format!(
                    "{} {} -> {}: {}",
                    reqwest::Method::DELETE, path, status, text
                )));
            }
            Ok(())
        })
    }

    fn update_bulk<'a>(&'a self, docs: &'a [SearchDocument]) -> EngineFuture<'a, ()> {
        Box::pin(async move {
            let mut groups: std::collections::HashMap<&str, Vec<Value>> = Default::default();
            for doc in docs {
                groups
                    .entry(doc.index.as_deref().unwrap_or("default"))
                    .or_default()
                    .push(Self::doc_to_document(doc));
            }
            for (index, docs) in groups {
                crate::validate_index_name(index)?;
                let path = format!("/indexes/{}/documents?primaryKey=id", percent_encode(index));
                let _ = self
                    .request(reqwest::Method::POST, &path, Some(Value::Array(docs)))
                    .await?;
            }
            Ok(())
        })
    }

    fn delete_bulk<'a>(&'a self, index: &'a str, ids: &'a [String]) -> EngineFuture<'a, ()> {
        // 与 delete_in 相同的 delete-batch 端点。
        self.delete_in(index, ids)
    }

    /// 仅作用于 `index`。Meilisearch 的 add-or-replace 是整体替换,无法只更新
    /// 单字段:先按 id 搜出原文档再整体写回打标版本(搜不到则跳过)。
    ///
    /// 不带索引的 [`Engine::soft_delete`] 在本驱动上不可用(见 `engine.rs` 说明):
    /// 原先它硬编码 `default`,对写在其它索引里的文档会静默跳过却返回 Ok。
    fn soft_delete_in<'a>(&'a self, index: &'a str, ids: &'a [String]) -> EngineFuture<'a, ()> {
        Box::pin(async move {
            crate::validate_index_name(index)?;
            for id in ids {
                let builder =
                    SearchBuilder::new("").within(index).where_field("id", id.clone());
                let result = self.search(&builder).await?;
                let Some(hit) = result.hits.first() else {
                    continue;
                };
                let mut fields: Map<String, Value> = match &hit.source {
                    Value::Object(map) => map
                        .iter()
                        .filter(|(k, _)| !k.starts_with('_')) // 丢弃 _rankingScore 等元数据
                        .map(|(k, v)| (k.clone(), v.clone()))
                        .collect(),
                    _ => Map::new(),
                };
                fields.insert("__soft_deleted".into(), Value::Bool(true));
                let doc = SearchDocument {
                    id: id.clone(),
                    index: None,
                    fields,
                };
                self.update(std::slice::from_ref(&doc)).await?;
            }
            Ok(())
        })
    }
    // reindex:trait 默认 Unsupported(Meilisearch 无原生端点)。
}

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

    #[test]
    fn filter_equality_and_in_clauses() {
        let builder = SearchBuilder::new("")
            .where_field("title", "a\"b\\c")
            .where_field("count", 5)
            .where_field("active", true)
            .where_in("tag", ["x", "y"])
            .where_not_in("tag", ["z"]);
        let filter = MeilisearchEngine::build_filter(&builder).unwrap();
        assert_eq!(
            filter,
            r#"title="a\"b\\c" AND count=5 AND active=true AND tag IN ["x", "y"] AND tag NOT IN ["z"] AND NOT __soft_deleted = true"#
        );
    }

    #[test]
    fn filter_trashed_variants() {
        assert_eq!(
            MeilisearchEngine::build_filter(&SearchBuilder::new("")).unwrap(),
            "NOT __soft_deleted = true"
        );
        assert_eq!(
            MeilisearchEngine::build_filter(&SearchBuilder::new("").only_trashed()).unwrap(),
            "__soft_deleted=true"
        );
        assert_eq!(
            MeilisearchEngine::build_filter(&SearchBuilder::new("").with_trashed()),
            None
        );
    }




    #[test]
    fn page_mode_prefers_page_when_offset_is_page_aligned() {
        // 关键回归:Meilisearch 只在 page/hitsPerPage 模式下返回**穷尽**的 totalHits;
        // offset/limit 模式只给 estimatedTotalHits(受 maxTotalHits 封顶,默认 1000)。
        // 全用 offset 会让 total 变成估算值,与其余七个驱动不一致。
        let aligned = SearchBuilder::new("q").take(10).skip(20);
        assert!(
            matches!(
                MeilisearchEngine::page_mode(&aligned),
                PageMode::Page(3, 10)
            ),
            "skip 是 take 的整数倍时必须走页模式(穷尽计数)"
        );
        // 页对不齐才退回 offset —— 页模式会丢掉 15 % 10 的余数
        let unaligned = SearchBuilder::new("q").take(10).skip(15);
        assert!(matches!(
            MeilisearchEngine::page_mode(&unaligned),
            PageMode::Offset(15, 10)
        ));
        // 无 skip 时页对齐(offset 0)
        assert!(matches!(
            MeilisearchEngine::page_mode(&SearchBuilder::new("q")),
            PageMode::Page(1, 10)
        ));
    }

    #[test]
    fn search_body_emits_exactly_one_paging_mode() {
        // 两种模式互斥:Meilisearch 拒绝 offset 与 page 同时出现
        let b = SearchBuilder::new("q");
        let paged = MeilisearchEngine::search_body(&b, &PageMode::Page(2, 10));
        assert!(paged.get("page").is_some() && paged.get("offset").is_none());
        let offset = MeilisearchEngine::search_body(&b, &PageMode::Offset(15, 10));
        assert!(offset.get("offset").is_some() && offset.get("page").is_none());
    }

    #[test]
    fn search_body_includes_filter_when_trashed_excludes() {
        let builder = SearchBuilder::new("x").where_field("cat", 1);
        let body = MeilisearchEngine::search_body(&builder, &MeilisearchEngine::page_mode_of(1, 10));
        assert_eq!(
            body["filter"],
            "cat=1 AND NOT __soft_deleted = true"
        );
    }

    #[test]
    fn parse_response_reads_estimated_total_hits() {
        // 回归:offset/limit 搜索只回 estimatedTotalHits,读到它之前 total 会
        // 退化成 hits.len()(页大小),分页 UI 的「共 N 条」就错了。
        let raw = serde_json::json!({"hits": [{"id": "1"}], "estimatedTotalHits": 471});
        let result = MeilisearchEngine::parse_search_response(&raw);
        assert_eq!(result.total, 471);
        assert_eq!(result.hits.len(), 1);
        // page/hitsPerPage 模式仍回 totalHits(穷尽计数),优先读它。
        let page_mode = serde_json::json!({
            "hits": [{"id": "1"}],
            "totalHits": 42,
            "estimatedTotalHits": 471
        });
        assert_eq!(MeilisearchEngine::parse_search_response(&page_mode).total, 42);
    }

    #[test]
    fn parse_response_extracts_hits_total_score() {
        let raw = serde_json::json!({
            "hits": [
                {"id": "1", "title": "a", "_rankingScore": 0.9},
                {"id": 2, "title": "b"}
            ],
            "totalHits": 42
        });
        let result = MeilisearchEngine::parse_search_response(&raw);
        assert_eq!(result.total, 42);
        assert_eq!(result.hits.len(), 2);
        assert_eq!(result.hits[0].id, "1");
        assert_eq!(result.hits[0].score, Some(0.9));
        assert_eq!(result.hits[0].source["title"], "a");
        assert_eq!(result.hits[1].id, "2"); // 数字 id 也转字符串
        assert_eq!(result.hits[1].score, None);
    }

    #[test]
    fn parse_response_total_falls_back_to_hits_len() {
        let raw = serde_json::json!({"hits": [{"id": "1"}]});
        assert_eq!(MeilisearchEngine::parse_search_response(&raw).total, 1);
        let empty = MeilisearchEngine::parse_search_response(&serde_json::json!({}));
        assert_eq!(empty.total, 0);
        assert!(empty.hits.is_empty());
    }

    #[tokio::test]
    async fn flush_is_a_noop_and_makes_no_request() {
        // 指向必然连不上的地址:真的打网络就会失败,返回 Ok 即证明没有请求。
        // flush 的契约是「刷新可见性」,绝不能是清空索引——README 的生命周期示例
        // 在 update 与 search 之间调用它,而这里曾打 delete-all 把索引删空。
        let engine = MeilisearchEngine::new("http://127.0.0.1:1".to_string(), None);
        engine.flush("books").await.unwrap();
    }

    #[tokio::test]
    async fn index_less_soft_delete_is_refused_not_silently_skipped() {
        // 曾经它硬编码 default:对写在别的索引里的文档静默跳过却返回 Ok。
        let engine = MeilisearchEngine::new("http://127.0.0.1:1".to_string(), None);
        let err = engine
            .soft_delete(&["b1".to_string()])
            .await
            .expect_err("index-less soft_delete 必须报错,而不是静默 no-op");
        assert!(matches!(err, crate::ScoutError::Unsupported(_)), "got {err:?}");
    }


    #[tokio::test]
    async fn empty_where_in_short_circuits_search_and_paginate() {
        // 回归:空 where_in 集合 = 不匹配任何(Collection 语义),曾被拼成
        // `tag IN []` 丢给后端(行为由后端决定:报错或匹配全部)。
        let engine = MeilisearchEngine::new("http://127.0.0.1:1".to_string(), None);
        let builder = SearchBuilder::new("")
            .within("books")
            .where_in("tag", Vec::<&str>::new());
        let result = engine.search(&builder).await.unwrap();
        assert!(result.hits.is_empty());
        assert_eq!(result.total, 0);
        // paginate 走同一条短路,不能只在 search 上修。
        let paged = engine.paginate(&builder, 2, 10).await.unwrap();
        assert!(paged.hits.is_empty());
        assert_eq!(paged.total, 0);
    }

    #[tokio::test]
    async fn reserved_index_name_is_rejected_even_with_empty_where_in() {
        // 回归:空 where_in 的短路原先排在 validate_index_name 之前,
        // within("_all") 会因此返回 Ok(空结果) 而不是 InvalidIndexName。
        let engine = MeilisearchEngine::new("http://127.0.0.1:1".to_string(), None);
        let err = engine
            .search(&SearchBuilder::new("q").within("_all").where_in("t", Vec::<&str>::new()))
            .await
            .unwrap_err();
        assert!(matches!(err, crate::ScoutError::InvalidIndexName(_)), "got {err:?}");
    }

}