1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use super::{Explanation, NestedIdentity, Source};
use crate::{util::ShouldSkip, InnerHitsResult, Map};
use serde::de::DeserializeOwned;
use serde_json::Value;

/// Represents a single matched document
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Hit {
    /// Search explanation
    #[serde(
        skip_serializing_if = "ShouldSkip::should_skip",
        rename = "_explanation"
    )]
    pub explanation: Option<Explanation>,

    /// Document index
    #[serde(
        default,
        skip_serializing_if = "ShouldSkip::should_skip",
        rename = "_index"
    )]
    pub index: String,

    /// Document ID
    #[serde(
        default,
        skip_serializing_if = "ShouldSkip::should_skip",
        rename = "_id"
    )]
    pub id: String,

    /// Document score. [`None`] when documents are implicitly sorted by a
    /// field other than `_score`
    #[serde(skip_serializing_if = "ShouldSkip::should_skip", rename = "_score")]
    pub score: Option<f32>,

    /// Nested document identity
    #[serde(skip_serializing_if = "ShouldSkip::should_skip", rename = "_nested")]
    pub nested: Option<NestedIdentity>,

    /// Document source
    #[serde(
        skip_serializing_if = "ShouldSkip::should_skip",
        rename = "_source",
        default
    )]
    pub source: Source,

    /// Highlighted matches
    #[serde(skip_serializing_if = "ShouldSkip::should_skip", default)]
    pub highlight: Map<String, Vec<String>>,

    /// Inner hits
    #[serde(skip_serializing_if = "ShouldSkip::should_skip", default)]
    pub inner_hits: Map<String, InnerHitsResult>,

    /// Matched queries
    #[serde(skip_serializing_if = "ShouldSkip::should_skip", default)]
    pub matched_queries: Vec<String>,

    /// Values document was sorted by
    #[serde(skip_serializing_if = "ShouldSkip::should_skip", default)]
    pub sort: Vec<Value>,

    /// Field values for the documents. Need to be specified in the request
    #[serde(skip_serializing_if = "ShouldSkip::should_skip", default)]
    pub fields: Map<String, Value>,
}

impl Hit {
    /// Parses document source into a concrete type
    pub fn source<T>(&self) -> Result<T, serde_json::Error>
    where
        T: DeserializeOwned,
    {
        self.source.parse()
    }
}