use qql_core::error::QqlError;
use qql_embed::{AvgLenEstimate, estimate_avg_len};
use super::{Executor, OnError};
impl Executor {
pub async fn estimate_bm25_avg_len(
&self,
collection: &str,
field: &str,
sample: usize,
) -> Result<Option<AvgLenEstimate>, QqlError> {
if collection.trim().is_empty() {
return Err(QqlError::validation(
"QQL-VALIDATION-COLLECTION",
"estimate_bm25_avg_len needs a non-empty collection name",
None,
));
}
if field.trim().is_empty() {
return Err(QqlError::validation(
"QQL-VALIDATION-FIELD",
"estimate_bm25_avg_len needs a non-empty payload field",
None,
));
}
if sample == 0 {
return Ok(None);
}
let quoted = collection.replace('\'', "''");
let report = self
.execute(
&format!("SCROLL FROM '{quoted}' LIMIT {sample}"),
OnError::Stop,
)
.await?;
let Some(response) = report.results.into_iter().next() else {
return Ok(None);
};
let Some(hits) = response.hits_ref() else {
return Ok(None);
};
let pipeline = match self.embedder.as_ref() {
Some(embedder) => embedder.bm25_text_config().pipeline(),
None => match self.config.as_ref() {
Some(config) => config.bm25_text_config()?.pipeline(),
None => qql_embed::Bm25TextConfig::default().pipeline(),
},
};
let mut texts = Vec::new();
for hit in hits {
if let Some(payload) = hit.payload.as_ref()
&& let Some((_, serde_json::Value::String(text))) = payload
.iter()
.find(|(key, _)| key.eq_ignore_ascii_case(field))
&& !text.is_empty()
{
texts.push(text.clone());
}
}
estimate_avg_len(texts.iter().map(String::as_str), &pipeline)
}
}