Skip to content

aggregations silently return empty results for invalid fields #2767

@mdashti

Description

@mdashti

Describe the bug

  • What did you do?

    Ran an aggregation query using a field name that doesn't exist in the schema:

    let agg_req: Aggregations = serde_json::from_str(r#"{
        "avg_price": {
            "avg": { "field": "invalid_field" }
        }
    }"#)?;
    
    let collector = AggregationCollector::from_aggs(agg_req, Default::default());
    let result = searcher.search(&AllQuery, &collector)?;
  • What happened?

    The aggregation succeeded and returned empty/null results:

    {"avg_price": {"value": null}}

    No error was raised, making it impossible to distinguish between:

    • A typo in the field name
    • A valid field with no data
    • A query that matched no documents
  • What was expected?

    An error should be returned indicating the field doesn't exist:

    Err(TantivyError::FieldNotFound("invalid_field"))

    This matches the behavior of other query types (like ExistsQuery) and aligns with user expectations from SQL databases and Elasticsearch.

Which version of tantivy are you using?

This affects the current main branch and all recent versions (tested on 0.26.0).

To Reproduce

use tantivy::aggregation::agg_req::Aggregations;
use tantivy::aggregation::AggregationCollector;
use tantivy::query::AllQuery;
use tantivy::schema::{Schema, FAST};
use tantivy::Index;

fn main() -> tantivy::Result<()> {
    // Create a simple schema
    let mut schema_builder = Schema::builder();
    schema_builder.add_f64_field("price", FAST);
    let schema = schema_builder.build();
    
    let index = Index::create_in_ram(schema);
    let mut index_writer = index.writer(50_000_000)?;
    index_writer.commit()?;
    
    let reader = index.reader()?;
    let searcher = reader.searcher();
    
    // Try to aggregate on a non-existent field
    let agg_req: Aggregations = serde_json::from_str(r#"{
        "avg_invalid": {
            "avg": { "field": "nonexistent_field" }
        }
    }"#)?;
    
    let collector = AggregationCollector::from_aggs(agg_req, Default::default());
    let result = searcher.search(&AllQuery, &collector)?;
    
    // Currently returns empty results instead of an error
    println!("{}", serde_json::to_string_pretty(&result)?);
    // Output: {"avg_invalid": {"value": null}}
    
    Ok(())
}

This issue affects all aggregation types: avg, sum, min, max, terms, histogram, date_histogram, range, etc.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions