elasticsearch_dsl/search/queries/span/
span_within_query.rs

1use super::SpanQuery;
2use crate::util::*;
3use crate::Query;
4use serde::Serialize;
5
6/// Returns matches which are enclosed inside another span query. The span within query maps to
7/// Lucene `SpanWithinQuery`.
8///
9/// The `big` and `little` clauses can be any span type query. Matching spans from `little` that
10/// are enclosed within `big` are returned.
11///
12/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html>
13#[derive(Debug, Clone, PartialEq, Serialize)]
14#[serde(remote = "Self")]
15pub struct SpanWithinQuery {
16    big: Box<SpanQuery>,
17    little: Box<SpanQuery>,
18}
19
20impl Query {
21    /// Creates an instance of [`SpanWithinQuery`]
22    pub fn span_within<T, U>(little: T, big: U) -> SpanWithinQuery
23    where
24        T: Into<SpanQuery>,
25        U: Into<SpanQuery>,
26    {
27        SpanWithinQuery {
28            little: Box::new(little.into()),
29            big: Box::new(big.into()),
30        }
31    }
32}
33
34impl ShouldSkip for SpanWithinQuery {}
35
36serialize_with_root!("span_within": SpanWithinQuery);
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn serialization() {
44        assert_serialize_query(
45            Query::span_within(
46                Query::span_term("little", 1234),
47                Query::span_term("big", 4321),
48            ),
49            json!({
50                "span_within": {
51                    "little": {
52                        "span_term": {
53                            "little": {
54                                "value": 1234
55                            }
56                        }
57                    },
58                    "big": {
59                        "span_term": {
60                            "big": {
61                                "value": 4321
62                            }
63                        }
64                    }
65                }
66            }),
67        );
68
69        assert_serialize_query(
70            Query::span_within(
71                Query::span_term("little", 1234),
72                Query::span_term("big", 4321),
73            ),
74            json!({
75                "span_within": {
76                    "little": {
77                        "span_term": {
78                            "little": {
79                                "value": 1234
80                            }
81                        }
82                    },
83                    "big": {
84                        "span_term": {
85                            "big": {
86                                "value": 4321
87                            }
88                        }
89                    }
90                }
91            }),
92        );
93    }
94}