From bac8ccff8a22d987fb049107eb49829574d36dd9 Mon Sep 17 00:00:00 2001 From: Shivam Kapoor <4599890+iamsmkr@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:54:00 +0000 Subject: [PATCH] fixes #1884 --- raphtory/src/search/mod.rs | 121 +++++++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 6 deletions(-) diff --git a/raphtory/src/search/mod.rs b/raphtory/src/search/mod.rs index b5206c04d..635608559 100644 --- a/raphtory/src/search/mod.rs +++ b/raphtory/src/search/mod.rs @@ -41,7 +41,7 @@ use rayon::{prelude::ParallelIterator, slice::ParallelSlice}; use std::{collections::HashSet, ops::Deref, sync::Arc}; use tantivy::{ collector::TopDocs, - schema::{Field, Schema, SchemaBuilder, Value, FAST, INDEXED, STORED, TEXT}, + schema::{Field, Schema, SchemaBuilder, Value, FAST, INDEXED, STORED, STRING, TEXT}, Index, IndexReader, IndexSettings, IndexWriter, TantivyDocument, TantivyError, }; @@ -117,9 +117,10 @@ impl<'graph, G: GraphViewOps<'graph>> IndexedGraph { // reverse to sort by it schema.add_u64_field(fields::VERTEX_ID_REV, FAST | STORED); // add name - schema.add_text_field(fields::NAME, TEXT); + schema.add_text_field(fields::NAME, STRING | STORED); // add node_type - schema.add_text_field(fields::NODE_TYPE, TEXT); + schema.add_text_field(fields::NODE_TYPE, STRING | STORED); + schema } @@ -157,7 +158,7 @@ impl<'graph, G: GraphViewOps<'graph>> IndexedGraph { fn set_schema_field_from_prop(schema: &mut SchemaBuilder, prop: &str, prop_value: Prop) { match prop_value { Prop::Str(_) => { - schema.add_text_field(prop, TEXT); + schema.add_text_field(prop, STRING | STORED); } Prop::NDTime(_) => { schema.add_date_field(prop, INDEXED); @@ -943,13 +944,121 @@ impl InternalDeletionOps for IndexedGraph { impl DeletionOps for IndexedGraph {} #[cfg(test)] -mod test { +mod search_tests { use super::*; + use itertools::Itertools; use raphtory_api::core::utils::logging::global_info_logger; use std::time::SystemTime; - use tantivy::{doc, DocAddress, Order}; + use tantivy::{doc, schema::STRING, DocAddress, Order}; use tracing::info; + #[test] + fn test_decode_sk() { + let graph = Graph::new(); + graph + .add_node( + 0, + "0x0a5e1db3671faccd146404925bda5c59929f66c3", + [ + ("balance", Prop::F32(0.0011540000414242968)), + ( + "cluster_id", + Prop::Str(ArcStr::from("0x0a5e1db3671faccd146404925bda5c59929f66c3")), + ), + ], + Some("center"), + ) + .unwrap(); + graph + .add_node( + 0, + "0x1c5e2c8e97f34a5ca18dc7370e2bfc0da3baed5c", + [ + ("balance", Prop::F32(0.0)), + ( + "cluster_id", + Prop::Str(ArcStr::from("0x1c5e2c8e97f34a5ca18dc7370e2bfc0da3baed5c")), + ), + ], + Some("collapsed"), + ) + .unwrap(); + graph + .add_node( + 0, + "0x941900204497226bede1324742eb83af6b0b5eec", + [ + ("balance", Prop::F32(0.0)), + ( + "cluster_id", + Prop::Str(ArcStr::from("0x941900204497226bede1324742eb83af6b0b5eec")), + ), + ], + Some("collapsed"), + ) + .unwrap(); + + let ig: IndexedGraph = graph.into(); + + let mut results = ig + .search_nodes("node_type:collapsed", 5, 0) + .expect("failed to search for node") + .into_iter() + .map(|v| v.name()) + .collect::>(); + results.sort(); + assert_eq!( + results, + vec![ + "0x1c5e2c8e97f34a5ca18dc7370e2bfc0da3baed5c", + "0x941900204497226bede1324742eb83af6b0b5eec" + ] + ); + + let results = ig + .search_nodes( + "cluster_id:\"0x941900204497226bede1324742eb83af6b0b5eec\"", + 5, + 0, + ) + .expect("failed to search for node") + .into_iter() + .map(|v| v.name()) + .collect::>(); + assert_eq!(results, vec!["0x941900204497226bede1324742eb83af6b0b5eec"]); + + let results = ig + .search_nodes( + "node_type:collapsed AND cluster_id:\"0x941900204497226bede1324742eb83af6b0b5eec\"", + 5, + 0, + ) + .expect("failed to search for node") + .into_iter() + .map(|v| v.name()) + .collect::>(); + assert_eq!(results, vec!["0x941900204497226bede1324742eb83af6b0b5eec"]); + + let mut results = ig + .search_nodes( + "node_type:collapsed OR cluster_id:\"0x941900204497226bede1324742eb83af6b0b5eec\"", + 5, + 0, + ) + .expect("failed to search for node") + .into_iter() + .map(|v| v.name()) + .collect::>(); + results.sort(); + assert_eq!( + results, + vec![ + "0x1c5e2c8e97f34a5ca18dc7370e2bfc0da3baed5c", + "0x941900204497226bede1324742eb83af6b0b5eec" + ] + ); + } + #[test] fn index_numeric_props() { let graph = Graph::new();