From b2e97e266a3cc524ea1e49a20a56238ea16fb895 Mon Sep 17 00:00:00 2001 From: saroh <325288+saroh@users.noreply.github.com> Date: Sat, 21 May 2022 17:05:31 +0200 Subject: [PATCH 1/6] more examples to explain default field handling --- examples/json_field.rs | 47 +++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/examples/json_field.rs b/examples/json_field.rs index e61f8a934..f625abc5e 100644 --- a/examples/json_field.rs +++ b/examples/json_field.rs @@ -1,7 +1,8 @@ // # Json field example // // This example shows how the json field can be used -// to make tantivy partially schemaless. +// to make tantivy partially schemaless by setting it as +// default query parser field. use tantivy::collector::{Count, TopDocs}; use tantivy::query::QueryParser; @@ -10,10 +11,6 @@ use tantivy::Index; fn main() -> tantivy::Result<()> { // # Defining the schema - // - // We need two fields: - // - a timestamp - // - a json object field let mut schema_builder = Schema::builder(); schema_builder.add_date_field("timestamp", FAST | STORED); let event_type = schema_builder.add_text_field("event_type", STRING | STORED); @@ -43,7 +40,8 @@ fn main() -> tantivy::Result<()> { "attributes": { "target": "submit-button", "cart": {"product_id": 133}, - "description": "das keyboard" + "description": "das keyboard", + "event_type": "holiday-sale" } }"#, )?; @@ -53,6 +51,7 @@ fn main() -> tantivy::Result<()> { let reader = index.reader()?; let searcher = reader.searcher(); + // # Default fields: event_type and attributes let query_parser = QueryParser::for_index(&index, vec![event_type, attributes]); { let query = query_parser.parse_query("target:submit-button")?; @@ -70,10 +69,38 @@ fn main() -> tantivy::Result<()> { assert_eq!(count_docs, 1); } { - let query = query_parser - .parse_query("event_type:click AND cart.product_id:133") - .unwrap(); - let hits = searcher.search(&*query, &TopDocs::with_limit(2)).unwrap(); + let query = query_parser.parse_query("click AND cart.product_id:133")?; + let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; + assert_eq!(hits.len(), 1); + } + { + // The default json fields need to be prefixed by the path + let query = query_parser.parse_query("click AND 133")?; + let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; + assert_eq!(hits.len(), 0); + } + // ## Default json fields are ignored if they collide with the schema + { + let query = query_parser.parse_query("holiday-sale")?; + let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; + assert_eq!(hits.len(), 0); + } + { + let query = query_parser.parse_query("event_type:holiday-sale")?; + let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; + assert_eq!(hits.len(), 0); + } + // # Query via full attribute path + { + // This only searches in our schema's `event_type` field + let query = query_parser.parse_query("event_type:click")?; + let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; + assert_eq!(hits.len(), 2); + } + { + // Default json fields can still be accessed by full path + let query = query_parser.parse_query("attributes.event_type:holiday-sale")?; + let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; assert_eq!(hits.len(), 1); } Ok(()) From 4e2a053b697a282343329c25a94b3fc3eff512fe Mon Sep 17 00:00:00 2001 From: PSeitz Date: Mon, 23 May 2022 11:27:05 +0200 Subject: [PATCH 2/6] Update examples/json_field.rs --- examples/json_field.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/json_field.rs b/examples/json_field.rs index f625abc5e..bcbf3ea4d 100644 --- a/examples/json_field.rs +++ b/examples/json_field.rs @@ -52,6 +52,7 @@ fn main() -> tantivy::Result<()> { let searcher = reader.searcher(); // # Default fields: event_type and attributes + // By setting attributes as a default field it allows omitting attributes itself, e.g. "target", instead of "attributes.target" let query_parser = QueryParser::for_index(&index, vec![event_type, attributes]); { let query = query_parser.parse_query("target:submit-button")?; From 0aa3d63a9f4d1b9b209dbfbb2a2d0ec58ce0de3f Mon Sep 17 00:00:00 2001 From: PSeitz Date: Mon, 23 May 2022 11:39:45 +0200 Subject: [PATCH 3/6] Update examples/json_field.rs --- examples/json_field.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/json_field.rs b/examples/json_field.rs index bcbf3ea4d..ac4553b6f 100644 --- a/examples/json_field.rs +++ b/examples/json_field.rs @@ -75,7 +75,7 @@ fn main() -> tantivy::Result<()> { assert_eq!(hits.len(), 1); } { - // The default json fields need to be prefixed by the path + // The sub-fields in the json field marked as default field still need to be explicitly addressed let query = query_parser.parse_query("click AND 133")?; let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; assert_eq!(hits.len(), 0); From 93cc8498b379a73a22fec68a1888ccfef08573fd Mon Sep 17 00:00:00 2001 From: PSeitz Date: Mon, 23 May 2022 11:59:42 +0200 Subject: [PATCH 4/6] Update examples/json_field.rs --- examples/json_field.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/json_field.rs b/examples/json_field.rs index ac4553b6f..07f689c35 100644 --- a/examples/json_field.rs +++ b/examples/json_field.rs @@ -75,7 +75,8 @@ fn main() -> tantivy::Result<()> { assert_eq!(hits.len(), 1); } { - // The sub-fields in the json field marked as default field still need to be explicitly addressed + // The sub-fields in the json field marked as default field still need to be explicitly + // addressed let query = query_parser.parse_query("click AND 133")?; let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; assert_eq!(hits.len(), 0); From 496b4a4fdb65f7a029b708bd3e1844b2f02132b2 Mon Sep 17 00:00:00 2001 From: PSeitz Date: Mon, 23 May 2022 12:24:36 +0200 Subject: [PATCH 5/6] Update examples/json_field.rs --- examples/json_field.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/json_field.rs b/examples/json_field.rs index 07f689c35..107beddde 100644 --- a/examples/json_field.rs +++ b/examples/json_field.rs @@ -52,7 +52,8 @@ fn main() -> tantivy::Result<()> { let searcher = reader.searcher(); // # Default fields: event_type and attributes - // By setting attributes as a default field it allows omitting attributes itself, e.g. "target", instead of "attributes.target" + // By setting attributes as a default field it allows omitting attributes itself, e.g. "target", + // instead of "attributes.target" let query_parser = QueryParser::for_index(&index, vec![event_type, attributes]); { let query = query_parser.parse_query("target:submit-button")?; From e766375700a4f7ffe4910ff036f078fbbbe3b0ce Mon Sep 17 00:00:00 2001 From: saroh <325288+saroh@users.noreply.github.com> Date: Mon, 23 May 2022 19:49:31 +0200 Subject: [PATCH 6/6] remove useless example --- examples/json_field.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/examples/json_field.rs b/examples/json_field.rs index 107beddde..9a2bf5d87 100644 --- a/examples/json_field.rs +++ b/examples/json_field.rs @@ -82,13 +82,8 @@ fn main() -> tantivy::Result<()> { let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; assert_eq!(hits.len(), 0); } - // ## Default json fields are ignored if they collide with the schema - { - let query = query_parser.parse_query("holiday-sale")?; - let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; - assert_eq!(hits.len(), 0); - } { + // Default json fields are ignored if they collide with the schema let query = query_parser.parse_query("event_type:holiday-sale")?; let hits = searcher.search(&*query, &TopDocs::with_limit(2))?; assert_eq!(hits.len(), 0);