Skip to content

Commit fd70de0

Browse files
committed
Refine DSL path handling and cleanup formatting
1 parent 9f9442d commit fd70de0

21 files changed

Lines changed: 593 additions & 258 deletions

src/apps/wikidata/interactive.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -676,16 +676,8 @@ impl InteractiveQueryMode {
676676
let t = Instant::now();
677677
let req = ShortestPathRequest {
678678
database: None,
679-
from: PathEndpoint {
680-
schema: "wikidata_entity".to_string(),
681-
match_clause: Some(eq_predicate("id", from)),
682-
search: None,
683-
},
684-
to: PathEndpoint {
685-
schema: "wikidata_entity".to_string(),
686-
match_clause: Some(eq_predicate("id", to)),
687-
search: None,
688-
},
679+
from: path_endpoint(from),
680+
to: path_endpoint(to),
689681
via: PathViaClause {
690682
edges: vec!["wikidata_link".to_string()],
691683
direction: Some(crate::dsl::query::json::TraverseDirection::Undirected),
@@ -721,16 +713,8 @@ impl InteractiveQueryMode {
721713
let t = Instant::now();
722714
let req = PathsRequest {
723715
database: None,
724-
from: PathEndpoint {
725-
schema: "wikidata_entity".to_string(),
726-
match_clause: Some(eq_predicate("id", from)),
727-
search: None,
728-
},
729-
to: PathEndpoint {
730-
schema: "wikidata_entity".to_string(),
731-
match_clause: Some(eq_predicate("id", to)),
732-
search: None,
733-
},
716+
from: path_endpoint(from),
717+
to: path_endpoint(to),
734718
via: crate::dsl::graph::json::PathsViaClause {
735719
edges: vec!["wikidata_link".to_string()],
736720
direction: Some(crate::dsl::query::json::TraverseDirection::Undirected),
@@ -864,6 +848,32 @@ fn eq_predicate(field: &str, value: &str) -> Predicate {
864848
})
865849
}
866850

851+
fn is_wikidata_entity_like_id(value: &str) -> bool {
852+
let mut chars = value.chars();
853+
matches!(chars.next(), Some('Q' | 'P')) && chars.all(|c| c.is_ascii_digit()) && value.len() > 1
854+
}
855+
856+
fn path_endpoint(value: &str) -> PathEndpoint {
857+
if is_wikidata_entity_like_id(value) {
858+
PathEndpoint {
859+
schema: "wikidata_entity".to_string(),
860+
match_clause: Some(eq_predicate("id", value)),
861+
search: None,
862+
}
863+
} else {
864+
PathEndpoint {
865+
schema: "wikidata_entity".to_string(),
866+
match_clause: None,
867+
search: Some(SearchClause {
868+
field: "label".to_string(),
869+
text: value.to_string(),
870+
top_k: Some(1),
871+
mode: Some(crate::dsl::query::json::SearchMode::Fulltext),
872+
}),
873+
}
874+
}
875+
}
876+
867877
fn build_traverse_request(id: &str, depth: usize) -> QueryDslRequest {
868878
let mut traverse = Vec::with_capacity(depth);
869879
let mut select = vec!["id".to_string(), "label".to_string()];

src/bin/wikidata_cli.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,32 @@ fn eq_pred(field: &str, value: &str) -> Predicate {
170170
})
171171
}
172172

173+
fn is_wikidata_entity_like_id(value: &str) -> bool {
174+
let mut chars = value.chars();
175+
matches!(chars.next(), Some('Q' | 'P')) && chars.all(|c| c.is_ascii_digit()) && value.len() > 1
176+
}
177+
178+
fn path_endpoint(schema: &str, value: &str) -> PathEndpoint {
179+
if is_wikidata_entity_like_id(value) {
180+
PathEndpoint {
181+
schema: schema.to_string(),
182+
match_clause: Some(eq_pred("id", value)),
183+
search: None,
184+
}
185+
} else {
186+
PathEndpoint {
187+
schema: schema.to_string(),
188+
match_clause: None,
189+
search: Some(SearchClause {
190+
field: "label".to_string(),
191+
text: value.to_string(),
192+
top_k: Some(1),
193+
mode: Some(morpheus::dsl::query::json::SearchMode::Fulltext),
194+
}),
195+
}
196+
}
197+
}
198+
173199
fn column_value<'a>(
174200
row: &'a morpheus::dsl::query::json::QueryDslRow,
175201
name: &str,
@@ -611,16 +637,8 @@ async fn run_query_command(
611637
} => {
612638
let req = ShortestPathRequest {
613639
database: None,
614-
from: PathEndpoint {
615-
schema: "wikidata_entity".to_string(),
616-
match_clause: Some(eq_pred("id", &from_id)),
617-
search: None,
618-
},
619-
to: PathEndpoint {
620-
schema: "wikidata_entity".to_string(),
621-
match_clause: Some(eq_pred("id", &to_id)),
622-
search: None,
623-
},
640+
from: path_endpoint("wikidata_entity", &from_id),
641+
to: path_endpoint("wikidata_entity", &to_id),
624642
via: PathViaClause {
625643
edges: vec!["wikidata_link".to_string()],
626644
direction: Some(TraverseDirection::Undirected),
@@ -654,16 +672,8 @@ async fn run_query_command(
654672
} => {
655673
let req = PathsRequest {
656674
database: None,
657-
from: PathEndpoint {
658-
schema: "wikidata_entity".to_string(),
659-
match_clause: Some(eq_pred("id", &from_id)),
660-
search: None,
661-
},
662-
to: PathEndpoint {
663-
schema: "wikidata_entity".to_string(),
664-
match_clause: Some(eq_pred("id", &to_id)),
665-
search: None,
666-
},
675+
from: path_endpoint("wikidata_entity", &from_id),
676+
to: path_endpoint("wikidata_entity", &to_id),
667677
via: morpheus::dsl::graph::json::PathsViaClause {
668678
edges: vec!["wikidata_link".to_string()],
669679
direction: Some(TraverseDirection::Undirected),

src/dsl/graph/exec_context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::dsl::exec_helpers::{
1111
};
1212
use crate::dsl::graph::json::{GraphRAGResult, GraphRAGSeedVertex, SubgraphEdge, SubgraphVertex};
1313
use crate::dsl::query::bind::{
14-
BoundGraphRAGQuery, BoundPredicate, SearchMode, extract_root_seed_ids,
14+
extract_root_seed_ids, BoundGraphRAGQuery, BoundPredicate, SearchMode,
1515
};
1616
use crate::graph::edge::EdgeType;
1717
use crate::server::schema::GraphSchema;
@@ -232,7 +232,9 @@ async fn fetch_seeds(
232232
) -> Result<(Vec<Id>, AHashMap<Id, AHashMap<(u64, QueryHitType), f32>>), String> {
233233
if let Some(seed_ids) = extract_root_seed_ids(query.match_clause.as_ref())? {
234234
if query.search.is_some() {
235-
return Err("root _cell_id match cannot be combined with search in context DSL".to_string());
235+
return Err(
236+
"root _cell_id match cannot be combined with search in context DSL".to_string(),
237+
);
236238
}
237239

238240
let mut ids = Vec::new();

0 commit comments

Comments
 (0)