1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
use crate::search::*;
use crate::util::*;
/// Filters documents based on a provided
/// [script](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html).
/// The script query is typically used in a
/// [filter context](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html).
///
/// To create script query:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # let query =
/// Query::script(Script::source("return doc['amount'].value < 10;"));
/// ```
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html>
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(remote = "Self")]
pub struct ScriptQuery {
script: Script,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
impl Query {
/// Creates an instance of [`ScriptQuery`]
///
/// - `script` - Contains a script to run as a query. This script must
/// return a boolean value, `true` or `false`
pub fn script(script: Script) -> ScriptQuery {
ScriptQuery {
script,
boost: None,
_name: None,
}
}
}
impl ScriptQuery {
add_boost_and_name!();
}
impl ShouldSkip for ScriptQuery {}
serialize_with_root!("script": ScriptQuery);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialization() {
assert_serialize_query(
Query::script(
Script::source("doc['numberOfCommits'].value > params.param1").param("param1", 50),
)
.name("_named_query")
.boost(1.1),
json!({
"script": {
"_name": "_named_query",
"boost": 1.1,
"script": {
"source": "doc['numberOfCommits'].value > params.param1",
"params": {
"param1": 50
}
}
}
}),
);
}
}