mirror of
https://github.com/quickwit-oss/tantivy.git
synced 2025-12-23 02:29:57 +00:00
* fix windows build (#1) * Fix windows build * Add doc traits * Add field value iter * Add value and serialization * Adjust order * Fix bug * Correct type * Fix generic bugs * Reformat code * Add generic to index writer which I forgot about * Fix missing generics on single segment writer * Add missing type export * Add default methods for convenience * Cleanup * Fix more-like-this query to use standard types * Update API and fix tests * Add doc traits * Add field value iter * Add value and serialization * Adjust order * Fix bug * Correct type * Rebase main and fix conflicts * Reformat code * Merge upstream * Fix missing generics on single segment writer * Add missing type export * Add default methods for convenience * Cleanup * Fix more-like-this query to use standard types * Update API and fix tests * Add tokenizer improvements from previous commits * Add tokenizer improvements from previous commits * Reformat * Fix unit tests * Fix unit tests * Use enum in changes * Stage changes * Add new deserializer logic * Add serializer integration * Add document deserializer * Implement new (de)serialization api for existing types * Fix bugs and type errors * Add helper implementations * Fix errors * Reformat code * Add unit tests and some code organisation for serialization * Add unit tests to deserializer * Add some small docs * Add support for deserializing serde values * Reformat * Fix typo * Fix typo * Change repr of facet * Remove unused trait methods * Add child value type * Resolve comments * Fix build * Fix more build errors * Fix more build errors * Fix the tests I missed * Fix examples * fix numerical order, serialize PreTok Str * fix coverage * rename Document to TantivyDocument, rename DocumentAccess to Document add Binary prefix to binary de/serialization * fix coverage --------- Co-authored-by: Pascal Seitz <pascal.seitz@gmail.com>
76 lines
2.5 KiB
Rust
76 lines
2.5 KiB
Rust
// # DateTime field example
|
|
//
|
|
// This example shows how the DateTime field can be used
|
|
|
|
use tantivy::collector::TopDocs;
|
|
use tantivy::query::QueryParser;
|
|
use tantivy::schema::{DateOptions, Schema, Value, INDEXED, STORED, STRING};
|
|
use tantivy::{Index, IndexWriter, TantivyDocument};
|
|
|
|
fn main() -> tantivy::Result<()> {
|
|
// # Defining the schema
|
|
let mut schema_builder = Schema::builder();
|
|
let opts = DateOptions::from(INDEXED)
|
|
.set_stored()
|
|
.set_fast()
|
|
.set_precision(tantivy::DateTimePrecision::Seconds);
|
|
// Add `occurred_at` date field type
|
|
let occurred_at = schema_builder.add_date_field("occurred_at", opts);
|
|
let event_type = schema_builder.add_text_field("event", STRING | STORED);
|
|
let schema = schema_builder.build();
|
|
|
|
// # Indexing documents
|
|
let index = Index::create_in_ram(schema.clone());
|
|
|
|
let mut index_writer: IndexWriter = index.writer(50_000_000)?;
|
|
// The dates are passed as string in the RFC3339 format
|
|
let doc = TantivyDocument::parse_json(
|
|
&schema,
|
|
r#"{
|
|
"occurred_at": "2022-06-22T12:53:50.53Z",
|
|
"event": "pull-request"
|
|
}"#,
|
|
)?;
|
|
index_writer.add_document(doc)?;
|
|
let doc = TantivyDocument::parse_json(
|
|
&schema,
|
|
r#"{
|
|
"occurred_at": "2022-06-22T13:00:00.22Z",
|
|
"event": "comment"
|
|
}"#,
|
|
)?;
|
|
index_writer.add_document(doc)?;
|
|
index_writer.commit()?;
|
|
|
|
let reader = index.reader()?;
|
|
let searcher = reader.searcher();
|
|
|
|
// # Search
|
|
let query_parser = QueryParser::for_index(&index, vec![event_type]);
|
|
{
|
|
// Simple exact search on the date
|
|
let query = query_parser.parse_query("occurred_at:\"2022-06-22T12:53:50.53Z\"")?;
|
|
let count_docs = searcher.search(&*query, &TopDocs::with_limit(5))?;
|
|
assert_eq!(count_docs.len(), 1);
|
|
}
|
|
{
|
|
// Range query on the date field
|
|
let query = query_parser
|
|
.parse_query(r#"occurred_at:[2022-06-22T12:58:00Z TO 2022-06-23T00:00:00Z}"#)?;
|
|
let count_docs = searcher.search(&*query, &TopDocs::with_limit(4))?;
|
|
assert_eq!(count_docs.len(), 1);
|
|
for (_score, doc_address) in count_docs {
|
|
let retrieved_doc = searcher.doc::<TantivyDocument>(doc_address)?;
|
|
assert!(matches!(
|
|
retrieved_doc.get_first(occurred_at),
|
|
Some(Value::Date(_))
|
|
));
|
|
assert_eq!(
|
|
retrieved_doc.to_json(&schema),
|
|
r#"{"event":["comment"],"occurred_at":["2022-06-22T13:00:00.22Z"]}"#
|
|
);
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|