Skip to content

Commit

Permalink
check unsupported parameters top_hits (#2351)
Browse files Browse the repository at this point in the history
* check unsupported parameters top_hits

* move to function
  • Loading branch information
PSeitz authored Apr 10, 2024
1 parent 398817c commit dfa3aed
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/aggregation/metric/top_hits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,6 +97,14 @@ pub struct TopHitsAggregation {
#[serde(rename = "docvalue_fields")]
#[serde(default)]
doc_value_fields: Vec<String>,

// Not supported
_source: Option<serde_json::Value>,
fields: Option<serde_json::Value>,
script_fields: Option<serde_json::Value>,
highlight: Option<serde_json::Value>,
explain: Option<serde_json::Value>,
version: Option<serde_json::Value>,
}

#[derive(Debug, Clone, PartialEq, Default)]
Expand Down Expand Up @@ -142,12 +151,49 @@ fn globbed_string_to_regex(glob: &str) -> Result<Regex, crate::TantivyError> {
})
}

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()
Expand Down
4 changes: 4 additions & 0 deletions src/aggregation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check warning on line 162 in src/aggregation/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

function `invalid_agg_request` is never used

warning: function `invalid_agg_request` is never used --> src/aggregation/mod.rs:162:15 | 162 | pub(crate) fn invalid_agg_request(message: String) -> crate::TantivyError { | ^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
crate::TantivyError::AggregationError(AggregationError::InvalidRequest(message))
}

fn parse_str_into_f64<E: de::Error>(value: &str) -> Result<f64, E> {
let parsed = value.parse::<f64>().map_err(|_err| {
de::Error::custom(format!("Failed to parse f64 from string: {:?}", value))
Expand Down

0 comments on commit dfa3aed

Please sign in to comment.