|
1 | 1 | package org.apache.solr.search.facet;
|
2 | 2 |
|
3 | 3 | import java.util.HashMap;
|
| 4 | +import java.util.LinkedHashMap; |
4 | 5 | import java.util.Map;
|
5 | 6 | import java.util.stream.Collectors;
|
6 | 7 |
|
| 8 | +import org.apache.solr.common.util.NamedList; |
7 | 9 | import org.apache.solr.common.util.SimpleOrderedMap;
|
8 | 10 |
|
9 | 11 | public class TermFrequencyCounter {
|
10 |
| - private final Map<String, Integer> counters; |
| 12 | + private final Map<String, Integer> counts; |
| 13 | + private boolean overflow; |
11 | 14 |
|
12 | 15 | public TermFrequencyCounter() {
|
13 |
| - this.counters = new HashMap<>(); |
| 16 | + this.counts = new HashMap<>(); |
14 | 17 | }
|
15 | 18 |
|
16 |
| - public Map<String, Integer> getCounters() { |
17 |
| - return this.counters; |
| 19 | + public Map<String, Integer> getCounts() { |
| 20 | + return this.counts; |
18 | 21 | }
|
19 | 22 |
|
20 | 23 | public void add(String value) {
|
21 |
| - counters.merge(value, 1, Integer::sum); |
| 24 | + counts.merge(value, 1, Integer::sum); |
22 | 25 | }
|
23 | 26 |
|
24 |
| - public Map<String, Integer> serialize(int limit) { |
25 |
| - if (limit < Integer.MAX_VALUE && limit < counters.size()) { |
26 |
| - return counters.entrySet() |
27 |
| - .stream() |
28 |
| - .sorted((l, r) -> r.getValue() - l.getValue()) // sort by value descending |
29 |
| - .limit(limit) |
30 |
| - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 27 | + public SimpleOrderedMap<Object> serialize(int limit) { |
| 28 | + SimpleOrderedMap<Object> result = new SimpleOrderedMap<>(); |
| 29 | + |
| 30 | + if (limit < counts.size()) { |
| 31 | + result.add("counts", getTopCounts(counts, limit)); |
| 32 | + result.add("overflow", Boolean.TRUE); |
31 | 33 | } else {
|
32 |
| - return counters; |
| 34 | + result.add("counts", counts); |
| 35 | + result.add("overflow", Boolean.FALSE); |
33 | 36 | }
|
| 37 | + |
| 38 | + return result; |
| 39 | + } |
| 40 | + |
| 41 | + private Map<String, Integer> getTopCounts(Map<String, Integer> counters, int limit) { |
| 42 | + return counters.entrySet() |
| 43 | + .stream() |
| 44 | + .sorted((l, r) -> r.getValue() - l.getValue()) // sort by value descending |
| 45 | + .limit(limit) |
| 46 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
34 | 47 | }
|
35 | 48 |
|
36 |
| - public TermFrequencyCounter merge(Map<String, Integer> serialized) { |
37 |
| - serialized.forEach((value, freq) -> counters.merge(value, freq, Integer::sum)); |
| 49 | + public TermFrequencyCounter merge(NamedList<Object> serialized) { |
| 50 | + final Map<String, Integer> counts = (Map<String, Integer>) serialized.get("counts"); |
| 51 | + if (counts != null) { |
| 52 | + counts.forEach((value, freq) -> this.counts.merge(value, freq, Integer::sum)); |
| 53 | + } |
| 54 | + |
| 55 | + final Boolean overflow = (Boolean) serialized.get("overflow"); |
| 56 | + if (overflow != null) { |
| 57 | + this.overflow = this.overflow || overflow; |
| 58 | + } |
38 | 59 |
|
39 | 60 | return this;
|
40 | 61 | }
|
| 62 | + |
| 63 | + public SimpleOrderedMap<Object> toFrequencyOfFrequencies() { |
| 64 | + SimpleOrderedMap<Object> result = new SimpleOrderedMap<>(); |
| 65 | + |
| 66 | + Map<Integer, Integer> frequencies = new LinkedHashMap<>(); |
| 67 | + counts.forEach((value, freq) -> frequencies.merge(freq, 1, Integer::sum)); |
| 68 | + |
| 69 | + result.add("frequencies", frequencies); |
| 70 | + result.add("overflow", overflow); |
| 71 | + |
| 72 | + return result; |
| 73 | + } |
41 | 74 | }
|
0 commit comments