Skip to main content

opensearch_dsl/search/queries/span/
span_field_masking_query.rs

1use crate::{util::*, Query, SpanQuery};
2
3/// Wrapper to allow span queries to participate in composite single-field span
4/// queries by _lying_ about their search field. The span field masking query
5/// maps to Lucene’s `SpanFieldMaskingQuery`
6///
7/// This can be used to support queries like `span-near` or `span-or` across
8/// different fields, which is not ordinarily permitted.
9///
10/// Span field masking query is invaluable in conjunction with **multi-fields**
11/// when same content is indexed with multiple analyzers. For instance we could
12/// index a field with the standard analyzer which breaks text up into words,
13/// and again with the english analyzer which stems words into their root form.
14///
15/// Note: as span field masking query returns the masked field, scoring will be
16/// done using the norms of the field name supplied. This may lead to unexpected
17/// scoring behavior.
18///
19/// <https://www.elastic.co/guide/en/opensearch/reference/current/query-dsl-span-field-masking-query.html>
20#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
21#[serde(remote = "Self")]
22pub struct SpanFieldMaskingQuery {
23    query: Box<SpanQuery>,
24    field: String,
25}
26
27impl Query {
28    /// Creates an instance of [`SpanFieldMaskingQuery`]
29    #[allow(unused)]
30    pub fn span_field_masking<Q, F>(query: Q, field: F) -> SpanFieldMaskingQuery
31    where
32        Q: Into<SpanQuery>,
33        F: ToString,
34    {
35        SpanFieldMaskingQuery {
36            query: Box::new(query.into()),
37            field: field.to_string(),
38        }
39    }
40}
41
42impl ShouldSkip for SpanFieldMaskingQuery {}
43
44serialize_with_root!("span_field_masking": SpanFieldMaskingQuery);
45deserialize_with_root!("span_field_masking": SpanFieldMaskingQuery);
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn serialization() {
53        assert_serialize_query(
54            Query::span_field_masking(Query::span_term("test", 1234), "test"),
55            json!({
56                "span_field_masking": {
57                    "query": {
58                        "span_term": {
59                            "test": {
60                                "value": 1234
61                            }
62                        }
63                    },
64                    "field": "test"
65                }
66            }),
67        );
68
69        assert_serialize_query(
70            Query::span_field_masking(Query::span_term("test", 1234), "test"),
71            json!({
72                "span_field_masking": {
73                    "query": {
74                        "span_term": {
75                            "test": {
76                                "value": 1234
77                            }
78                        }
79                    },
80                    "field": "test"
81                }
82            }),
83        );
84    }
85}