Skip to main content

fetchium_core/index/
document.rs

1//! Indexed document schema for the local hybrid search index (Phase 5, PRD §28).
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6/// A document stored in the local index.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct IndexedDocument {
9    /// Unique numeric ID (used as key in the HNSW vector index).
10    pub id: u64,
11    /// Source URL.
12    pub url: String,
13    /// Page title.
14    pub title: String,
15    /// Full extracted text content.
16    pub content: String,
17    /// Registered domain (for dedup and authority scoring).
18    pub domain: String,
19    /// When this document was indexed.
20    pub fetched_at: DateTime<Utc>,
21    /// SHA-256 of the extracted content (change-detection).
22    pub content_hash: String,
23    /// Pre-computed embedding, present after `fetchium index build`.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub embedding: Option<Vec<f32>>,
26}
27
28impl IndexedDocument {
29    /// Check if this document has an embedding.
30    pub fn is_embedded(&self) -> bool {
31        self.embedding.is_some()
32    }
33}
34
35/// Summary statistics for the local index.
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct IndexStats {
38    /// Total number of indexed documents.
39    pub document_count: usize,
40    /// Number of documents with embeddings.
41    pub embedded_count: usize,
42    /// Approximate size of the index on disk in bytes.
43    pub index_size_bytes: u64,
44    /// Whether the HNSW vector index is built and ready.
45    pub vector_index_ready: bool,
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn is_embedded_returns_correct_values() {
54        let mut doc = IndexedDocument {
55            id: 1,
56            url: "https://example.com".into(),
57            title: "Test".into(),
58            content: "content".into(),
59            domain: "example.com".into(),
60            fetched_at: Utc::now(),
61            content_hash: "abc".into(),
62            embedding: None,
63        };
64        assert!(!doc.is_embedded());
65        doc.embedding = Some(vec![0.0; 384]);
66        assert!(doc.is_embedded());
67    }
68}