Ragex. Retrieval. MetaASTRanker
(Ragex v0.14.1)
View Source
Enhances retrieval ranking using MetaAST metadata from Metastatic.
Provides ranking boosts based on semantic properties extracted from MetaAST:
- Purity analysis (pure functions rank higher for query contexts)
- Complexity metrics (simpler code ranks higher for explanations)
- Cross-language semantic equivalence
- Meta-level properties (M2.1 core vs M2.3 native)
MetaAST Levels
- M2.1 Core: Universal constructs (literals, variables, binary_op, etc.)
- M2.2 Extended: Common patterns (loops, lambdas, collections)
- M2.3 Native: Language-specific escape hatches
Ranking Strategy
Higher scores are given to:
- Core-level constructs (more portable/understandable)
- Pure functions (no side effects)
- Lower complexity (easier to understand)
- Cross-language semantic matches
Summary
Functions
Apply context-aware MetaAST ranking boosts to retrieval results.
Calculate MetaAST-based ranking boost for a retrieval result.
Extract semantic features from MetaAST for query expansion.
Find cross-language equivalents for a given result.
Check if two results represent semantically equivalent constructs.
Functions
Apply context-aware MetaAST ranking boosts to retrieval results.
Analyzes query context to apply appropriate ranking strategies:
- Explanation queries: Prefer simple, pure, core-level code
- Refactoring queries: Prefer code with improvement opportunities
- Example queries: Prefer diverse, cross-language examples
- Debugging queries: Prefer code with side effects/complexity
Options
:query- The original query string for context detection:intent- Explicitly specify intent (:explain,:refactor,:example,:debug)- All options from
calculate_boost/2
Examples
results = [...]
# Auto-detect intent from query
MetaASTRanker.apply_ranking(results, query: "explain how map works")
# Explicit intent
MetaASTRanker.apply_ranking(results, intent: :refactor)
Calculate MetaAST-based ranking boost for a retrieval result.
Returns a boost multiplier (1.0 = no boost, >1.0 = boost, <1.0 = penalty).
Options
:boost_core- Boost for M2.1 core constructs (default: 1.2):boost_pure- Boost for pure functions (default: 1.3):complexity_penalty- Penalty per complexity unit (default: 0.02):native_penalty- Penalty for M2.3 native constructs (default: 0.9)
Extract semantic features from MetaAST for query expansion.
Returns a list of semantic tags that can be used for query expansion.
Examples
result = %{meta_ast: {:collection_op, :map, fn, collection}}
MetaASTRanker.extract_semantic_features(result)
# => ["collection", "map", "transform", "iteration"]
Find cross-language equivalents for a given result.
Returns results that have semantically equivalent MetaAST structures but are from different languages.
Examples
python_map = %{meta_ast: {:collection_op, :map, ...}, language: :python}
all_results = [python_map, elixir_map, javascript_map, ...]
MetaASTRanker.find_cross_language_equivalents(python_map, all_results)
# => [elixir_map, javascript_map]
Check if two results represent semantically equivalent constructs.
Uses MetaAST comparison to identify cross-language equivalents.
Examples
# Python list comprehension and Elixir Enum.map are equivalent at M2 level
result1 = %{meta_ast: {:collection_op, :map, ...}, language: :python}
result2 = %{meta_ast: {:collection_op, :map, ...}, language: :elixir}
MetaASTRanker.semantically_equivalent?(result1, result2)
# => true