fix(elasticsearch): preserve document numeric precision

This commit is contained in:
t8y2 committed 2026-07-15 16:07:05 +08:00
1 parent 977f47a65f
commit 7a434b0674
9 files changed
+203 -34

No files matched your search

+29 -1
View File
@@ -408,6 +408,10 @@ pub async fn find_documents(
let result: SearchResponse = resp.json().await.map_err(|e| format!("Elasticsearch parse error: {e}"))?;
search_response_to_document_result(result)
}
fn search_response_to_document_result(result: SearchResponse) -> Result<MongoDocumentResult, String> {
let documents: Vec<serde_json::Value> = result
.hits
.hits
@@ -425,7 +429,16 @@ pub async fn find_documents(
})
.collect();
Ok(MongoDocumentResult { documents, total: result.hits.total.value() })
// Keep the exact JSON numeric tokens alongside the compatibility value tree.
// Desktop IPC and browser JSON parsing otherwise coerce ES long values through
// JavaScript Number before the document editor can serialize them again.
let raw_documents = documents
.iter()
.map(serde_json::to_string)
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("Elasticsearch document serialization failed: {e}"))?;
Ok(MongoDocumentResult { documents, raw_documents: Some(raw_documents), total: result.hits.total.value() })
}
fn build_find_documents_body(
@@ -1743,6 +1756,21 @@ mod tests {
assert_eq!(response.hits.hits[0].routing.as_deref(), Some("tenant-1"));
}
#[test]
fn preserves_long_literals_in_document_transport_json() {
let response: SearchResponse = serde_json::from_str(
r#"{"hits":{"total":{"value":1,"relation":"eq"},"hits":[{"_id":"doc-1","_source":{"id":2018551659033767937,"string_id":"2018551659033767937","legacy":{"$numberLong":"2018551659033767937"}}}]}}"#,
)
.unwrap();
let result = super::search_response_to_document_result(response).unwrap();
assert_eq!(
result.raw_documents.as_deref(),
Some(&[r#"{"id":2018551659033767937,"string_id":"2018551659033767937","legacy":{"$numberLong":"2018551659033767937"},"_id":"doc-1"}"#.to_string()][..])
);
}
#[test]
fn parses_search_response_rows_with_routing_metadata() {
let result = super::parse_elasticsearch_response(
+11 -5
View File
@@ -15,6 +15,8 @@ use std::{collections::HashSet, time::Duration};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MongoDocumentResult {
pub documents: Vec<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub raw_documents: Option<Vec<String>>,
pub total: u64,
}
@@ -589,7 +591,7 @@ pub async fn find_documents(
documents.push(bson_to_json(&Bson::Document(doc)));
}
Ok(MongoDocumentResult { documents, total })
Ok(MongoDocumentResult { documents, raw_documents: None, total })
}
#[derive(Debug, Default, Deserialize)]
@@ -725,7 +727,7 @@ pub async fn find_documents_extended_json(
documents.push(Bson::Document(doc).into_relaxed_extjson());
}
Ok(MongoDocumentResult { documents, total })
Ok(MongoDocumentResult { documents, raw_documents: None, total })
}
pub async fn aggregate_documents(
@@ -755,7 +757,7 @@ pub async fn aggregate_documents(
if documents.len() > max_rows {
documents.truncate(max_rows);
}
Ok(MongoDocumentResult { documents, total })
Ok(MongoDocumentResult { documents, raw_documents: None, total })
}
pub async fn create_index(
@@ -1111,8 +1113,12 @@ fn find_and_modify_array_filters(
fn single_document_result(document: Option<Document>) -> MongoDocumentResult {
match document {
Some(document) => MongoDocumentResult { documents: vec![bson_to_json(&Bson::Document(document))], total: 1 },
None => MongoDocumentResult { documents: Vec::new(), total: 0 },
Some(document) => MongoDocumentResult {
documents: vec![bson_to_json(&Bson::Document(document))],
raw_documents: None,
total: 1,
},
None => MongoDocumentResult { documents: Vec::new(), raw_documents: None, total: 0 },
}
}
+6 -2
View File
@@ -520,7 +520,11 @@ pub async fn find_documents(
Value::Object(map)
})
.collect();
return Ok(crate::db::mongo_driver::MongoDocumentResult { documents, total: result.affected_rows });
return Ok(crate::db::mongo_driver::MongoDocumentResult {
documents,
raw_documents: None,
total: result.affected_rows,
});
}
let query = match client.kind {
@@ -562,7 +566,7 @@ pub async fn find_documents(
Value::Object(map)
})
.collect();
Ok(crate::db::mongo_driver::MongoDocumentResult { documents, total: result.affected_rows })
Ok(crate::db::mongo_driver::MongoDocumentResult { documents, raw_documents: None, total: result.affected_rows })
}
pub async fn execute_rest_query(client: &VectorClient, input: &str) -> Result<QueryResult, String> {