feat: trace_id i64→i32 fix, OCR search filter, Rule1 OCR trigger
- Fix trace_id SQL type mismatch: i64 → i32 in profile.rs structs and queries - Add /ocr /asr /asrx search filter suffix to keyword/semantic/smart search - Fix fetch_ocr_texts to handle both OCR data formats (string array + object array) - Add has_ocr trigger for Rule1 chunk ingestion in job_worker.rs - Add search_bm25 chunk_type filter parameter - Fix single-frame video clip duration (0-frame → 1-frame minimum) - Re-run Rule1 for 14 files with stuck OCR in pre_chunks
This commit is contained in:
+119
-38
@@ -1714,41 +1714,67 @@ async fn delete_trace(
|
||||
let qdrant = crate::core::db::qdrant_db::QdrantDb::new();
|
||||
|
||||
if hard_delete {
|
||||
// Hard delete: actually remove from Qdrant and TKG
|
||||
// Hard delete: move to _faces_trash, then remove from _faces and TKG
|
||||
let filter = serde_json::json!({
|
||||
"must": [
|
||||
{"key": "file_uuid", "match": {"value": file_uuid}},
|
||||
{"key": "file_uuid", "match": {"value": &file_uuid}},
|
||||
{"key": "trace_id", "match": {"value": trace_id}}
|
||||
]
|
||||
});
|
||||
|
||||
let deleted_count = match qdrant
|
||||
.delete_points_by_filter("_faces", filter.clone())
|
||||
// 0. Scroll points with vectors for trash backup
|
||||
let trashed_points = qdrant
|
||||
.scroll_all_points_with_vectors("_faces", filter.clone(), 1000)
|
||||
.await
|
||||
{
|
||||
Ok(count) => {
|
||||
tracing::info!(
|
||||
"[delete_trace] Hard deleted {} face points from Qdrant for trace {} in {}",
|
||||
count,
|
||||
trace_id,
|
||||
file_uuid
|
||||
);
|
||||
count
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("[delete_trace] Failed to delete Qdrant points: {}", e);
|
||||
return Json(serde_json::json!({
|
||||
"success": false,
|
||||
"error": format!("Failed to delete Qdrant points: {}", e)
|
||||
}));
|
||||
}
|
||||
};
|
||||
.unwrap_or_default();
|
||||
|
||||
// 1. Copy to _faces_trash
|
||||
if !trashed_points.is_empty() {
|
||||
let trash_batch: Vec<(u64, Vec<f32>, serde_json::Value)> = trashed_points
|
||||
.iter()
|
||||
.filter_map(|p| {
|
||||
let id = p["id"].as_u64()?;
|
||||
let vec_arr = p["vector"].as_array()?;
|
||||
let vector: Vec<f32> = vec_arr.iter().filter_map(|v| v.as_f64().map(|f| f as f32)).collect();
|
||||
let mut payload = p["payload"].clone();
|
||||
if let Some(obj) = payload.as_object_mut() {
|
||||
obj.insert("trashed_at".to_string(), serde_json::json!(chrono::Utc::now().to_rfc3339()));
|
||||
}
|
||||
Some((id, vector, payload))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let ref_batch: Vec<(u64, &[f32], Option<serde_json::Value>)> = trash_batch
|
||||
.iter()
|
||||
.map(|(id, vec, pl)| (*id, vec.as_slice(), Some(pl.clone())))
|
||||
.collect();
|
||||
|
||||
if let Err(e) = qdrant.upsert_vectors_batch("_faces_trash", &ref_batch).await {
|
||||
tracing::error!("[delete_trace] Failed to copy to trash: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Save TKG nodes before deleting
|
||||
let nodes_table = crate::core::db::schema::table_name("tkg_nodes");
|
||||
let ext_id_face = format!("face_track_{}", trace_id);
|
||||
let ext_id_trace = format!("trace_{}", trace_id);
|
||||
|
||||
let tkg_result = sqlx::query(&format!(
|
||||
let tkg_snapshot: Vec<(String, String, serde_json::Value)> = sqlx::query_as(&format!(
|
||||
"SELECT external_id, label, properties FROM {} WHERE file_uuid = $1 AND node_type = 'face_track' AND (external_id = $2 OR external_id = $3)",
|
||||
nodes_table
|
||||
))
|
||||
.bind(&file_uuid)
|
||||
.bind(&ext_id_face)
|
||||
.bind(&ext_id_trace)
|
||||
.fetch_all(state.db.pool())
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
|
||||
// 3. Delete from _faces
|
||||
let _ = qdrant.delete_points_by_filter("_faces", filter.clone()).await;
|
||||
|
||||
// 4. Delete TKG nodes
|
||||
let _ = sqlx::query(&format!(
|
||||
"DELETE FROM {} WHERE file_uuid = $1 AND node_type = 'face_track' AND (external_id = $2 OR external_id = $3)",
|
||||
nodes_table
|
||||
))
|
||||
@@ -1758,21 +1784,33 @@ async fn delete_trace(
|
||||
.execute(state.db.pool())
|
||||
.await;
|
||||
|
||||
let tkg_deleted = match tkg_result {
|
||||
Ok(r) => r.rows_affected(),
|
||||
Err(e) => {
|
||||
tracing::error!("[delete_trace] Failed to delete TKG nodes: {}", e);
|
||||
0
|
||||
}
|
||||
};
|
||||
// 5. Save operation snapshot
|
||||
let op_table = crate::core::db::schema::table_name("trace_operation_history");
|
||||
let before_snapshot = serde_json::json!({
|
||||
"trace_id": trace_id,
|
||||
"tkg_nodes": tkg_snapshot.iter().map(|(ext_id, label, props)| {
|
||||
serde_json::json!({
|
||||
"external_id": ext_id,
|
||||
"label": label,
|
||||
"properties": props,
|
||||
})
|
||||
}).collect::<Vec<_>>(),
|
||||
});
|
||||
let _ = sqlx::query(&format!(
|
||||
"INSERT INTO {} (file_uuid, operation, before_snapshot) VALUES ($1, 'hard_delete', $2)",
|
||||
op_table
|
||||
))
|
||||
.bind(&file_uuid)
|
||||
.bind(&before_snapshot)
|
||||
.execute(state.db.pool())
|
||||
.await;
|
||||
|
||||
Json(serde_json::json!({
|
||||
"success": true,
|
||||
"file_uuid": file_uuid,
|
||||
"trace_id": trace_id,
|
||||
"hard_delete": true,
|
||||
"deleted_face_points": deleted_count,
|
||||
"deleted_tkg_nodes": tkg_deleted
|
||||
"trashed_points": trashed_points.len(),
|
||||
}))
|
||||
} else {
|
||||
// Soft delete: mark as deleted in Qdrant and TKG
|
||||
@@ -1860,6 +1898,45 @@ async fn merge_trace(
|
||||
|
||||
let qdrant = crate::core::db::qdrant_db::QdrantDb::new();
|
||||
|
||||
// Step 0: Snapshot source trace state before merge
|
||||
let src_filter_all = serde_json::json!({
|
||||
"must": [
|
||||
{"key": "file_uuid", "match": {"value": &file_uuid}},
|
||||
{"key": "trace_id", "match": {"value": trace_id}}
|
||||
]
|
||||
});
|
||||
let qdrant_points = qdrant
|
||||
.scroll_all_points_with_vectors("_faces", src_filter_all.clone(), 1000)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
|
||||
let nodes_table = crate::core::db::schema::table_name("tkg_nodes");
|
||||
let ext_id_face = format!("face_track_{}", trace_id);
|
||||
let ext_id_trace = format!("trace_{}", trace_id);
|
||||
let tkg_nodes: Vec<(String, String, serde_json::Value)> = sqlx::query_as(&format!(
|
||||
"SELECT external_id, label, properties FROM {} WHERE file_uuid = $1 AND node_type = 'face_track' AND (external_id = $2 OR external_id = $3)",
|
||||
nodes_table
|
||||
))
|
||||
.bind(&file_uuid)
|
||||
.bind(&ext_id_face)
|
||||
.bind(&ext_id_trace)
|
||||
.fetch_all(state.db.pool())
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
|
||||
let before_snapshot = serde_json::json!({
|
||||
"source_trace_id": trace_id,
|
||||
"target_trace_id": target_trace_id,
|
||||
"qdrant_points": qdrant_points,
|
||||
"tkg_nodes": tkg_nodes.iter().map(|(ext_id, label, props)| {
|
||||
serde_json::json!({
|
||||
"external_id": ext_id,
|
||||
"label": label,
|
||||
"properties": props,
|
||||
})
|
||||
}).collect::<Vec<_>>(),
|
||||
});
|
||||
|
||||
// Step 1: Update all face points from source trace to target trace
|
||||
let src_filter = serde_json::json!({
|
||||
"must": [
|
||||
@@ -1895,10 +1972,6 @@ async fn merge_trace(
|
||||
};
|
||||
|
||||
// Step 2: Delete source TKG nodes
|
||||
let nodes_table = crate::core::db::schema::table_name("tkg_nodes");
|
||||
let ext_id_face = format!("face_track_{}", trace_id);
|
||||
let ext_id_trace = format!("trace_{}", trace_id);
|
||||
|
||||
let tkg_result = sqlx::query(&format!(
|
||||
"DELETE FROM {} WHERE file_uuid = $1 AND node_type = 'face_track' AND (external_id = $2 OR external_id = $3)",
|
||||
nodes_table
|
||||
@@ -1917,8 +1990,16 @@ async fn merge_trace(
|
||||
}
|
||||
};
|
||||
|
||||
// Step 3: Rebuild TKG for the target trace
|
||||
// (The TKG rebuild will pick up the merged points automatically)
|
||||
// Step 3: Save operation snapshot for undo
|
||||
let op_table = crate::core::db::schema::table_name("trace_operation_history");
|
||||
let _ = sqlx::query(&format!(
|
||||
"INSERT INTO {} (file_uuid, operation, before_snapshot) VALUES ($1, 'merge_trace', $2)",
|
||||
op_table
|
||||
))
|
||||
.bind(&file_uuid)
|
||||
.bind(&before_snapshot)
|
||||
.execute(state.db.pool())
|
||||
.await;
|
||||
|
||||
Json(serde_json::json!({
|
||||
"success": true,
|
||||
|
||||
Reference in New Issue
Block a user