1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct SearchDocument {
5 pub id: String,
6 #[serde(skip_serializing_if = "Option::is_none")]
7 pub index: Option<String>,
8 #[serde(flatten)]
9 pub fields: serde_json::Map<String, serde_json::Value>,
10}
11
12impl SearchDocument {
13 pub fn new(id: impl Into<String>, fields: impl Into<serde_json::Value>) -> crate::Result<Self> {
14 let fields = match fields.into() {
15 serde_json::Value::Object(fields) => fields,
16 _ => {
17 return Err(crate::ScoutError::InvalidResult(
18 "document fields must be a JSON object".to_string(),
19 ))
20 }
21 };
22 Ok(Self {
23 id: id.into(),
24 index: None,
25 fields,
26 })
27 }
28
29 pub fn get(&self, key: &str) -> Option<&serde_json::Value> {
30 self.fields.get(key)
31 }
32
33 pub fn set(&mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) {
34 self.fields.insert(key.into(), value.into());
35 }
36}