pub struct KeywordMatcher {
pub name_weight: usize,
pub type_weight: usize,
pub desc_weight: usize,
pub synonyms: HashMap<String, Vec<String>>,
pub use_tfidf: bool,
pub synonym_weight: f64,
pub cjk_bigram_weight: f64,
}Expand description
Matches entities by keyword substring search.
This is the default matcher used by GraphRAG. It splits the query into keywords and scores each entity based on how many keywords match the entity’s name, type, and description. Name matches are weighted highest.
P2-4: on top of the fixed name+3/type+2/desc+1 weights, three improvements fix the arbitrary weights and the recall gaps of substring matching for synonyms, polysemes, and Chinese-English mixed text:
- Synonym-table expansion
synonyms: when a query term hits a synonym key, the equivalent words are matched too (each hit decayed bysynonym_weight, default 0.7). - Chinese-English mixed normalization: full-width -> half-width plus splitting long Chinese queries into CJK bigrams, fixing the problem that a space-free Chinese single token cannot hit a short entity name.
- TF-IDF weighting: each query term is weighted by its inverse document frequency in
the entity corpus; common words (e.g. “Technology”) discriminate little and contribute
little, while rare words contribute more;
use_tfidfcan disable it.
Fields§
§name_weight: usizeWeight for name matches (default: 3).
type_weight: usizeWeight for type matches (default: 2).
desc_weight: usizeWeight for description matches (default: 1).
synonyms: HashMap<String, Vec<String>>Synonym table: query term (normalized lowercase/half-width form) -> list of equivalent
words, also in normalized form. When an equivalent word is hit, it is matched once more
with the contribution multiplied by synonym_weight.
use_tfidf: boolWhether TF-IDF weighting is enabled (default true). When disabled, falls back to the fixed weights.
synonym_weight: f64Decay factor for synonym hits (default 0.7).
cjk_bigram_weight: f64Decay factor for CJK bigram hits (default 0.5).
Implementations§
Source§impl KeywordMatcher
impl KeywordMatcher
Sourcepub fn with_synonyms(self, synonyms: HashMap<String, Vec<String>>) -> Self
pub fn with_synonyms(self, synonyms: HashMap<String, Vec<String>>) -> Self
Configures the synonym table (query term -> equivalent word list).
Sourcepub fn with_tfidf(self, enabled: bool) -> Self
pub fn with_tfidf(self, enabled: bool) -> Self
Toggles TF-IDF weighting (enabled by default).
Trait Implementations§
Source§impl Default for KeywordMatcher
impl Default for KeywordMatcher
Source§impl EntityMatcher for KeywordMatcher
impl EntityMatcher for KeywordMatcher
Source§fn find_relevant(
&self,
query: &str,
store: &GraphStore,
top_k: usize,
) -> Vec<String>
fn find_relevant( &self, query: &str, store: &GraphStore, top_k: usize, ) -> Vec<String>
top_k results.