elasticsearch_dsl/search/queries/custom/
json_query.rs

1use crate::search::*;
2use crate::util::*;
3
4/// Raw JSON query for something not yet supported.
5///
6/// To create JSON query:
7/// ```
8/// # use elasticsearch_dsl::queries::*;
9/// # use elasticsearch_dsl::queries::params::*;
10/// # let query =
11/// Query::json(serde_json::json!({ "term": { "user": "username" } }));
12/// ```
13/// **NOTE**: This is fallible and can lead to incorrect queries and
14/// rejected search requests, use ar your own risk.
15#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
16pub struct JsonQuery(serde_json::Value);
17
18impl Query {
19    /// Creates an instance of [`JsonQuery`]
20    ///
21    /// - `query` - raw JSON query
22    pub fn json(query: serde_json::Value) -> JsonQuery {
23        JsonQuery(query)
24    }
25}
26
27impl From<serde_json::Value> for Query {
28    fn from(value: serde_json::Value) -> Self {
29        Self::Json(JsonQuery(value))
30    }
31}
32
33impl From<serde_json::Value> for JsonQuery {
34    fn from(value: serde_json::Value) -> Self {
35        Self(value)
36    }
37}
38
39impl ShouldSkip for JsonQuery {
40    fn should_skip(&self) -> bool {
41        !self.0.is_object()
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn serialization() {
51        assert_serialize_query(
52            Query::json(json!({ "term": { "user": "username" } })),
53            json!({ "term": { "user": "username" } }),
54        );
55    }
56}