Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(elasticsearch): strip metadata fields from writes

t8y29fbafa9a6dc3
diff --git a/crates/dbx-core/Cargo.toml b/crates/dbx-core/Cargo.toml
index 1e7b2e75d9..d1d0c8d157 100644
--- a/crates/dbx-core/Cargo.toml
+++ b/crates/dbx-core/Cargo.toml
@@ -10,7 +10,7 @@ duckdb-bundled = ["duckdb/bundled"]
[dependencies]
serde = { version = "1.0", features = ["derive"] }
-serde_json = "1.0"
+serde_json = { version = "1.0", features = ["preserve_order"] }
regex = "1"
rayon = "1"
percent-encoding = "2"
diff --git a/crates/dbx-core/src/db/elasticsearch_driver.rs b/crates/dbx-core/src/db/elasticsearch_driver.rs
index 20475bca8b..794aa7ec46 100644
--- a/crates/dbx-core/src/db/elasticsearch_driver.rs
+++ b/crates/dbx-core/src/db/elasticsearch_driver.rs
@@ -596,7 +596,7 @@ fn elasticsearch_sort_from_document_sort(sort: Option<&str>) -> Result<serde_jso
}
pub async fn insert_document(client: &EsClient, index: &str, doc_json: &str) -> Result<String, String> {
- let doc: serde_json::Value = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
+ let doc = elasticsearch_document_body_from_json(doc_json)?;
let path = format!("/{}/_doc?refresh=true", index);
let resp = client.post(&path).json(&doc).send().await.map_err(|e| format!("Elasticsearch request failed: {e}"))?;
@@ -611,7 +611,7 @@ pub async fn insert_document(client: &EsClient, index: &str, doc_json: &str) ->
}
pub async fn update_document(client: &EsClient, index: &str, id: &str, doc_json: &str) -> Result<u64, String> {
- let doc: serde_json::Value = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
+ let doc = elasticsearch_document_body_from_json(doc_json)?;
let path = format!("/{}/_doc/{}?refresh=true", index, id);
let resp = client.put(&path).json(&doc).send().await.map_err(|e| format!("Elasticsearch request failed: {e}"))?;
@@ -624,6 +624,14 @@ pub async fn update_document(client: &EsClient, index: &str, id: &str, doc_json:
Ok(1)
}
+fn elasticsearch_document_body_from_json(doc_json: &str) -> Result<serde_json::Value, String> {
+ let mut doc: serde_json::Value = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
+ if let serde_json::Value::Object(map) = &mut doc {
+ map.remove("_id");
+ }
+ Ok(doc)
+}
+
pub async fn delete_document(client: &EsClient, index: &str, id: &str) -> Result<u64, String> {
let path = format!("/{}/_doc/{}?refresh=true", index, id);
let resp = client.delete(&path).send().await.map_err(|e| format!("Elasticsearch request failed: {e}"))?;
@@ -1540,4 +1548,18 @@ mod tests {
assert_eq!(response.hits.total.value(), 5);
}
+
+ #[test]
+ fn document_body_removes_elasticsearch_id_metadata() {
+ let doc = super::elasticsearch_document_body_from_json(r#"{"_id":"abc","name":"Alice"}"#).unwrap();
+
+ assert_eq!(doc, json!({ "name": "Alice" }));
+ }
+
+ #[test]
+ fn document_body_preserves_user_field_order() {
+ let doc = super::elasticsearch_document_body_from_json(r#"{"z":1,"_id":"abc","a":2}"#).unwrap();
+
+ assert_eq!(serde_json::to_string(&doc).unwrap(), r#"{"z":1,"a":2}"#);
+ }
}