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(search,def): drop a field any index maps as numeric (#1617)

xerj-org265637377668
diff --git a/engine/crates/xerj-autoindex/src/def.rs b/engine/crates/xerj-autoindex/src/def.rs
index fcb46bf5f..cb7081943 100644
--- a/engine/crates/xerj-autoindex/src/def.rs
+++ b/engine/crates/xerj-autoindex/src/def.rs
@@ -15,8 +15,8 @@ use std::collections::BTreeSet;
use crate::esclient::Es;
use crate::search::{
- corpus_snapshot, empty_lookup_report, field_mapped, hit_location, is_identifier,
- mapped_property_names, passage_text, weighted_query_fields,
+ corpus_snapshot, empty_lookup_report, field_lookup_ok, hit_location, is_identifier,
+ mapped_property_names, numeric_property_names, passage_text, weighted_query_fields,
};
use serde_json::{json, Value};
@@ -92,10 +92,17 @@ const DEMOTED_PATH_GLOBS: &[&str] = &[
"*/bench/*",
];
+/// Fields `xerj def` may name. A numeric mapping of one of these on any
+/// index drops it from the wildcard (#1284), including `term` / `match_phrase`
+/// and the kind/`pub` ranking weights.
+const DEF_LOOKUP_FIELDS: &[&str] = &[
+ "name", "defs", "code", "title", "body", "text", "impl", "message", "kind", "language",
+];
+
/// True only when a mapping was actually read and names `field`. `None` and
/// an empty set stay on the historical query (`field_mapped` treats both as
/// "assume the code fields"), so a new clause that is not in that historical
-/// shape must use this and not [`field_mapped`].
+/// shape must use this and not [`field_lookup_ok`].
fn explicitly_mapped(mapped: Option<&BTreeSet<String>>, field: &str) -> bool {
mapped.is_some_and(|set| set.contains(field))
}
@@ -113,14 +120,20 @@ fn def_query(
lang: Option<&str>,
k: usize,
mapped: Option<&BTreeSet<String>>,
+ numeric: Option<&BTreeSet<String>>,
) -> Value {
- let built = build_def_query(query, kind, lang, k, mapped);
+ let built = build_def_query(query, kind, lang, k, mapped, numeric);
let empty = built
.pointer("/query/bool/should")
.and_then(Value::as_array)
.is_some_and(Vec::is_empty);
- if empty {
- return build_def_query(query, kind, lang, k, None);
+ // A numeric field must stay dropped. Restoring the historical query
+ // would name it again. `minimum_should_match: 1` with an empty `should`
+ // matches nothing, which is the safe result.
+ let numeric_lookup =
+ numeric.is_some_and(|set| DEF_LOOKUP_FIELDS.iter().any(|name| set.contains(*name)));
+ if empty && !numeric_lookup {
+ return build_def_query(query, kind, lang, k, None, None);
}
built
}
@@ -131,6 +144,7 @@ fn build_def_query(
lang: Option<&str>,
k: usize,
mapped: Option<&BTreeSet<String>>,
+ numeric: Option<&BTreeSet<String>>,
) -> Value {
// Definition-first ranking, measured at 94% correct-file-at-rank-1 on an
// 80-task cross-file benchmark (vs 38% for body-text search):
@@ -141,18 +155,21 @@ fn build_def_query(
// - `multi_match`: recall floor so plain-words queries ("euler angles to
// rotation matrix") still surface the definer via `defs`/`code` text.
let mut should = Vec::new();
- if field_mapped(mapped, "name") {
+ if field_lookup_ok(mapped, numeric, "name") {
should.push(json!({ "term": { "name": { "value": query, "boost": 12 } } }));
}
- if field_mapped(mapped, "defs") {
+ if field_lookup_ok(mapped, numeric, "defs") {
should.push(json!({ "match_phrase": { "defs": { "query": query, "boost": 8 } } }));
}
- let mut fields = weighted_query_fields(DEF_MM_FIELDS, DEF_PROSE_EXTRA_FIELDS, mapped);
+ let mut fields = weighted_query_fields(DEF_MM_FIELDS, DEF_PROSE_EXTRA_FIELDS, mapped, numeric);
// A phrase ("flush the tree to disk") lives in the symbol body (`impl`),
- // not in the declaration. Only when the read mapping actually has `impl`:
- // the historical code query (mapped = None) does not name that field.
+ // not in the declaration. Only when the read mapping actually has `impl`
+ // and no index maps it as numeric: the historical code query
+ // (mapped = None) does not name that field, and a numeric `impl` would
+ // fail the wildcard the same way a numeric `name` does.
if !is_identifier(query)
&& explicitly_mapped(mapped, "impl")
+ && !numeric.is_some_and(|set| set.contains("impl"))
&& !fields.iter().any(|f| f.split('^').next() == Some("impl"))
{
fields.push("impl^2".to_string());
@@ -164,19 +181,19 @@ fn build_def_query(
"type": "most_fields"
}}));
}
- if is_identifier(query) && field_mapped(mapped, "code") {
+ if is_identifier(query) && field_lookup_ok(mapped, numeric, "code") {
// `code` holds the declaration text; a phrase hit there ("def <name>(",
// "class <name>:") is a definition even when `name` casing differs.
should.push(json!({ "match_phrase": { "code": { "query": query, "boost": 4 } } }));
}
let mut filter = Vec::new();
if let Some(kd) = kind {
- if field_mapped(mapped, "kind") {
+ if field_lookup_ok(mapped, numeric, "kind") {
filter.push(json!({ "term": { "kind": kd } }));
}
}
if let Some(lg) = lang {
- if field_mapped(mapped, "language") {
+ if field_lookup_ok(mapped, numeric, "language") {
filter.push(json!({ "term": { "language": lg } }));
}
}
@@ -193,7 +210,7 @@ fn build_def_query(
// Kind and `pub` weights sit on function_score, not on a bare `should`:
// a should clause would match every struct in the index. The negative
// boosting clause only multiplies hits that already matched.
- let query = rank_adjusted_query(bool_query, mapped);
+ let query = rank_adjusted_query(bool_query, mapped, numeric);
json!({
"size": k,
"query": query,
@@ -202,13 +219,20 @@ fn build_def_query(
})
}
-fn rank_adjusted_query(bool_query: Value, mapped: Option<&BTreeSet<String>>) -> Value {
+fn rank_adjusted_query(
+ bool_query: Value,
+ mapped: Option<&BTreeSet<String>>,
+ numeric: Option<&BTreeSet<String>>,
+) -> Value {
// `function_score` (boost_mode sum) adds weight only to hits the bool
// already matched. A `boosting` wrapper was tried first; this engine's
// planner rewrites it to a bool and every hit came back with `_score`
// 1.0, so the weights never moved the order.
let mut functions = Vec::new();
- if field_mapped(mapped, "kind") {
+ // A numeric `kind` or `code` cannot carry the ranking filter: the
+ // wildcard would fail on that index the same way a `term` on a numeric
+ // `name` does. Unreadable mappings (numeric = None) keep both weights.
+ if field_lookup_ok(mapped, numeric, "kind") {
let mut kind_filter = json!({
"bool": { "must": [ { "terms": { "kind": PREFERRED_KINDS } } ] }
});
@@ -226,7 +250,7 @@ fn rank_adjusted_query(bool_query: Value, mapped: Option<&BTreeSet<String>>) ->
}
functions.push(json!({ "filter": kind_filter, "weight": 20.0 }));
}
- if field_mapped(mapped, "code") {
+ if field_lookup_ok(mapped, numeric, "code") {
functions.push(json!({
"filter": { "bool": { "should": [
{ "match": { "code": "pub" } },
@@ -329,10 +353,11 @@ pub fn run_def_cli() -> i32 {
}
let pattern = format!("{prefix}-*");
- let mapped = es
- .get_json(&format!("/{pattern}/_mapping"))
- .ok()
- .and_then(|body| mapped_property_names(&body));
+ let raw_mapping = es.get_json(&format!("/{pattern}/_mapping")).ok();
+ let mapped = raw_mapping.as_ref().and_then(mapped_property_names);
+ let numeric = mapped
+ .as_ref()
+ .and(raw_mapping.as_ref().map(numeric_property_names));
if let Some(set) = mapped.as_ref().filter(|s| !s.is_empty()) {
if kind.is_some() && !set.contains("kind") {
eprintln!("xerj def: --kind ignored; no index under this prefix maps `kind`");
@@ -341,7 +366,14 @@ pub fn run_def_cli() -> i32 {
eprintln!("xerj def: --lang ignored; no index under this prefix maps `language`");
}
}
- let body = def_query(&query, kind.as_deref(), lang.as_deref(), k, mapped.as_ref());
+ let body = def_query(
+ &query,
+ kind.as_deref(),
+ lang.as_deref(),
+ k,
+ mapped.as_ref(),
+ numeric.as_ref(),
+ );
let resp = match es.search(&pattern, &body) {
Ok(v) => v,
Err(e) => {
@@ -424,7 +456,7 @@ mod tests {
(Some("class"), Some("rust")),
(None, None),
] {
- let body = def_query("zzqq_nonexistent", kind, lang, 5, None);
+ let body = def_query("zzqq_nonexistent", kind, lang, 5, None, None);
// Kind/pub weights wrap the bool; the filter still has to sit on
// that bool, not replace the name clauses.
let b = body
@@ -461,6 +493,7 @@ mod tests {
Some("markdown"),
5,
Some(&prose),
+ None,
);
let should = body["query"]["bool"]["should"].as_array().unwrap();
assert_eq!(should.len(), 1, "{body}");
@@ -490,12 +523,12 @@ mod tests {
]);
let query = "compressPayload";
assert_eq!(
- def_query(query, Some("function"), Some("rust"), 5, Some(&code)),
- def_query(query, Some("function"), Some("rust"), 5, None)
+ def_query(query, Some("function"), Some("rust"), 5, Some(&code), None),
+ def_query(query, Some("function"), Some("rust"), 5, None, None)
);
assert_eq!(
- def_query(query, None, None, 5, Some(&BTreeSet::new())),
- def_query(query, None, None, 5, None)
+ def_query(query, None, None, 5, Some(&BTreeSet::new()), None),
+ def_query(query, None, None, 5, None, None)
);
}
@@ -504,7 +537,7 @@ mod tests {
/// function_score filter, not a `should`, so it cannot match every struct.
#[test]
fn preferred_kinds_and_pub_are_weighted_and_aliases_are_demoted() {
- let body = def_query("Db", None, None, 5, None);
+ let body = def_query("Db", None, None, 5, None, None);
let rendered = body["query"].to_string();
assert!(rendered.contains("function_score"), "{body}");
assert!(
@@ -530,7 +563,7 @@ mod tests {
);
assert!(!rendered.contains("ax_path"), "{body}");
let with_path = names_of(&["name", "defs", "code", "title", "kind", "ax_path"]);
- let body = def_query("Db", None, None, 5, Some(&with_path));
+ let body = def_query("Db", None, None, 5, Some(&with_path), None);
let rendered = body["query"].to_string();
assert!(rendered.contains("*/tests/*"), "{body}");
assert!(rendered.contains("*/examples/*"), "{body}");
@@ -544,7 +577,7 @@ mod tests {
#[test]
fn a_phrase_searches_impl_only_when_the_mapping_has_it() {
let code = names_of(&["name", "defs", "code", "title", "impl", "kind"]);
- let phrase = def_query("flush the tree to disk", None, None, 5, Some(&code));
+ let phrase = def_query("flush the tree to disk", None, None, 5, Some(&code), None);
let fields = phrase["query"]["function_score"]["query"]["bool"]["should"]
.as_array()
.unwrap()
@@ -559,15 +592,81 @@ mod tests {
.any(|f| f.as_str() == Some("impl^2")),
"{phrase}"
);
- let ident = def_query("flush", None, None, 5, Some(&code));
+ let ident = def_query("flush", None, None, 5, Some(&code), None);
let rendered = ident.to_string();
assert!(!rendered.contains("impl^2"), "{ident}");
let no_impl = names_of(&["name", "defs", "code", "title", "kind"]);
- let phrase = def_query("flush the tree to disk", None, None, 5, Some(&no_impl));
+ let phrase = def_query(
+ "flush the tree to disk",
+ None,
+ None,
+ 5,
+ Some(&no_impl),
+ None,
+ );
let fields = phrase.to_string();
assert!(
!fields.contains("impl^2"),
"no impl field, so the body leg must stay out: {phrase}"
);
}
+
+ /// #1284: `name` mapped `long` on any index must not be a `term` or a
+ /// `multi_match` leg, and `defs` mapped `double` must not be a phrase.
+ /// `keyword` is not numeric, so a keyword-only `name` stays.
+ #[test]
+ fn a_numeric_name_or_defs_field_is_not_looked_up() {
+ assert!(!numeric_property_names(&json!({
+ "only": { "mappings": { "properties": { "name": { "type": "keyword" } } } }
+ }))
+ .contains("name"));
+ let mapping = json!({
+ "symbols": { "mappings": { "properties": {
+ "name": { "type": "keyword" },
+ "defs": { "type": "text" },
+ "code": { "type": "text" },
+ "title": { "type": "text" },
+ "kind": { "type": "keyword" }
+ }}},
+ "metrics": { "mappings": { "properties": {
+ "name": { "type": "long" },
+ "defs": { "type": "double" },
+ "kind": { "type": "integer" }
+ }}}
+ });
+ let names = mapped_property_names(&mapping).unwrap();
+ let numeric = numeric_property_names(&mapping);
+ let body = def_query(
+ "compressPayload",
+ Some("function"),
+ None,
+ 5,
+ Some(&names),
+ Some(&numeric),
+ );
+ let query = body["query"].to_string();
+ assert!(!query.contains("\"name\""), "{query}");
+ assert!(!query.contains("defs"), "{query}");
+ assert!(!query.contains("kind"), "{query}");
+ assert!(query.contains("code"), "{query}");
+ assert!(query.contains("title"), "{query}");
+ // `code` is still text, so the pub ranking weight wraps function_score.
+ // The numeric `kind` must not: that filter would name `kind`.
+ let should = body
+ .pointer("/query/function_score/query/bool/should")
+ .or_else(|| body.pointer("/query/bool/should"))
+ .and_then(Value::as_array)
+ .expect("should clauses");
+ let fields = should
+ .iter()
+ .find_map(|clause| clause.get("multi_match"))
+ .expect("recall floor")["fields"]
+ .as_array()
+ .unwrap();
+ let bare: Vec<&str> = fields
+ .iter()
+ .map(|f| f.as_str().unwrap().split('^').next().unwrap())
+ .collect();
+ assert_eq!(bare, vec!["code", "title"]);
+ }
}
diff --git a/engine/crates/xerj-autoindex/src/search.rs b/engine/crates/xerj-autoindex/src/search.rs
index 7ddc0bd76..1838b2992 100644
--- a/engine/crates/xerj-autoindex/src/search.rs
+++ b/engine/crates/xerj-autoindex/src/search.rs
@@ -97,6 +97,13 @@ const SEARCH_MM_FIELDS: &[(&str, &str)] = &[
/// #916 shape exactly.
const PROSE_EXTRA_FIELDS: &[&str] = &["text", "impl", "message"];
+/// Every field `xerj search` might name in `multi_match`, `term`, or
+/// `match_phrase`. A numeric mapping of one of these on any index in the
+/// wildcard fails the whole request (`For input string`, #1284).
+const SEARCH_LOOKUP_FIELDS: &[&str] = &[
+ "name", "code", "body", "defs", "title", "text", "impl", "message",
+];
+
/// Union of property names in a `GET /{pattern}/_mapping` body.
///
/// `None` when `mapping` is not that shape, so a transport failure keeps the
@@ -126,6 +133,72 @@ pub(crate) fn mapped_property_names(mapping: &Value) -> Option<BTreeSet<String>>
Some(names)
}
+/// Types stored as `long` or `double`. A `multi_match`, `term`, or
+/// `match_phrase` that names one fails with `For input string: "<query>"`
+/// (#1284). `keyword` is not in this list: `name` stays an exact term.
+fn is_numeric_mapping_type(ty: &str) -> bool {
+ matches!(
+ ty,
+ "long"
+ | "integer"
+ | "short"
+ | "byte"
+ | "unsigned_long"
+ | "double"
+ | "float"
+ | "half_float"
+ | "scaled_float"
+ )
+}
+
+/// Field names any index in a `GET /{pattern}/_mapping` body maps as numeric.
+///
+/// One numeric mapping is enough to drop the field from a wildcard query:
+/// the request is refused for every index, the way `xerj code` drops a
+/// numeric field from its union (#1605). An unreadable body yields an empty
+/// set; callers ignore that unless [`mapped_property_names`] also succeeded.
+pub(crate) fn numeric_property_names(mapping: &Value) -> BTreeSet<String> {
+ let mut out = BTreeSet::new();
+ let Some(obj) = mapping.as_object() else {
+ return out;
+ };
+ for body in obj.values() {
+ let Some(props) = body
+ .pointer("/mappings/properties")
+ .and_then(Value::as_object)
+ else {
+ continue;
+ };
+ for (key, spec) in props {
+ if spec
+ .get("type")
+ .and_then(Value::as_str)
+ .is_some_and(is_numeric_mapping_type)
+ {
+ out.insert(key.clone());
+ }
+ }
+ }
+ out
+}
+
+/// A lookup clause may name `field` when it is mapped (or the mapping was
+/// unreadable) and no index maps it as numeric.
+pub(crate) fn field_lookup_ok(
+ mapped: Option<&BTreeSet<String>>,
+ numeric: Option<&BTreeSet<String>>,
+ field: &str,
+) -> bool {
+ field_mapped(mapped, field) && !numeric.is_some_and(|set| set.contains(field))
+}
+
+/// True when a numeric mapping removed a field this command would look up,
+/// so the historical query must not be restored — it would name that field
+/// again and the wildcard would fail.
+fn numeric_drops_a_lookup(numeric: Option<&BTreeSet<String>>, candidates: &[&str]) -> bool {
+ numeric.is_some_and(|set| candidates.iter().any(|name| set.contains(*name)))
+}
+
/// Whether `field` may be named in a lookup clause.
///
/// An unreadable mapping (`None`) or an empty union keeps every historical
@@ -153,16 +226,20 @@ pub(crate) fn weighted_query_fields(
preferred: &[(&str, &str)],
extras: &[&str],
mapped: Option<&BTreeSet<String>>,
+ numeric: Option<&BTreeSet<String>>,
) -> Vec<String> {
let mut out: Vec<String> = preferred
.iter()
- .filter(|(name, _)| field_mapped(mapped, name))
+ .filter(|(name, _)| field_lookup_ok(mapped, numeric, name))
.map(|(_, weighted)| (*weighted).to_string())
.collect();
if code_symbol_fields_absent(mapped) {
if let Some(set) = mapped {
for name in extras {
- if set.contains(*name) && !out.iter().any(|f| f.split('^').next() == Some(*name)) {
+ if set.contains(*name)
+ && !numeric.is_some_and(|nums| nums.contains(*name))
+ && !out.iter().any(|f| f.split('^').next() == Some(*name))
+ {
out.push((*name).to_string());
}
}
@@ -176,20 +253,29 @@ pub(crate) fn weighted_query_fields(
/// `mapped` is the union of property names under the prefix. `None` (and an
/// empty union) is the historical code-shaped query.
pub fn search_query_body(query: &str, k: usize) -> Value {
- search_query_body_for_mapping(query, k, None)
+ search_query_body_for_mapping(query, k, None, None)
}
-/// [`search_query_body`] with the target's mapped field names (#1601).
+/// [`search_query_body`] with the target's mapped field names (#1601) and
+/// the names any index maps as numeric (#1284).
pub fn search_query_body_for_mapping(
query: &str,
k: usize,
mapped: Option<&BTreeSet<String>>,
+ numeric: Option<&BTreeSet<String>>,
) -> Value {
- let built = build_search_query(query, k, mapped);
+ let built = build_search_query(query, k, mapped, numeric);
if should_clauses(&built).is_empty() {
+ if numeric_drops_a_lookup(numeric, SEARCH_LOOKUP_FIELDS) {
+ // Restoring the historical query would name the numeric field
+ // again. An empty `should` matches every document; match nothing.
+ let mut built = built;
+ built["query"] = json!({ "match_none": {} });
+ return built;
+ }
// Nothing we can name is mapped. The historical shape is what the
// server's 400 then explains, listing the searchable fields.
- return build_search_query(query, k, None);
+ return build_search_query(query, k, None, None);
}
built
}
@@ -201,10 +287,15 @@ fn should_clauses(body: &Value) -> &[Value] {
.unwrap_or(&[])
}
-fn build_search_query(query: &str, k: usize, mapped: Option<&BTreeSet<String>>) -> Value {
+fn build_search_query(
+ query: &str,
+ k: usize,
+ mapped: Option<&BTreeSet<String>>,
+ numeric: Option<&BTreeSet<String>>,
+) -> Value {
let shaped = xerj_common::code_query::normalize_code_search_query(query);
let mut should = Vec::new();
- let fields = weighted_query_fields(SEARCH_MM_FIELDS, PROSE_EXTRA_FIELDS, mapped);
+ let fields = weighted_query_fields(SEARCH_MM_FIELDS, PROSE_EXTRA_FIELDS, mapped, numeric);
if !fields.is_empty() {
should.push(json!({ "multi_match": {
"query": shaped,
@@ -212,10 +303,10 @@ fn build_search_query(query: &str, k: usize, mapped: Option<&BTreeSet<String>>)
"type": "most_fields"
}}));
}
- if field_mapped(mapped, "defs") {
+ if field_lookup_ok(mapped, numeric, "defs") {
should.push(json!({ "match_phrase": { "defs": { "query": shaped, "boost": 8 } } }));
}
- if field_mapped(mapped, "name") {
+ if field_lookup_ok(mapped, numeric, "name") {
should.push(json!({ "term": { "name": { "value": query.trim(), "boost": 10 } } }));
}
json!({
@@ -490,11 +581,15 @@ pub fn run_search_cli() -> i32 {
// (#1601). Read the mapping and drop a leg no resolved index maps; an
// unreadable mapping keeps the code-shaped query.
let pattern = format!("{prefix}-*");
- let mapped = es
- .get_json(&format!("/{pattern}/_mapping"))
- .ok()
- .and_then(|body| mapped_property_names(&body));
- let body = search_query_body_for_mapping(&query, k, mapped.as_ref());
+ let raw_mapping = es.get_json(&format!("/{pattern}/_mapping")).ok();
+ let mapped = raw_mapping.as_ref().and_then(mapped_property_names);
+ // Types are only trusted when the name union was readable. An error body
+ // has no properties, and treating that as "nothing is numeric" while the
+ // names fall back to the historical query is the #1601 contract.
+ let numeric = mapped
+ .as_ref()
+ .and(raw_mapping.as_ref().map(numeric_property_names));
+ let body = search_query_body_for_mapping(&query, k, mapped.as_ref(), numeric.as_ref());
let resp = match es.search(&pattern, &body) {
Ok(v) => v,
Err(e) => {
@@ -735,7 +830,7 @@ mod tests {
#[test]
fn prose_mapping_drops_unmapped_code_legs() {
let mapped = names_of(&["body", "title", "text", "impl"]);
- let body = search_query_body_for_mapping("retention ceiling", 5, Some(&mapped));
+ let body = search_query_body_for_mapping("retention ceiling", 5, Some(&mapped), None);
let should = body["query"]["bool"]["should"].as_array().unwrap();
assert_eq!(should.len(), 1, "{body}");
assert!(should[0].get("match_phrase").is_none(), "{body}");
@@ -755,7 +850,7 @@ mod tests {
#[test]
fn log_mapping_searches_message_and_names_no_code_field() {
let mapped = names_of(&["message", "ts", "host"]);
- let body = search_query_body_for_mapping("SIP 403 Forbidden", 3, Some(&mapped));
+ let body = search_query_body_for_mapping("SIP 403 Forbidden", 3, Some(&mapped), None);
let should = body["query"]["bool"]["should"].as_array().unwrap();
assert_eq!(should.len(), 1, "{body}");
assert_eq!(should[0]["multi_match"]["fields"], json!(["message"]));
@@ -767,23 +862,26 @@ mod tests {
fn code_mapping_and_unreadable_mapping_keep_the_historical_query() {
let query = "fsync the WAL segment";
let historical = search_query_body(query, 5);
- assert_eq!(search_query_body_for_mapping(query, 5, None), historical);
+ assert_eq!(
+ search_query_body_for_mapping(query, 5, None, None),
+ historical
+ );
let empty = BTreeSet::new();
assert_eq!(
- search_query_body_for_mapping(query, 5, Some(&empty)),
+ search_query_body_for_mapping(query, 5, Some(&empty), None),
historical
);
assert!(mapped_property_names(&json!({"error": "no such index"})).is_none());
let code = names_of(&["name", "code", "body", "defs", "title", "text", "impl"]);
assert_eq!(
- search_query_body_for_mapping(query, 5, Some(&code)),
+ search_query_body_for_mapping(query, 5, Some(&code), Some(&BTreeSet::new())),
historical
);
// One index in the union mapping `defs` is enough to keep the leg:
// strict mode refuses a field no resolved index maps, not a field
// some index does not map.
let partial = names_of(&["body", "defs", "title"]);
- let body = search_query_body_for_mapping(query, 5, Some(&partial));
+ let body = search_query_body_for_mapping(query, 5, Some(&partial), None);
let should = body["query"]["bool"]["should"].as_array().unwrap();
assert!(
should.iter().any(|c| c.get("match_phrase").is_some()),
@@ -925,4 +1023,75 @@ mod tests {
assert!(text.contains("catalog lists no prefixes"), "{text}");
assert!(!text.contains("xerj autoindex"), "{text}");
}
+
+ /// #1284: one index mapping `code` as `long` fails a wildcard `multi_match`
+ /// or `match_phrase` on `code` for every index. Drop it. `keyword` `name`
+ /// stays. A prose field that is numeric on any index is dropped too, and
+ /// when that was the only lookup the query matches nothing rather than
+ /// restoring the historical legs.
+ #[test]
+ fn a_numeric_field_is_dropped_from_multi_match_term_and_phrase() {
+ let mapping = json!({
+ "p-code": { "mappings": { "properties": {
+ "name": { "type": "keyword" },
+ "code": { "type": "text" },
+ "defs": { "type": "text" },
+ "title": { "type": "text" },
+ "body": { "type": "text" }
+ }}},
+ "p-metrics": { "mappings": { "properties": {
+ "code": { "type": "long" },
+ "title": { "type": "integer" },
+ "line": { "type": "long" }
+ }}}
+ });
+ let names = mapped_property_names(&mapping).expect("mapping");
+ let numeric = numeric_property_names(&mapping);
+ assert!(numeric.contains("code") && numeric.contains("title") && numeric.contains("line"));
+ assert!(!numeric.contains("name"), "keyword is not numeric");
+ assert!(
+ numeric_property_names(&json!({
+ "only": { "mappings": { "properties": {
+ "half": { "type": "half_float" },
+ "scaled": { "type": "scaled_float" },
+ "unsigned": { "type": "unsigned_long" }
+ }}}
+ }))
+ .len()
+ == 3
+ );
+
+ let body = search_query_body_for_mapping("timeout", 5, Some(&names), Some(&numeric));
+ let query = body["query"].to_string();
+ assert!(!query.contains("code"), "{query}");
+ assert!(!query.contains("title"), "{query}");
+ assert!(query.contains("name"), "{query}");
+ assert!(query.contains("defs"), "{query}");
+ assert!(query.contains("body"), "{query}");
+ let fields = body["query"]["bool"]["should"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .find_map(|clause| clause.get("multi_match"))
+ .unwrap()["fields"]
+ .as_array()
+ .unwrap();
+ assert!(fields
+ .iter()
+ .all(|f| f.as_str().unwrap().split('^').next() != Some("code")));
+ assert!(fields
+ .iter()
+ .all(|f| f.as_str().unwrap().split('^').next() != Some("title")));
+
+ let prose = json!({
+ "logs": { "mappings": { "properties": {
+ "message": { "type": "double" }
+ }}}
+ });
+ let prose_names = mapped_property_names(&prose).unwrap();
+ let prose_numeric = numeric_property_names(&prose);
+ let none =
+ search_query_body_for_mapping("timeout", 5, Some(&prose_names), Some(&prose_numeric));
+ assert!(none["query"].get("match_none").is_some(), "{none}");
+ }
}