← Back to Pull requests

SOLR-18664: Add expandFilter to the graph query parser #5356

Closed
unknown closed this pull request without merging on Nov 24, 2026 03:42 UTC
View on GitHub
unknown unknown commented on Nov 17, 2026 03:49 UTC
Author

https://issues.apache.org/jira/browse/SOLR-18664

Description

{!graph} does a breadth-first traversal: at each hop it reads the to field of every collected document and adds those values to the query used to find the next level, from IN (...). There is no way to collect a node without also expanding it.

In a file/folder hierarchy ({!graph from=parentId to=id traversalFilter=name:prefix}), only folders can have children, but every file's id still becomes a frontier term. Each one costs a docValues read, a hash insert and a terms-dictionary seek per segment, and can never match. traversalFilter=docType:folder would drop the files from the results. The existing leaf-node optimization doesn't help either, because it only skips documents with no value in to, and here every document has one.

Solution

A new optional expandFilter local param. Documents reached by the traversal (and matching traversalFilter) are always collected, but only those that also match expandFilter have their edges followed. It applies at every level, including the root. Without the param, behaviour is unchanged.

{!graph from=parentId to=id traversalFilter=name:prefix* expandFilter=docType:folder}<root query>

  • GraphQuery gets the filter's DocSet once per request (via the filterCache) and passes it to the edge collectors. GraphEdgeCollector.collect() checks it only before extracting edges; collection itself is unconditional.

  • The JSON Facet graph domain goes through GraphQueryParser, so it supports the param too.

  • Ref guide: new expandFilter entry in the graph query parser parameters.
  • This change was developed with Claude Code (implementation, tests and benchmark); root cause analyzed and changes reviewed by me.

    Benchmark

    Synthetic 1M-doc file-system index: 80k folders, 920k files (~11.5 files per folder), depth up to 15. JDK 21, 4 GB heap, standalone, rows=0, queryResultCache disabled so every query does the full traversal. Each session ran in a fresh JVM with 10 warmup + 30 measured iterations per root and arm, interleaved. Session order: patched, baseline (unpatched build of the same commit), patched.

    The query was run from 18 starting folders whose results range from ~100 documents to the whole tree. Each row covers folders that return a similar number of documents (shown as the average numFound). For each folder we take the median QTime (in ms) of its 60 runs; the row shows the geometric mean of those medians:

    | Docs returned (avg numFound) | Starting folders | Without expandFilter (ms) | With expandFilter=docType:folder (ms) | Speedup (same build) |
    |---|---|---|---|---|
    | 101 | 3 | 2.6 | 1.3 | 2.1×\
    |
    | 884 | 3 | 7.6 | 2.3 | 3.3×\ |
    | 10,214 | 3 | 53 | 7.8 | 6.8× |
    | 42,546 | 3 | 174 | 24 | 7.4× |
    | 93,708 | 3 | 349 | 42 | 8.3× |
    | 217,834 | 2 | 656 | 73 | 8.9× |
    | 828,781 (whole tree) | 1 | 3,038 | 298 | 10.2× |

    \ QTime is reported in whole milliseconds, so these two rows are coarse (due to rounding).

  • Correctness: all 5,760 queries returned the numFound computed independently from the generated tree, and all variants agree.

  • Why: for the whole tree the frontier queries carry 12× fewer terms in total (828,781 → 68,600). What remains is collecting the documents themselves.

  • No regression without the param: unpatched vs. patched ranged from 0.98× to 1.18× by bucket. An index-time control that runs identical code on both builds showed the same per-bucket differences, so these come from the baseline JVM session, not the change.
  • Tests

  • GraphQueryTest.doGraph() (runs across the existing string, docValues-only string and int/long/float/double point field matrix, so both collectors are covered):

  • - a node with children that doesn't match expandFilter is returned but not expanded;
    - a pure-negative expandFilter;
    - a root that doesn't match only returns the root;
    - combined with traversalFilter;
    - combined with maxDepth and returnRoot=false.
  • Ran ./gradlew tidy, ./gradlew check -x test, TestJsonFacets and all org.apache.solr.search.join.* tests.
  • Checklist

  • I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.

  • I have created a Jira issue and added the issue ID to my pull request title.

  • I have given Solr maintainers access to contribute to my PR branch. (optional but recommended, not available for branches on forks living under an organisation)

  • I have developed this patch against the main branch.

  • I have run ./gradlew check.

  • I have added tests for my changes.

  • I have added documentation for the Reference Guide

  • I have added a changelog entry for my change
  • 🤖 Generated with Claude Code

    Diff: 640 lines (+480 / -160) across 8 files Reviewer: nick-boss-tech Review duration: 10072m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 640 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.387
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #5356"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 51.9% (MEDIUM) · Higher = larger blast radius
    51.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%
    Repository