Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

6.0 KB
Raw Blame
//! Issue #423 (symptom #406): `fuzzy` on a `keyword` field must be
//! case-SENSITIVE by default (ES: `case_insensitive` defaults to false) — the
//! value is matched against the raw term dictionary, so `fuzzy{value:"hello",
//! fuzziness:0}` on `"Hello"` must NOT match. XERJ folds case on both the
//! memtable and segment paths (the FST fuzzy route hardcodes
//! `case_insensitive: true`, index.rs:~41133), so it matches (wrong result).
//!
//! CONSTRAINT (like keyword wildcard, #668): a `fuzzy{case_insensitive:true}`
//! must still fold. `QueryNode::Fuzzy` carries no `case_insensitive` flag today,
//! so the fix threads one through AST + parser + the memtable Fuzzy arm + the
//! FTS route, exactly as #668 did for `Wildcard`.
//!
//! IGNORED until that fix; un-ignored by it.

use serde_json::json;
use tempfile::TempDir;
use xerj_common::config::Config;
use xerj_common::types::{FieldConfig, FieldType, Schema};
use xerj_engine::Engine;
use xerj_query::parse_request;

async fn hits(engine: &Engine, q: serde_json::Value) -> u64 {
    let idx = engine.get_index("docs").expect("get index");
    let req = parse_request(&json!({ "query": q, "size": 0 })).expect("parse_request");
    idx.search(&req).await.expect("search").total.value
}

#[tokio::test]
async fn fuzzy_keyword_is_case_sensitive_but_ci_flag_still_folds() {
    let dir = TempDir::new().unwrap();
    let mut config = Config::default();
    config.server.data_dir = dir.path().to_str().unwrap().to_string();
    let engine = Engine::new(config).expect("engine");

    let mut schema = Schema::empty();
    schema
        .fields
        .push(FieldConfig::new("name", FieldType::Keyword));
    engine.create_index("docs", schema).expect("create");
    let idx = engine.get_index("docs").expect("get");
    idx.index_document(Some("d0".to_string()), json!({ "name": "Hello" }))
        .await
        .expect("index");

    let fz = |v: &str| json!({ "fuzzy": { "name": { "value": v, "fuzziness": 0 } } });
    let fz_ci = json!({ "fuzzy": { "name": { "value": "hello", "fuzziness": 0, "case_insensitive": true } } });

    // Buffered (memtable) answers, then flush and re-query the segment — every
    // pair must agree (flush-invariance — the #668 lesson).
    let pre_lower = hits(&engine, fz("hello")).await;
    let pre_cased = hits(&engine, fz("Hello")).await;
    let pre_ci = hits(&engine, fz_ci.clone()).await;
    idx.refresh().await.expect("refresh");
    let post_lower = hits(&engine, fz("hello")).await;
    let post_cased = hits(&engine, fz("Hello")).await;
    let post_ci = hits(&engine, fz_ci.clone()).await;

    assert_eq!(
        pre_lower, post_lower,
        "#423/#406: keyword fuzzy must be flush-invariant"
    );
    assert_eq!(
        pre_cased, post_cased,
        "#423/#406: keyword fuzzy must be flush-invariant"
    );
    assert_eq!(
        pre_ci, post_ci,
        "#423/#406: fuzzy case_insensitive must be flush-invariant"
    );

    // ES default: case-sensitive → a lower-case value does not fuzzy-match a
    // capitalized keyword at fuzziness 0; the correctly-cased one does; and the
    // explicit `case_insensitive: true` still folds.
    assert_eq!(
        post_lower, 0,
        "#423/#406: keyword fuzzy must be case-sensitive by default"
    );
    assert_eq!(post_cased, 1, "case-correct fuzzy matches");
    assert_eq!(
        post_ci, 1,
        "#423/#406: fuzzy with case_insensitive true still folds"
    );
}

#[tokio::test]
async fn fuzzy_keyword_separator_matches_whole_term_only_when_case_sensitive() {
    // #423/#406 (PR #675 correctness skeptic): a case-SENSITIVE keyword fuzzy
    // matches the whole un-analysed term ONLY — the sub-token fallback is a
    // FOLD-path leniency. A separator-containing keyword "alpha-beta" must NOT
    // fuzzy-match the sub-token query "beta" (fuzziness 0) case-sensitively,
    // and — the load-bearing part — must answer the SAME before and after a
    // flush. The DV `ScoredPlan::KeywordFuzzy` expansion used to run the
    // sub-token fallback UNCONDITIONALLY, so it matched only after flush
    // (memtable=0, segment=1). The explicit case_insensitive:true still folds,
    // where the sub-token leniency applies and is itself flush-invariant.
    let dir = TempDir::new().unwrap();
    let mut config = Config::default();
    config.server.data_dir = dir.path().to_str().unwrap().to_string();
    let engine = Engine::new(config).expect("engine");

    let mut schema = Schema::empty();
    schema
        .fields
        .push(FieldConfig::new("name", FieldType::Keyword));
    engine.create_index("docs", schema).expect("create");
    let idx = engine.get_index("docs").expect("get");
    idx.index_document(Some("d0".to_string()), json!({ "name": "alpha-beta" }))
        .await
        .expect("index");

    let fz = |v: &str| json!({ "fuzzy": { "name": { "value": v, "fuzziness": 0 } } });
    let fz_ci = json!({ "fuzzy": { "name": { "value": "beta", "fuzziness": 0, "case_insensitive": true } } });

    let pre_sub = hits(&engine, fz("beta")).await;
    let pre_whole = hits(&engine, fz("alpha-beta")).await;
    let pre_ci = hits(&engine, fz_ci.clone()).await;
    idx.refresh().await.expect("refresh");
    let post_sub = hits(&engine, fz("beta")).await;
    let post_whole = hits(&engine, fz("alpha-beta")).await;
    let post_ci = hits(&engine, fz_ci.clone()).await;

    assert_eq!(
        pre_sub, post_sub,
        "#423/#406: case-sensitive keyword sub-token fuzzy must be flush-invariant"
    );
    assert_eq!(
        pre_whole, post_whole,
        "#423/#406: case-sensitive keyword whole-term fuzzy must be flush-invariant"
    );
    assert_eq!(
        pre_ci, post_ci,
        "#423/#406: fuzzy case_insensitive must be flush-invariant"
    );

    assert_eq!(
        post_sub, 0,
        "#423/#406: case-sensitive keyword fuzzy matches the whole term only (no sub-token)"
    );
    assert_eq!(
        post_whole, 1,
        "the whole-term case-sensitive fuzzy matches exactly"
    );
    assert_eq!(
        post_ci, 1,
        "#423/#406: case_insensitive folds and keeps the sub-token leniency"
    );
}