1 package org.apache.lucene.facet.util;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
7 import org.apache.lucene.util.PriorityQueue;
9 import org.apache.lucene.facet.search.Heap;
10 import org.apache.lucene.facet.search.params.FacetRequest;
11 import org.apache.lucene.facet.search.params.FacetRequest.SortOrder;
12 import org.apache.lucene.facet.search.results.FacetResultNode;
15 * Licensed to the Apache Software Foundation (ASF) under one or more
16 * contributor license agreements. See the NOTICE file distributed with
17 * this work for additional information regarding copyright ownership.
18 * The ASF licenses this file to You under the Apache License, Version 2.0
19 * (the "License"); you may not use this file except in compliance with
20 * the License. You may obtain a copy of the License at
22 * http://www.apache.org/licenses/LICENSE-2.0
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
32 * Utilities for generating facet results sorted as required
34 * @lucene.experimental
36 public class ResultSortUtils {
39 * Create a suitable heap according to facet request being served.
40 * @return heap for maintaining results for specified request.
41 * @throws IllegalArgumentException is provided facet request is not supported
43 public static Heap<FacetResultNode> createSuitableHeap(FacetRequest facetRequest) {
44 int nresults = facetRequest.getNumResults();
45 boolean accending = (facetRequest.getSortOrder() == SortOrder.ASCENDING);
47 if (nresults == Integer.MAX_VALUE) {
48 return new AllValueHeap(accending);
52 switch (facetRequest.getSortBy()) {
54 return new MaxValueHeap(nresults);
56 return new MaxOrdinalHeap(nresults);
59 switch (facetRequest.getSortBy()) {
61 return new MinValueHeap(nresults);
63 return new MinOrdinalHeap(nresults);
66 throw new IllegalArgumentException("none supported facet request: "+facetRequest);
69 private static class MinValueHeap extends PriorityQueue<FacetResultNode> implements Heap<FacetResultNode> {
70 public MinValueHeap(int size) {
75 protected boolean lessThan(FacetResultNode arg0, FacetResultNode arg1) {
76 double value0 = arg0.getValue();
77 double value1 = arg1.getValue();
79 int valueCompare = Double.compare(value0, value1);
80 if (valueCompare == 0) {
81 return arg0.getOrdinal() < arg1.getOrdinal();
84 return valueCompare < 0;
89 private static class MaxValueHeap extends PriorityQueue<FacetResultNode> implements Heap<FacetResultNode> {
90 public MaxValueHeap(int size) {
95 protected boolean lessThan(FacetResultNode arg0, FacetResultNode arg1) {
96 double value0 = arg0.getValue();
97 double value1 = arg1.getValue();
99 int valueCompare = Double.compare(value0, value1);
100 if (valueCompare == 0) {
101 return arg0.getOrdinal() > arg1.getOrdinal();
104 return valueCompare > 0;
108 private static class MinOrdinalHeap extends
109 PriorityQueue<FacetResultNode> implements Heap<FacetResultNode> {
110 public MinOrdinalHeap(int size) {
115 protected boolean lessThan(FacetResultNode arg0, FacetResultNode arg1) {
116 return arg0.getOrdinal() < arg1.getOrdinal();
121 private static class MaxOrdinalHeap extends
122 PriorityQueue<FacetResultNode> implements Heap<FacetResultNode> {
123 public MaxOrdinalHeap(int size) {
128 protected boolean lessThan(FacetResultNode arg0, FacetResultNode arg1) {
129 return arg0.getOrdinal() > arg1.getOrdinal();
135 * Create a Heap-Look-Alike, which implements {@link Heap}, but uses a
136 * regular <code>ArrayList</code> for holding <b>ALL</b> the objects given,
137 * only sorting upon the first call to {@link #pop()}.
139 private static class AllValueHeap implements Heap<FacetResultNode> {
140 private ArrayList<FacetResultNode> resultNodes = new ArrayList<FacetResultNode>();
141 final boolean accending;
142 private boolean isReady = false;
143 public AllValueHeap(boolean accending) {
144 this.accending = accending;
147 public FacetResultNode insertWithOverflow(FacetResultNode node) {
148 resultNodes.add(node);
152 public FacetResultNode pop() {
154 Collections.sort(resultNodes, new Comparator<FacetResultNode>() {
155 public int compare(FacetResultNode o1, FacetResultNode o2) {
156 int value = Double.compare(o1.getValue(), o2
159 value = o1.getOrdinal() - o2.getOrdinal();
170 return resultNodes.remove(0);
174 return resultNodes.size();
177 public FacetResultNode top() {
178 if (resultNodes.size() > 0) {
179 return resultNodes.get(0);
185 public FacetResultNode add(FacetResultNode frn) {
186 resultNodes.add(frn);
190 public void clear() {