X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java diff --git a/lucene-java-3.5.0/lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java b/lucene-java-3.5.0/lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java new file mode 100644 index 0000000..4d23c8d --- /dev/null +++ b/lucene-java-3.5.0/lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java @@ -0,0 +1,188 @@ +package org.apache.lucene.search.grouping; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.Sort; +import org.apache.lucene.search.SortField; +import org.apache.lucene.search.TopDocs; + +import java.io.IOException; + +/** Represents result returned by a grouping search. + * + * @lucene.experimental */ +public class TopGroups { + /** Number of documents matching the search */ + public final int totalHitCount; + + /** Number of documents grouped into the topN groups */ + public final int totalGroupedHitCount; + + /** The total number of unique groups. If null this value is not computed. */ + public final Integer totalGroupCount; + + /** Group results in groupSort order */ + public final GroupDocs[] groups; + + /** How groups are sorted against each other */ + public final SortField[] groupSort; + + /** How docs are sorted within each group */ + public final SortField[] withinGroupSort; + + public TopGroups(SortField[] groupSort, SortField[] withinGroupSort, int totalHitCount, int totalGroupedHitCount, GroupDocs[] groups) { + this.groupSort = groupSort; + this.withinGroupSort = withinGroupSort; + this.totalHitCount = totalHitCount; + this.totalGroupedHitCount = totalGroupedHitCount; + this.groups = groups; + this.totalGroupCount = null; + } + + public TopGroups(TopGroups oldTopGroups, Integer totalGroupCount) { + this.groupSort = oldTopGroups.groupSort; + this.withinGroupSort = oldTopGroups.withinGroupSort; + this.totalHitCount = oldTopGroups.totalHitCount; + this.totalGroupedHitCount = oldTopGroups.totalGroupedHitCount; + this.groups = oldTopGroups.groups; + this.totalGroupCount = totalGroupCount; + } + + /** Merges an array of TopGroups, for example obtained + * from the second-pass collector across multiple + * shards. Each TopGroups must have been sorted by the + * same groupSort and docSort, and the top groups passed + * to all second-pass collectors must be the same. + * + * NOTE: We can't always compute an exact totalGroupCount. + * Documents belonging to a group may occur on more than + * one shard and thus the merged totalGroupCount can be + * higher than the actual totalGroupCount. In this case the + * totalGroupCount represents a upper bound. If the documents + * of one group do only reside in one shard then the + * totalGroupCount is exact. + * + * NOTE: the topDocs in each GroupDocs is actually + * an instance of TopDocsAndShards + */ + public static TopGroups merge(TopGroups[] shardGroups, Sort groupSort, Sort docSort, int docOffset, int docTopN) + throws IOException { + + //System.out.println("TopGroups.merge"); + + if (shardGroups.length == 0) { + return null; + } + + int totalHitCount = 0; + int totalGroupedHitCount = 0; + // Optionally merge the totalGroupCount. + Integer totalGroupCount = null; + + final int numGroups = shardGroups[0].groups.length; + for(TopGroups shard : shardGroups) { + if (numGroups != shard.groups.length) { + throw new IllegalArgumentException("number of groups differs across shards; you must pass same top groups to all shards' second-pass collector"); + } + totalHitCount += shard.totalHitCount; + totalGroupedHitCount += shard.totalGroupedHitCount; + if (shard.totalGroupCount != null) { + if (totalGroupCount == null) { + totalGroupCount = 0; + } + + totalGroupCount += shard.totalGroupCount; + } + } + + @SuppressWarnings("unchecked") + final GroupDocs[] mergedGroupDocs = new GroupDocs[numGroups]; + + final TopDocs[] shardTopDocs = new TopDocs[shardGroups.length]; + + for(int groupIDX=0;groupIDX