elasticsearch_dsl/search/queries/span/
span_containing_query.rs

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