From dfa3aed32de5793bea325bdbc3d261b56b74d5f1 Mon Sep 17 00:00:00 2001 From: PSeitz Date: Wed, 10 Apr 2024 08:20:52 +0200 Subject: [PATCH] check unsupported parameters top_hits (#2351) * check unsupported parameters top_hits * move to function --- src/aggregation/metric/top_hits.rs | 46 ++++++++++++++++++++++++++++++ src/aggregation/mod.rs | 4 +++ 2 files changed, 50 insertions(+) diff --git a/src/aggregation/metric/top_hits.rs b/src/aggregation/metric/top_hits.rs index 28f4418644..7ffcf2e2b0 100644 --- a/src/aggregation/metric/top_hits.rs +++ b/src/aggregation/metric/top_hits.rs @@ -13,6 +13,7 @@ use crate::aggregation::intermediate_agg_result::{ IntermediateAggregationResult, IntermediateMetricResult, }; use crate::aggregation::segment_agg_result::SegmentAggregationCollector; +use crate::aggregation::AggregationError; use crate::collector::TopNComputer; use crate::schema::term::JSON_PATH_SEGMENT_SEP_STR; use crate::schema::OwnedValue; @@ -96,6 +97,14 @@ pub struct TopHitsAggregation { #[serde(rename = "docvalue_fields")] #[serde(default)] doc_value_fields: Vec, + + // Not supported + _source: Option, + fields: Option, + script_fields: Option, + highlight: Option, + explain: Option, + version: Option, } #[derive(Debug, Clone, PartialEq, Default)] @@ -142,12 +151,49 @@ fn globbed_string_to_regex(glob: &str) -> Result { }) } +fn use_doc_value_fields_err(parameter: &str) -> crate::Result<()> { + Err(crate::TantivyError::AggregationError( + AggregationError::InvalidRequest(format!( + "The `{}` parameter is not supported, only `docvalue_fields` is supported in \ + `top_hits` aggregation", + parameter + )), + )) +} +fn unsupported_err(parameter: &str) -> crate::Result<()> { + Err(crate::TantivyError::AggregationError( + AggregationError::InvalidRequest(format!( + "The `{}` parameter is not supported in the `top_hits` aggregation", + parameter + )), + )) +} + impl TopHitsAggregation { /// Validate and resolve field retrieval parameters pub fn validate_and_resolve_field_names( &mut self, reader: &ColumnarReader, ) -> crate::Result<()> { + if self._source.is_some() { + use_doc_value_fields_err("_source")?; + } + if self.fields.is_some() { + use_doc_value_fields_err("fields")?; + } + if self.script_fields.is_some() { + use_doc_value_fields_err("script_fields")?; + } + if self.explain.is_some() { + unsupported_err("explain")?; + } + if self.highlight.is_some() { + unsupported_err("highlight")?; + } + if self.version.is_some() { + unsupported_err("version")?; + } + self.doc_value_fields = self .doc_value_fields .iter() diff --git a/src/aggregation/mod.rs b/src/aggregation/mod.rs index fbb2925dd1..c88d14c6e4 100644 --- a/src/aggregation/mod.rs +++ b/src/aggregation/mod.rs @@ -159,6 +159,10 @@ use itertools::Itertools; use serde::de::{self, Visitor}; use serde::{Deserialize, Deserializer, Serialize}; +pub(crate) fn invalid_agg_request(message: String) -> crate::TantivyError { + crate::TantivyError::AggregationError(AggregationError::InvalidRequest(message)) +} + fn parse_str_into_f64(value: &str) -> Result { let parsed = value.parse::().map_err(|_err| { de::Error::custom(format!("Failed to parse f64 from string: {:?}", value))