Repository navigation
Adding a Jitexpr ValueSource to allow jitexpr in aggregations. #3162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // # Jitexpr fields in aggregations | ||
| // | ||
| // This example shows how aggregations can use a jitexpr field: a field that is not in the | ||
| // index, but computed from the fast fields of each document with a jitexpr expression. | ||
| // | ||
| // - A jitexpr field is registered under a name in a `ValueSourceRegistry` | ||
| // - Aggregations use that name like the name of a fast field. | ||
| // | ||
| // Requires the `jitexpr` feature. | ||
| // --- | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use serde_json::{json, Value}; | ||
| use tantivy::aggregation::agg_req::Aggregations; | ||
| use tantivy::aggregation::{ | ||
| AggContextParams, AggregationCollector, JitExprValueSourceProvider, ValueSourceRegistry, | ||
| }; | ||
| use tantivy::query::AllQuery; | ||
| use tantivy::schema::{Schema, FAST, STRING}; | ||
| use tantivy::{doc, Index, IndexWriter}; | ||
|
|
||
| fn main() -> tantivy::Result<()> { | ||
| // # Create Schema | ||
| // | ||
| // Aggregations read fast fields: the inputs of the jitexpr field must be fast. | ||
| let mut schema_builder = Schema::builder(); | ||
| let category = schema_builder.add_text_field("category", STRING | FAST); | ||
| let price = schema_builder.add_f64_field("price", FAST); | ||
| let stock = schema_builder.add_f64_field("stock", FAST); | ||
| let schema = schema_builder.build(); | ||
|
|
||
| // # Indexing documents | ||
| let index = Index::create_in_ram(schema); | ||
| let mut index_writer: IndexWriter = index.writer(50_000_000)?; | ||
| index_writer.add_document(doc!(category => "Womens Footwear", price => 99.0, stock => 5.0))?; | ||
| index_writer.add_document(doc!(category => "Womens Footwear", price => 59.0, stock => 8.0))?; | ||
| index_writer.add_document(doc!(category => "Mens Footwear", price => 120.0, stock => 2.0))?; | ||
| index_writer.add_document(doc!(category => "Mens Footwear", price => 75.0, stock => 10.0))?; | ||
| // A document without a price: the jitexpr field has no value for it. | ||
| index_writer.add_document(doc!(category => "Mens Footwear", stock => 3.0))?; | ||
| index_writer.commit()?; | ||
|
|
||
| // # Registering the jitexpr field | ||
| // | ||
| // The expression is written in the jitexpr s-expression syntax. Variables (e.g. `price`) are | ||
| // resolved as fast field names, for each segment. | ||
| // | ||
| // A jitexpr field shadows a fast field with the same name. Here, the name starts with `#` | ||
| // so that it cannot collide with a fast field. | ||
| let expression = tantivy::jitexpr::ast::deserialize("(MULTIPLY price stock)") | ||
| .map_err(|err| tantivy::TantivyError::InvalidArgument(format!("{err:?}")))?; | ||
| let provider = JitExprValueSourceProvider::new(expression) | ||
| .map_err(|err| tantivy::TantivyError::InvalidArgument(format!("{err:?}")))?; | ||
| let mut value_sources = ValueSourceRegistry::default(); | ||
| value_sources.register("#stock_value", Arc::new(provider)); | ||
| let context = AggContextParams::default().with_value_sources(Arc::new(value_sources)); | ||
|
|
||
| // # Aggregating over the jitexpr field | ||
| // | ||
| // The total stock value of each category. | ||
| let aggs: Aggregations = serde_json::from_value(json!({ | ||
| "categories": { | ||
| "terms": { "field": "category" }, | ||
| "aggs": { | ||
| "total_stock_value": { "sum": { "field": "#stock_value" } } | ||
| } | ||
| } | ||
| }))?; | ||
| let collector = AggregationCollector::from_aggs(aggs, context); | ||
|
|
||
| let searcher = index.reader()?.searcher(); | ||
| let agg_res = searcher.search(&AllQuery, &collector)?; | ||
| let res: Value = serde_json::to_value(agg_res)?; | ||
| println!("{}", serde_json::to_string_pretty(&res)?); | ||
|
|
||
| let buckets = &res["categories"]["buckets"]; | ||
| assert_eq!(buckets[0]["key"], "Mens Footwear"); | ||
| // 120 * 2 + 75 * 10. The document without a price has no stock value. | ||
| assert_eq!(buckets[0]["total_stock_value"]["value"], 990.0); | ||
| assert_eq!(buckets[1]["key"], "Womens Footwear"); | ||
| // 99 * 5 + 59 * 8. | ||
| assert_eq!(buckets[1]["total_stock_value"]["value"], 967.0); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
[[example]]target has neitherexamples/aggregation_jitexpr.rsnorexamples/aggregation_jitexpr/main.rsanywhere in the repository. Cargo therefore rejects the manifest before resolving or compiling any target (can't find aggregation_jitexpr example), so all Cargo build, test, and metadata commands fail even when thejitexprfeature is not enabled.Useful? React with 👍 / 👎.