elasticsearch_dsl/search/queries/span/
span_field_masking_query.rs

1use crate::util::*;
2use crate::{Query, SpanQuery};
3
4/// Wrapper to allow span queries to participate in composite single-field span queries by
5/// _lying_ about their search field. The span field masking query maps to Lucene’s
6/// `SpanFieldMaskingQuery`
7///
8/// This can be used to support queries like `span-near` or `span-or` across different fields,
9/// which is not ordinarily permitted.
10///
11/// Span field masking query is invaluable in conjunction with **multi-fields** when same content
12/// is indexed with multiple analyzers. For instance we could index a field with the standard
13/// analyzer which breaks text up into words, and again with the english analyzer which stems words
14/// into their root form.
15///
16/// Note: as span field masking query returns the masked field, scoring will be done using the
17/// norms of the field name supplied. This may lead to unexpected scoring behavior.
18///
19/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-field-masking-query.html>
20#[derive(Debug, Clone, PartialEq, 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);
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn serialization() {
52        assert_serialize_query(
53            Query::span_field_masking(Query::span_term("test", 1234), "test"),
54            json!({
55                "span_field_masking": {
56                    "query": {
57                        "span_term": {
58                            "test": {
59                                "value": 1234
60                            }
61                        }
62                    },
63                    "field": "test"
64                }
65            }),
66        );
67
68        assert_serialize_query(
69            Query::span_field_masking(Query::span_term("test", 1234), "test"),
70            json!({
71                "span_field_masking": {
72                    "query": {
73                        "span_term": {
74                            "test": {
75                                "value": 1234
76                            }
77                        }
78                    },
79                    "field": "test"
80                }
81            }),
82        );
83    }
84}