elasticsearch_dsl/search/queries/span/
span_first_query.rs

1use crate::util::*;
2use crate::{Query, SpanQuery};
3use serde::Serialize;
4
5/// Matches spans near the beginning of a field. The span first query maps to Lucene
6/// `SpanFirstQuery`. <br/>
7/// The `match` clause can be any other span type query. The `end` controls the maximum end
8/// position permitted in a match.
9///
10/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html>
11#[derive(Debug, Clone, PartialEq, 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);
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn serialization() {
41        assert_serialize_query(
42            Query::span_first(Query::span_term("test", 1234), 10),
43            json!({
44                "span_first": {
45                    "match": {
46                        "span_term": {
47                            "test": {
48                                "value": 1234
49                            }
50                        }
51                    },
52                    "end": 10
53                }
54            }),
55        );
56
57        assert_serialize_query(
58            Query::span_first(Query::span_term("test", 1234), 10),
59            json!({
60                "span_first": {
61                    "match": {
62                        "span_term": {
63                            "test": {
64                                "value": 1234
65                            }
66                        }
67                    },
68                    "end": 10
69                }
70            }),
71        );
72    }
73}