~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/grouping/endresulttransformer/SimpleEndResultTransformer.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.solr.search.grouping.endresulttransformer;
2
 
 
3
 
/*
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
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;
29
 
 
30
 
import java.util.Map;
31
 
 
32
 
/**
33
 
 *
34
 
 */
35
 
public class SimpleEndResultTransformer implements EndResultTransformer {
36
 
 
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);
48
 
        }
49
 
        SolrDocumentList docList = new SolrDocumentList();
50
 
        docList.setStart(groupingSpecification.getOffset());
51
 
        docList.setNumFound(topGroups.totalHitCount);
52
 
 
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;
58
 
            }
59
 
            docList.add(solrDocumentSource.retrieve(scoreDoc));
60
 
          }
61
 
        }
62
 
        if (maxScore != Float.NEGATIVE_INFINITY) {
63
 
          docList.setMaxScore(maxScore);
64
 
        }
65
 
        command.add("doclist", docList);
66
 
        commands.add(entry.getKey(), command);
67
 
      }
68
 
    }
69
 
 
70
 
    response.add("grouped", commands);
71
 
  }
72
 
}