|
20 | 20 | import org.apache.lucene.search.QueryVisitor; |
21 | 21 | import org.apache.lucene.search.Scorable; |
22 | 22 | import org.apache.lucene.search.ScoreDoc; |
| 23 | +import org.apache.lucene.search.Sort; |
23 | 24 | import org.apache.lucene.search.TopDocs; |
24 | 25 | import org.apache.lucene.search.TotalHits; |
| 26 | +import org.apache.lucene.search.grouping.FirstPassGroupingCollector; |
25 | 27 | import org.apache.lucene.search.grouping.GroupSelector; |
26 | | -import org.apache.lucene.search.grouping.GroupingSearch; |
27 | 28 | import org.apache.lucene.search.grouping.SearchGroup; |
28 | | -import org.apache.lucene.search.grouping.TopGroups; |
29 | 29 | import org.apache.lucene.search.join.BitSetProducer; |
30 | 30 | import org.apache.lucene.util.BitSet; |
31 | 31 | import org.elasticsearch.core.Nullable; |
@@ -217,18 +217,22 @@ private LateRescoreQuery( |
217 | 217 | record DocAndParent(int parent, int doc) {} |
218 | 218 |
|
219 | 219 | @Override |
| 220 | + @SuppressWarnings("unchecked") |
220 | 221 | public Query rewrite(IndexSearcher searcher) throws IOException { |
221 | 222 | final TopDocs topDocs; |
222 | 223 | if (parentFilter != null) { |
223 | 224 | // We're dealing with a nested field, so we need to search at the child level. |
224 | 225 | // We retrieve the top `rescoreK` child documents, but collapse them so that only |
225 | 226 | // the best child per parent is kept. |
226 | | - var groupSearch = new GroupingSearch(new ParentSelector(parentFilter)); |
227 | | - TopGroups<DocAndParent> topGroups = groupSearch.search(searcher, innerQuery, 0, rescoreK); |
228 | | - var scoreDocs = Arrays.stream(topGroups.groups) |
229 | | - .map(g -> new ScoreDoc(g.groupValue().doc, g.score())) |
230 | | - .toArray(ScoreDoc[]::new); |
231 | | - topDocs = new TopDocs(new TotalHits(topGroups.totalHitCount, TotalHits.Relation.EQUAL_TO), scoreDocs); |
| 227 | + FirstPassGroupingCollector<ParentAndDoc> groupingCollector = new FirstPassGroupingCollector<>( |
| 228 | + new ParentSelector(parentFilter), |
| 229 | + Sort.RELEVANCE, |
| 230 | + rescoreK |
| 231 | + ); |
| 232 | + searcher.search(innerQuery, groupingCollector); |
| 233 | + var groups = groupingCollector.getTopGroups(0); |
| 234 | + var scoreDocs = groups.stream().map(g -> new ScoreDoc(g.groupValue.doc, (float) g.sortValues[0])).toArray(ScoreDoc[]::new); |
| 235 | + topDocs = new TopDocs(new TotalHits(scoreDocs.length, TotalHits.Relation.EQUAL_TO), scoreDocs); |
232 | 236 | } else { |
233 | 237 | // Retrieve top `rescoreK` documents from the inner query |
234 | 238 | topDocs = searcher.search(innerQuery, rescoreK); |
|
0 commit comments