Skip to main content

opensearch_dsl/search/queries/span/
span_first_query.rs

1use serde::Serialize;
2
3use crate::{util::*, Query, SpanQuery};
4
5/// Matches spans near the beginning of a field. The span first query maps to
6/// Lucene `SpanFirstQuery`. <br/>
7/// The `match` clause can be any other span type query. The `end` controls the
8/// maximum end position permitted in a match.
9///
10/// <https://www.elastic.co/guide/en/opensearch/reference/current/query-dsl-span-first-query.html>
11#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
12#[serde(remote = "Self")]
13pub struct SpanFirstQuery {
14    r#match: Box<SpanQuery>,
15    end: u32,
16}
17
18impl Query {
19    /// Creates an instance of [`SpanFirstQuery`]
20    pub fn span_first<T>(r#match: T, end: u32) -> SpanFirstQuery
21    where
22        T: Into<SpanQuery>,
23    {
24        SpanFirstQuery {
25            r#match: Box::new(r#match.into()),
26            end,
27        }
28    }
29}
30
31impl ShouldSkip for SpanFirstQuery {}
32
33serialize_with_root!("span_first": SpanFirstQuery);
34deserialize_with_root!("span_first": SpanFirstQuery);
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn serialization() {
42        assert_serialize_query(
43            Query::span_first(Query::span_term("test", 1234), 10),
44            json!({
45                "span_first": {
46                    "match": {
47                        "span_term": {
48                            "test": {
49                                "value": 1234
50                            }
51                        }
52                    },
53                    "end": 10
54                }
55            }),
56        );
57
58        assert_serialize_query(
59            Query::span_first(Query::span_term("test", 1234), 10),
60            json!({
61                "span_first": {
62                    "match": {
63                        "span_term": {
64                            "test": {
65                                "value": 1234
66                            }
67                        }
68                    },
69                    "end": 10
70                }
71            }),
72        );
73    }
74}