1
package org.apache.solr.search.grouping.endresulttransformer;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.lucene.search.ScoreDoc;
21
import org.apache.lucene.search.grouping.GroupDocs;
22
import org.apache.lucene.search.grouping.TopGroups;
23
import org.apache.lucene.util.BytesRef;
24
import org.apache.solr.common.SolrDocumentList;
25
import org.apache.solr.common.util.NamedList;
26
import org.apache.solr.common.util.SimpleOrderedMap;
27
import org.apache.solr.response.SolrQueryResponse;
28
import org.apache.solr.search.grouping.GroupingSpecification;
35
public class SimpleEndResultTransformer implements EndResultTransformer {
37
public void transform(Map<String, ?> result, SolrQueryResponse response, GroupingSpecification groupingSpecification, SolrDocumentSource solrDocumentSource) {
38
NamedList<Object> commands = new SimpleOrderedMap<Object>();
39
for (Map.Entry<String, ?> entry : result.entrySet()) {
40
Object value = entry.getValue();
41
if (TopGroups.class.isInstance(value)) {
42
@SuppressWarnings("unchecked")
43
TopGroups<BytesRef> topGroups = (TopGroups<BytesRef>) value;
44
NamedList<Object> command = new SimpleOrderedMap<Object>();
45
command.add("matches", topGroups.totalHitCount);
46
if (topGroups.totalGroupCount != null) {
47
command.add("ngroups", topGroups.totalGroupCount);
49
SolrDocumentList docList = new SolrDocumentList();
50
docList.setStart(groupingSpecification.getOffset());
51
docList.setNumFound(topGroups.totalHitCount);
53
Float maxScore = Float.NEGATIVE_INFINITY;
54
for (GroupDocs<BytesRef> group : topGroups.groups) {
55
for (ScoreDoc scoreDoc : group.scoreDocs) {
56
if (maxScore < scoreDoc.score) {
57
maxScore = scoreDoc.score;
59
docList.add(solrDocumentSource.retrieve(scoreDoc));
62
if (maxScore != Float.NEGATIVE_INFINITY) {
63
docList.setMaxScore(maxScore);
65
command.add("doclist", docList);
66
commands.add(entry.getKey(), command);
70
response.add("grouped", commands);