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.

fix(es-compat): bool minimum_should_match honors negative and combination specs (#846) (#861)

xerj-org32d2fe306bb1
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 85475245f..65a9db82c 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -33931,7 +33931,15 @@ fn lower_lexically_typeless_clauses(
None => must.is_empty() && filter.is_empty(),
Some(MinShouldMatch::Fixed(0)) => false,
Some(MinShouldMatch::Fixed(_)) | Some(MinShouldMatch::Percentage(_)) => true,
- Some(MinShouldMatch::Field(_)) | Some(MinShouldMatch::Script { .. }) => {
+ // #846: negative / negative-% / combination can resolve to 0 (no
+ // requirement) depending on the should-count, so this constant
+ // fold is unsound — leave the node exactly as recursed (like the
+ // per-doc Field/Script specs), letting the evaluator resolve it.
+ Some(MinShouldMatch::Field(_))
+ | Some(MinShouldMatch::Script { .. })
+ | Some(MinShouldMatch::Negative(_))
+ | Some(MinShouldMatch::NegativePercentage(_))
+ | Some(MinShouldMatch::Combination(_)) => {
return QueryNode::Bool {
must,
should,
@@ -37794,6 +37802,11 @@ fn doc_matches_query_typed(q: &QueryNode, source: &Value, schema: &Schema) -> bo
_ => unsatisfiable,
}
}
+ // #846: negative / negative-% / combination specs resolve
+ // to a concrete required count against the should-clause
+ // count (the FTS/columnar fast paths decline these, so this
+ // stored-doc scan is the flush-invariant path for them).
+ Some(other) => other.required(should.len()).unwrap_or(unsatisfiable),
None => {
// Default: if must/filter are empty, at least 1 should must match.
if must.is_empty() && filter.is_empty() {
@@ -37956,9 +37969,10 @@ fn doc_matches_query_typed(q: &QueryNode, source: &Value, schema: &Schema) -> bo
Some(MinShouldMatch::Percentage(pct)) => {
((q_tokens.len() as f32 * (*pct as f32 / 100.0)).floor() as usize).max(1)
}
- // terms_set field/script counts aren't valid on a plain
- // match; degrade to OR rather than reject.
- Some(_) => 1,
+ // #846: negative / negative-% / combination resolve against
+ // the token count; terms_set field/script counts aren't
+ // valid on a plain match, so degrade those to OR (need 1).
+ Some(other) => other.required(q_tokens.len()).unwrap_or(1),
},
};
let count_matched = |field_tokens: &[String]| -> usize {
diff --git a/engine/crates/xerj-engine/tests/msm_negative_combination.rs b/engine/crates/xerj-engine/tests/msm_negative_combination.rs
new file mode 100644
index 000000000..0c426cbed
--- /dev/null
+++ b/engine/crates/xerj-engine/tests/msm_negative_combination.rs
@@ -0,0 +1,85 @@
+//! Issue #846: bool `minimum_should_match` must honor NEGATIVE integers
+//! (`-1` = all-but-one), negative percentages, and COMBINATION specs
+//! (`"3<90%"`). These previously failed to parse and dropped to `None`, so the
+//! bool silently behaved as a plain OR (default msm 1) — more hits than ES.
+//!
+//! Asserted flush-invariant (buffered memtable AND flushed segment) because the
+//! FTS/columnar fast paths decline these specs and route to the stored-doc
+//! scan; both must agree.
+
+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 bool_min_should_match_honors_negative_and_combination() {
+ 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");
+
+ // Single-valued fields (one per should-clause) so "how many should clauses
+ // match" is unambiguous — avoids multi-valued-array term counting.
+ let mut schema = Schema::empty();
+ for f in ["fa", "fb", "fc"] {
+ schema.fields.push(FieldConfig::new(f, FieldType::Keyword));
+ }
+ engine.create_index("docs", schema).expect("create");
+ let idx = engine.get_index("docs").expect("get");
+ // How many of the three should-clauses each doc satisfies:
+ for (id, doc) in [
+ ("d0", json!({ "fa": "y", "fb": "y", "fc": "y" })), // 3
+ ("d1", json!({ "fa": "y", "fb": "y" })), // 2
+ ("d2", json!({ "fa": "y" })), // 1
+ ("d3", json!({ "fa": "n" })), // 0
+ ] {
+ idx.index_document(Some(id.to_string()), doc)
+ .await
+ .expect("index");
+ }
+
+ // Three should clauses on distinct fields. `required` varies by the msm spec.
+ let boolq = |msm: serde_json::Value| {
+ json!({ "bool": {
+ "should": [
+ {"term": {"fa": "y"}}, {"term": {"fb": "y"}}, {"term": {"fc": "y"}}
+ ],
+ "minimum_should_match": msm
+ }})
+ };
+
+ // (spec, expected_hits, label). should_count = 3.
+ let cases = [
+ (json!(-1), 2, "negative -1: required 2 → d0,d1"),
+ (json!("-1"), 2, "string -1: required 2 → d0,d1"),
+ (json!(-2), 3, "negative -2: required 1 → d0,d1,d2"),
+ (json!("2<-1"), 2, "combination 2<-1: 3>2 → -1 → required 2"),
+ (
+ json!("3<90%"),
+ 1,
+ "combination 3<90%: 3<=3 → all required → d0",
+ ),
+ ];
+
+ // Answer every case buffered, flush once, answer again — assert agreement
+ // AND the ES-correct count.
+ let mut pre = Vec::new();
+ for (msm, _, _) in &cases {
+ pre.push(hits(&engine, &boolq(msm.clone())).await);
+ }
+ idx.refresh().await.expect("refresh");
+ for (i, (msm, expected, label)) in cases.iter().enumerate() {
+ let post = hits(&engine, &boolq(msm.clone())).await;
+ assert_eq!(pre[i], post, "#846: msm must be flush-invariant — {label}");
+ assert_eq!(post, *expected, "#846: {label}");
+ }
+}
diff --git a/engine/crates/xerj-engine/tests/msm_spec_cache_distinct.rs b/engine/crates/xerj-engine/tests/msm_spec_cache_distinct.rs
new file mode 100644
index 000000000..a36dc526a
--- /dev/null
+++ b/engine/crates/xerj-engine/tests/msm_spec_cache_distinct.rs
@@ -0,0 +1,82 @@
+//! #860 / #846: semantically different `minimum_should_match` specs on an
+//! otherwise identical query must NOT collide in the query-result cache.
+//!
+//! The cache key hashes the serialized request. `MinShouldMatch` is an untagged
+//! enum, so a derived `Serialize` emitted `Negative(2)` and `Percentage(2)` as
+//! the bare number `2` — identical to `Fixed(2)` — so a `msm:-2` query and a
+//! `msm:2` query hashed the same and the second returned the first's cached
+//! count. A hand-written `Serialize` gives every spec a distinct wire form.
+
+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 msm_specs_do_not_collide_in_the_query_cache() {
+ 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();
+ for f in ["fa", "fb", "fc"] {
+ schema.fields.push(FieldConfig::new(f, FieldType::Keyword));
+ }
+ engine.create_index("docs", schema).expect("create");
+ let idx = engine.get_index("docs").expect("get");
+ for (id, doc) in [
+ ("d0", json!({ "fa": "y", "fb": "y", "fc": "y" })), // 3 should match
+ ("d1", json!({ "fa": "y", "fb": "y" })), // 2
+ ("d2", json!({ "fa": "y" })), // 1
+ ] {
+ idx.index_document(Some(id.to_string()), doc)
+ .await
+ .expect("index");
+ }
+
+ let boolq = |msm: serde_json::Value| {
+ json!({ "bool": {
+ "should": [
+ {"term": {"fa": "y"}}, {"term": {"fb": "y"}}, {"term": {"fc": "y"}}
+ ],
+ "minimum_should_match": msm
+ }})
+ };
+
+ // Run the LOW-required spec first so it would poison the cache for the
+ // high-required spec if their keys collided.
+ // Negative(2): required 3-2 = 1 → d0,d1,d2 = 3.
+ assert_eq!(
+ hits(&engine, &boolq(json!(-2))).await,
+ 3,
+ "msm:-2 → required 1"
+ );
+ // Fixed(2): required 2 → d0,d1 = 2. MUST NOT read the msm:-2 entry.
+ assert_eq!(
+ hits(&engine, &boolq(json!(2))).await,
+ 2,
+ "#860: Fixed msm:2 must not collide with the earlier Negative(2) cache entry"
+ );
+
+ // Percentage(2) required floor(3*.02).max(1) = 1 → 3 hits; must also not
+ // collide with Fixed(2) (the pre-existing bare-number collision).
+ assert_eq!(
+ hits(&engine, &boolq(json!("2%"))).await,
+ 3,
+ "msm:2% → required 1"
+ );
+ assert_eq!(
+ hits(&engine, &boolq(json!(2))).await,
+ 2,
+ "#860: Fixed msm:2 must not collide with Percentage(2) either"
+ );
+}
diff --git a/engine/crates/xerj-query/src/ast.rs b/engine/crates/xerj-query/src/ast.rs
index ec89d0b7b..067e8625c 100644
--- a/engine/crates/xerj-query/src/ast.rs
+++ b/engine/crates/xerj-query/src/ast.rs
@@ -90,7 +90,12 @@ pub enum Fuzziness {
///
/// Not `Copy`/`Eq` because the `Script` variant carries an owned source
/// string and a free-form params object (`serde_json::Value` is not `Eq`).
-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+// NOTE: `Serialize` is HAND-WRITTEN below (not derived). The query cache hashes
+// the serialized request (index.rs `body_hash`), and an untagged derive would
+// serialize `Negative(2)`/`Percentage(2)` as the bare number `2` — identical to
+// `Fixed(2)` — conflating semantically different specs into one cache entry
+// (#860). The manual impl gives every spec a distinct wire form.
+#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(untagged)]
pub enum MinShouldMatch {
/// Exact number of `should` clauses that must match.
@@ -99,6 +104,18 @@ pub enum MinShouldMatch {
/// `>100` requires more matches than there are clauses, so the clause set
/// is unsatisfiable (the parser applies no upper cap).
Percentage(u32),
+ /// #846: negative integer `-N` — `required = should_count - N` (floored at
+ /// 0). Stores the absolute value `N`. e.g. `-1` requires all-but-one.
+ Negative(u32),
+ /// #846: negative percentage `-P%` — `required = should_count -
+ /// floor(should_count * P/100)`. Stores `P`. Distinct from `Percentage`
+ /// because the two round differently (ES treats "at most P% may be
+ /// missing", not "P% must match").
+ NegativePercentage(u32),
+ /// #846: combination spec `pivot<spec [pivot<spec ...]` — the spec of the
+ /// highest pivot strictly below `should_count` applies; if `should_count`
+ /// is `<=` every pivot, all clauses are required.
+ Combination(Vec<CombCond>),
/// `terms_set.minimum_should_match_field`: the required count is read
/// per-document from this numeric field.
Field(String),
@@ -111,6 +128,109 @@ pub enum MinShouldMatch {
},
}
+/// One `pivot<spec` condition of a [`MinShouldMatch::Combination`] spec.
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+pub struct CombCond {
+ /// If `should_count > pivot`, this condition's `spec` is a candidate.
+ pub pivot: u32,
+ /// The spec to apply (a `Fixed`, `Percentage`, `Negative`, or
+ /// `NegativePercentage` — never nested `Combination`/`Field`/`Script`).
+ pub spec: Box<MinShouldMatch>,
+}
+
+impl MinShouldMatch {
+ /// Resolve the concrete required `should`-clause count against the number of
+ /// `should` clauses (`should_count`), applying ES `minimum_should_match`
+ /// semantics. Returns `None` for the per-document `Field`/`Script` variants,
+ /// which the caller must resolve against each document.
+ pub fn required(&self, should_count: usize) -> Option<usize> {
+ let n = should_count;
+ match self {
+ MinShouldMatch::Fixed(k) => Some(*k as usize),
+ // floor(n * pct/100), min 1 — matches the legacy resolver.
+ MinShouldMatch::Percentage(pct) => {
+ Some(((n as f32) * (*pct as f32 / 100.0)).floor().max(1.0) as usize)
+ }
+ // required = n - k, floored at 0.
+ MinShouldMatch::Negative(k) => Some(n.saturating_sub(*k as usize)),
+ // required = n - floor(n * pct/100).
+ MinShouldMatch::NegativePercentage(pct) => {
+ let missing = ((n as f32) * (*pct as f32 / 100.0)).floor() as usize;
+ Some(n.saturating_sub(missing))
+ }
+ // The applicable condition is the one with the HIGHEST pivot that is
+ // still strictly below `n`; if `n <= every pivot`, all are required.
+ MinShouldMatch::Combination(conds) => {
+ let applicable = conds
+ .iter()
+ .filter(|c| (c.pivot as usize) < n)
+ .max_by_key(|c| c.pivot)
+ .map(|c| c.spec.as_ref());
+ match applicable {
+ Some(spec) => spec.required(n),
+ None => Some(n),
+ }
+ }
+ MinShouldMatch::Field(_) | MinShouldMatch::Script { .. } => None,
+ }
+ }
+
+ /// The canonical ES string spelling of a SCALAR spec (`Fixed`/`Percentage`/
+ /// `Negative`/`NegativePercentage`). Used inside `Combination` and by the
+ /// custom `Serialize` so distinct specs never share a wire form. `None` for
+ /// the non-scalar `Combination`/`Field`/`Script`.
+ fn spec_token(&self) -> Option<String> {
+ match self {
+ MinShouldMatch::Fixed(n) => Some(n.to_string()),
+ MinShouldMatch::Percentage(p) => Some(format!("{p}%")),
+ MinShouldMatch::Negative(n) => Some(format!("-{n}")),
+ MinShouldMatch::NegativePercentage(p) => Some(format!("-{p}%")),
+ _ => None,
+ }
+ }
+}
+
+impl Serialize for MinShouldMatch {
+ // Distinct wire form per spec so the request hash (query cache) never
+ // conflates two specs (#860). Only feeds the opaque cache hash — no client
+ // response echoes this — so the exact spelling is free to choose.
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ match self {
+ // A plain count stays a JSON number.
+ MinShouldMatch::Fixed(n) => serializer.serialize_u32(*n),
+ // Percentage / negative / negative-% serialize as their distinct ES
+ // string spelling ("50%", "-1", "-25%") — NOT bare numbers.
+ MinShouldMatch::Percentage(_)
+ | MinShouldMatch::Negative(_)
+ | MinShouldMatch::NegativePercentage(_) => {
+ serializer.serialize_str(&self.spec_token().expect("scalar spec has a token"))
+ }
+ // Combination as its "pivot<spec ..." spelling.
+ MinShouldMatch::Combination(conds) => {
+ let s = conds
+ .iter()
+ .map(|c| format!("{}<{}", c.pivot, c.spec.spec_token().unwrap_or_default()))
+ .collect::<Vec<_>>()
+ .join(" ");
+ serializer.serialize_str(&s)
+ }
+ MinShouldMatch::Field(f) => serializer.serialize_str(f),
+ MinShouldMatch::Script { source, params } => {
+ use serde::ser::SerializeMap;
+ let mut m = serializer.serialize_map(None)?;
+ m.serialize_entry("source", source)?;
+ if let Some(p) = params {
+ m.serialize_entry("params", p)?;
+ }
+ m.end()
+ }
+ }
+ }
+}
+
// ─────────────────────────────────────────────────────────────────────────────
// QueryNode — the core type
// ─────────────────────────────────────────────────────────────────────────────
diff --git a/engine/crates/xerj-query/src/parser.rs b/engine/crates/xerj-query/src/parser.rs
index 43817ac40..85eeb0e3b 100644
--- a/engine/crates/xerj-query/src/parser.rs
+++ b/engine/crates/xerj-query/src/parser.rs
@@ -838,17 +838,9 @@ fn parse_multi_match(params: &Value) -> Result<QueryNode> {
.as_deref(),
Some("and")
);
- let mm_ms = obj.get("minimum_should_match").and_then(|v| match v {
- Value::Number(n) => n.as_u64().map(|i| MinShouldMatch::Fixed(i as u32)),
- Value::String(s) => {
- if let Some(p) = s.strip_suffix('%') {
- p.parse::<u32>().ok().map(MinShouldMatch::Percentage)
- } else {
- s.parse::<u32>().ok().map(MinShouldMatch::Fixed)
- }
- }
- _ => None,
- });
+ let mm_ms = obj
+ .get("minimum_should_match")
+ .and_then(|v| parse_min_should_match(v).ok());
let fuzziness_opt = obj.get("fuzziness").map(|v| match v {
Value::String(s) if s.eq_ignore_ascii_case("auto") => crate::ast::Fuzziness::Auto,
Value::String(s) => s
@@ -2397,17 +2389,9 @@ fn parse_simple_query_string(params: &Value) -> Result<QueryNode> {
.unwrap_or("or")
.to_lowercase();
- let mm: Option<MinShouldMatch> = obj.get("minimum_should_match").and_then(|v| match v {
- Value::Number(n) => n.as_u64().map(|i| MinShouldMatch::Fixed(i as u32)),
- Value::String(s) => {
- if let Some(stripped) = s.strip_suffix('%') {
- stripped.parse::<u32>().ok().map(MinShouldMatch::Percentage)
- } else {
- s.parse::<u32>().ok().map(MinShouldMatch::Fixed)
- }
- }
- _ => None,
- });
+ let mm: Option<MinShouldMatch> = obj
+ .get("minimum_should_match")
+ .and_then(|v| parse_min_should_match(v).ok());
// Tokenize the query: split on whitespace; leading +/-/| signal per-term operators.
let mut must: Vec<QueryNode> = Vec::new();
@@ -3399,35 +3383,75 @@ fn qerr(msg: impl Into<String>) -> QueryError {
/// Parse a `minimum_should_match` value.
fn parse_min_should_match(v: &Value) -> Result<MinShouldMatch> {
match v {
+ // #846: a negative JSON integer (`-1`) is "all but N", not an error.
Value::Number(n) => {
- let i = n
- .as_u64()
- .ok_or_else(|| qerr("`minimum_should_match` must be non-negative"))?;
- Ok(MinShouldMatch::Fixed(i as u32))
- }
- Value::String(s) => {
- let s = s.trim();
- if let Some(pct) = s.strip_suffix('%') {
- let pct: i64 = pct.trim().parse().map_err(|_| {
- qerr(format!("invalid `minimum_should_match` percentage: `{s}`"))
- })?;
- let pct = if pct < 0 {
- (100 + pct).max(0) as u32
- } else {
- pct as u32
- };
- Ok(MinShouldMatch::Percentage(pct))
+ if let Some(i) = n.as_u64() {
+ Ok(MinShouldMatch::Fixed(i as u32))
+ } else if let Some(neg) = n.as_i64().filter(|x| *x < 0) {
+ Ok(MinShouldMatch::Negative(neg.unsigned_abs() as u32))
} else {
- let n: u32 = s
- .parse()
- .map_err(|_| qerr(format!("invalid `minimum_should_match` value: `{s}`")))?;
- Ok(MinShouldMatch::Fixed(n))
+ Err(qerr("`minimum_should_match` must be an integer"))
}
}
+ Value::String(s) => parse_min_should_match_str(s.trim()),
_ => invalid("`minimum_should_match` must be a number or string"),
}
}
+/// Parse the string spelling of `minimum_should_match`: a plain or negative
+/// integer, a plain or negative percentage, or a `pivot<spec [pivot<spec ...]`
+/// combination (#846). Combination specs recurse (their `spec` is any of the
+/// non-combination forms).
+fn parse_min_should_match_str(s: &str) -> Result<MinShouldMatch> {
+ let s = s.trim();
+ // Combination: whitespace-separated `pivot<spec` conditions.
+ if s.contains('<') {
+ let mut conds = Vec::new();
+ for tok in s.split_whitespace() {
+ let (pivot, spec) = tok.split_once('<').ok_or_else(|| {
+ qerr(format!("invalid `minimum_should_match` combination: `{s}`"))
+ })?;
+ let pivot: u32 = pivot
+ .trim()
+ .parse()
+ .map_err(|_| qerr(format!("invalid `minimum_should_match` pivot in `{tok}`")))?;
+ let spec = parse_min_should_match_str(spec.trim())?;
+ conds.push(crate::ast::CombCond {
+ pivot,
+ spec: Box::new(spec),
+ });
+ }
+ return Ok(MinShouldMatch::Combination(conds));
+ }
+ // Percentage, possibly negative.
+ if let Some(pct) = s.strip_suffix('%') {
+ let pct = pct.trim();
+ if let Some(neg) = pct.strip_prefix('-') {
+ let p: u32 = neg
+ .trim()
+ .parse()
+ .map_err(|_| qerr(format!("invalid `minimum_should_match` percentage: `{s}`")))?;
+ return Ok(MinShouldMatch::NegativePercentage(p));
+ }
+ let p: u32 = pct
+ .parse()
+ .map_err(|_| qerr(format!("invalid `minimum_should_match` percentage: `{s}`")))?;
+ return Ok(MinShouldMatch::Percentage(p));
+ }
+ // Plain integer, possibly negative.
+ if let Some(neg) = s.strip_prefix('-') {
+ let k: u32 = neg
+ .trim()
+ .parse()
+ .map_err(|_| qerr(format!("invalid `minimum_should_match` value: `{s}`")))?;
+ return Ok(MinShouldMatch::Negative(k));
+ }
+ let n: u32 = s
+ .parse()
+ .map_err(|_| qerr(format!("invalid `minimum_should_match` value: `{s}`")))?;
+ Ok(MinShouldMatch::Fixed(n))
+}
+
fn parse_bool_operator(v: Option<&Value>) -> Result<BoolOperator> {
match v {
None => Ok(BoolOperator::Or),
@@ -4232,17 +4256,14 @@ fn parse_match_bool_prefix(params: &Value) -> Result<QueryNode> {
qerr("`match_bool_prefix` field value must be a string or object")
})?;
let q = string_field(inner, "query")?;
- let mm_val = inner.get("minimum_should_match").and_then(|v| match v {
- Value::Number(n) => n.as_u64().map(|i| MinShouldMatch::Fixed(i as u32)),
- Value::String(s) => {
- if let Some(p) = s.strip_suffix('%') {
- p.parse::<u32>().ok().map(MinShouldMatch::Percentage)
- } else {
- s.parse::<u32>().ok().map(MinShouldMatch::Fixed)
- }
- }
- _ => None,
- });
+ // #846: go through the shared `minimum_should_match` parser so
+ // negative / negative-% / combination specs reach the evaluator
+ // from this site too. It used to inline a number/percentage-only
+ // subset, which silently dropped every other spec to `None` (=
+ // plain OR) instead of honouring it.
+ let mm_val = inner
+ .get("minimum_should_match")
+ .and_then(|v| parse_min_should_match(v).ok());
let op_and = matches!(
inner
.get("operator")
@@ -4639,6 +4660,87 @@ mod tests {
parse_query(&j).expect("parse failed")
}
+ // ── #846: minimum_should_match negative / combination specs ───────────────
+
+ #[test]
+ fn msm_parses_negative_and_combination_specs() {
+ use crate::ast::CombCond;
+ let p = |v: serde_json::Value| parse_min_should_match(&v).expect("parse msm");
+
+ // Negative integer (number AND string spellings) — before #846 the
+ // number errored ("must be non-negative") and the string dropped to
+ // Fixed-parse failure; both are now Negative(N) = all-but-N.
+ assert_eq!(p(json!(-1)), MinShouldMatch::Negative(1));
+ assert_eq!(p(json!("-2")), MinShouldMatch::Negative(2));
+ // Plain forms unchanged.
+ assert_eq!(p(json!(3)), MinShouldMatch::Fixed(3));
+ assert_eq!(p(json!("75%")), MinShouldMatch::Percentage(75));
+ // Negative percentage — distinct from Percentage (they round differently).
+ assert_eq!(p(json!("-25%")), MinShouldMatch::NegativePercentage(25));
+ // Combination — single and multiple conditions.
+ assert_eq!(
+ p(json!("3<90%")),
+ MinShouldMatch::Combination(vec![CombCond {
+ pivot: 3,
+ spec: Box::new(MinShouldMatch::Percentage(90)),
+ }])
+ );
+ assert_eq!(
+ p(json!("2<-25% 9<-3")),
+ MinShouldMatch::Combination(vec![
+ CombCond {
+ pivot: 2,
+ spec: Box::new(MinShouldMatch::NegativePercentage(25)),
+ },
+ CombCond {
+ pivot: 9,
+ spec: Box::new(MinShouldMatch::Negative(3)),
+ },
+ ])
+ );
+ }
+
+ #[test]
+ fn msm_required_resolves_es_semantics() {
+ // Negative: required = n - N (floored at 0).
+ assert_eq!(MinShouldMatch::Negative(1).required(4), Some(3));
+ assert_eq!(MinShouldMatch::Negative(9).required(4), Some(0));
+ // Negative percentage: required = n - floor(n*P/100); differs from the
+ // same-looking Percentage on non-divisible counts.
+ assert_eq!(MinShouldMatch::NegativePercentage(25).required(5), Some(4));
+ assert_eq!(MinShouldMatch::Percentage(75).required(5), Some(3));
+ // Combination: n <= every pivot ⇒ all required; else highest pivot < n.
+ let c = MinShouldMatch::Combination(vec![
+ crate::ast::CombCond {
+ pivot: 2,
+ spec: Box::new(MinShouldMatch::NegativePercentage(25)),
+ },
+ crate::ast::CombCond {
+ pivot: 9,
+ spec: Box::new(MinShouldMatch::Negative(3)),
+ },
+ ]);
+ assert_eq!(c.required(2), Some(2)); // n <= smallest pivot → all
+ assert_eq!(c.required(5), Some(4)); // pivot 2 applies: 5 - floor(1.25)
+ assert_eq!(c.required(10), Some(7)); // pivot 9 applies: 10 - 3
+ }
+
+ #[test]
+ fn msm_negative_survives_through_bool_parse() {
+ // End-to-end: a bool with `minimum_should_match: -1` no longer errors
+ // or drops — it lands as Negative(1) on the AST.
+ match q(json!({"bool": {"should": [
+ {"term": {"a": "x"}}, {"term": {"b": "y"}}, {"term": {"c": "z"}}
+ ], "minimum_should_match": -1}}))
+ {
+ QueryNode::Bool {
+ minimum_should_match,
+ ..
+ } => assert_eq!(minimum_should_match, Some(MinShouldMatch::Negative(1))),
+ other => panic!("expected Bool, got {other:?}"),
+ }
+ }
+
// ── match_all ─────────────────────────────────────────────────────────────
#[test]