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.

3.5 KB
Raw Blame
//! Issue #668: `wildcard` on a `keyword` field must be case-SENSITIVE (ES
//! default — the pattern is not analysed). `wildcard:{name:"hell*"}` on
//! keyword `"Hello"` must NOT match. XERJ currently folds case on both the
//! memtable and segment paths, so it matches (wrong result — flush-invariant
//! but ES-incorrect).
//!
//! CONSTRAINT (why a blanket flip is wrong): XERJ's parser rewrites
//! `term{case_insensitive:true}` INTO a `Wildcard` node and relies on the
//! matcher folding case (index.rs:41061-41065). So the fix must thread a
//! `case_insensitive` flag through `QueryNode::Wildcard`: genuine wildcard =>
//! case-sensitive; rewritten term-ci => still folds. This test pins BOTH.
//!
//! IGNORED until the flag is threaded (AST + parser + memtable arm + segment
//! FTS route index.rs:41071 + DV path index.rs:22550).

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 keyword_wildcard_is_case_sensitive_but_term_ci_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 wc = |p: &str| json!({ "wildcard": { "name": p } });
    let term_ci = json!({ "term": { "name": { "value": "hello", "case_insensitive": true } } });

    // Capture the buffered (memtable) answers, then flush and re-query the
    // segment: every pair must agree (flush-invariance — the #667 lesson).
    let pre_lower = hits(&engine, wc("hell*")).await;
    let pre_cased = hits(&engine, wc("Hell*")).await;
    let pre_ci = hits(&engine, term_ci.clone()).await;
    idx.refresh().await.expect("refresh");
    let post_lower = hits(&engine, wc("hell*")).await;
    let post_cased = hits(&engine, wc("Hell*")).await;
    let post_ci = hits(&engine, term_ci.clone()).await;

    assert_eq!(
        pre_lower, post_lower,
        "#668: keyword wildcard must be flush-invariant"
    );
    assert_eq!(
        pre_cased, post_cased,
        "#668: keyword wildcard must be flush-invariant"
    );
    assert_eq!(
        pre_ci, post_ci,
        "#668: term-ci wildcard must be flush-invariant"
    );

    // ES-correct: keyword wildcard is case-sensitive, so a lower-case pattern
    // does not match a capitalized value; a correctly-cased one does.
    assert_eq!(
        post_lower, 0,
        "#668: keyword `wildcard` must be case-sensitive"
    );
    assert_eq!(post_cased, 1, "case-correct wildcard still matches");
    // CONSTRAINT: `term{case_insensitive:true}` (rewritten to a Wildcard) must
    // KEEP folding — the fix must not break this passing path.
    assert_eq!(
        post_ci, 1,
        "#668: term with case_insensitive true must still fold"
    );
}