/** * Get a list of all Spans made available from the passed-in Scorer * @param scorer the scorer to extract spans from * @param errorOnNoSpans if true, throw an error if no Spans can be extracted * from the Scorer or any of its children * @return a List of Spans */ public static List<Spans> getSpans(Scorer scorer, boolean errorOnNoSpans) throws IOException { List<Spans> spans = new ArrayList<>(); if (scorer instanceof SpanScorer) { spans.add(((SpanScorer) scorer).getSpans()); return spans; } Collection<Scorer.ChildScorer> children = scorer.getChildren(); if (errorOnNoSpans && children.isEmpty()) throw new RuntimeException("Couldn't extract SpanScorer from " + scorer.getClass().getCanonicalName()); for (Scorer.ChildScorer child : children) { spans.addAll(getSpans(child.child, errorOnNoSpans)); } return spans; }