xz_rag/types/chunk.rs
1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4/// Document chunk with metadata and optional embedding.
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Chunk {
7 /// Unique chunk identifier.
8 pub id: String,
9 /// ID of the parent document.
10 pub document_id: String,
11 /// Index of this chunk within the document.
12 pub chunk_index: u32,
13 /// Text content of the chunk.
14 pub content: String,
15 /// Optional summary of the chunk content.
16 pub summary: Option<String>,
17 /// Metadata associated with this chunk.
18 pub metadata: ChunkMetadata,
19 /// Optional embedding vector for vector search.
20 pub embedding: Option<Vec<f32>>,
21 /// Timestamp when the chunk was created (milliseconds since epoch).
22 pub created_at: u64,
23 /// Optional expiry timestamp (milliseconds since epoch).
24 pub expires_at: Option<u64>,
25}
26
27/// Metadata attached to each chunk.
28#[derive(Debug, Clone, Serialize, Deserialize, Default)]
29pub struct ChunkMetadata {
30 /// Source of the document (e.g. file path, URL).
31 pub source: Option<String>,
32 /// Title of the parent document.
33 pub document_title: Option<String>,
34 /// Author of the document.
35 pub author: Option<String>,
36 /// Timestamp when the chunk was created (milliseconds since epoch).
37 pub created_at: Option<u64>,
38 /// List of tags associated with the chunk.
39 pub tags: Vec<String>,
40 /// Namespace for scoping the chunk.
41 pub namespace: Option<String>,
42 /// Extra key-value metadata.
43 pub extra: HashMap<String, String>,
44}
45
46impl Chunk {
47 /// Create a new chunk with the given id, document_id, content, and index.
48 pub fn new(id: String, document_id: String, content: String, chunk_index: u32) -> Self {
49 Self {
50 id,
51 document_id,
52 chunk_index,
53 content,
54 summary: None,
55 metadata: ChunkMetadata::default(),
56 embedding: None,
57 created_at: std::time::SystemTime::now()
58 .duration_since(std::time::UNIX_EPOCH)
59 .unwrap_or_default()
60 .as_millis() as u64,
61 expires_at: None,
62 }
63 }
64}