-
Notifications
You must be signed in to change notification settings - Fork 242
/
TextMultiValueFacetCounts.java
246 lines (217 loc) · 8.48 KB
/
TextMultiValueFacetCounts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.backend.lucene.lowlevel.facet.impl;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import org.hibernate.search.backend.lucene.lowlevel.docvalues.impl.TextMultiValues;
import org.hibernate.search.backend.lucene.lowlevel.docvalues.impl.TextMultiValuesSource;
import com.carrotsearch.hppc.IntHashSet;
import com.carrotsearch.hppc.procedures.IntProcedure;
import org.apache.lucene.facet.FacetResult;
import org.apache.lucene.facet.Facets;
import org.apache.lucene.facet.FacetsCollector;
import org.apache.lucene.facet.FacetsCollector.MatchingDocs;
import org.apache.lucene.facet.LabelAndValue;
import org.apache.lucene.facet.TopOrdAndIntQueue;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultiDocValues;
import org.apache.lucene.index.MultiDocValues.MultiSortedSetDocValues;
import org.apache.lucene.index.OrdinalMap;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LongValues;
/**
* Copied with some changes from {@code org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts}
* of <a href="https://lucene.apache.org/">Apache Lucene project</a>.
*/
public class TextMultiValueFacetCounts extends Facets {
final SortedSetDocValues dv;
final String field;
final int ordCount;
final int[] counts;
public TextMultiValueFacetCounts(IndexReader reader, String field, TextMultiValuesSource valuesSource, FacetsCollector hits)
throws IOException {
this.field = field;
dv = MultiDocValues.getSortedSetValues( reader, field );
if ( dv != null && dv.getValueCount() > Integer.MAX_VALUE ) {
// We may want to remove this limitation?
// Note that DefaultSortedSetDocValuesReaderState has the same limitation,
// so this is no worse than the "legacy" facets from Search 5.
throw new IllegalStateException(
"Cannot aggregate when more than " + Integer.MAX_VALUE + " terms are indexed" );
}
ordCount = dv == null ? 0 : (int) dv.getValueCount();
counts = new int[ordCount];
count( reader, valuesSource, hits.getMatchingDocs() );
}
@Override
public FacetResult getTopChildren(int topN, String dim, String... path) throws IOException {
if ( topN <= 0 ) {
throw new IllegalArgumentException( "topN must be > 0 (got: " + topN + ")" );
}
if ( !dim.equals( field ) ) {
throw new IllegalArgumentException( "invalid dim \"" + dim + "\"; should be \"" + field + "\"" );
}
if ( path.length != 0 ) {
throw new IllegalArgumentException( "path.length should be 0" );
}
return getTopChildrenSortByCount( topN );
}
private FacetResult getTopChildrenSortByCount(int topN) throws IOException {
TopOrdAndIntQueue q = null;
int bottomCount = 0;
int totCount = 0;
int childCount = 0;
TopOrdAndIntQueue.OrdAndValue reuse = null;
for ( int ord = 0; ord < ordCount; ord++ ) {
if ( counts[ord] > 0 ) {
totCount += counts[ord];
childCount++;
if ( counts[ord] > bottomCount ) {
if ( reuse == null ) {
reuse = new TopOrdAndIntQueue.OrdAndValue();
}
reuse.ord = ord;
reuse.value = counts[ord];
if ( q == null ) {
// Lazy init, so we don't create this for the
// sparse case unnecessarily
q = new TopOrdAndIntQueue( topN );
}
reuse = q.insertWithOverflow( reuse );
if ( q.size() == topN ) {
bottomCount = q.top().value;
}
}
}
}
if ( q == null ) {
return null;
}
LabelAndValue[] labelValues = new LabelAndValue[q.size()];
for ( int i = labelValues.length - 1; i >= 0; i-- ) {
TopOrdAndIntQueue.OrdAndValue ordAndValue = q.pop();
final BytesRef term = dv.lookupOrd( ordAndValue.ord );
labelValues[i] = new LabelAndValue( term.utf8ToString(), ordAndValue.value );
}
return new FacetResult( field, new String[0], totCount, labelValues, childCount );
}
private void countOneSegment(OrdinalMap ordinalMap, TextMultiValues segValues, int segOrd, MatchingDocs hits)
throws IOException {
if ( segValues == null ) {
// nothing to count
return;
}
IntHashSet uniqueOrdinalsForDocument = new IntHashSet();
DocIdSetIterator docs = hits.bits.iterator();
// TODO: yet another option is to count all segs
// first, only in seg-ord space, and then do a
// merge-sort-PQ in the end to only "resolve to
// global" those seg ords that can compete, if we know
// we just want top K? ie, this is the same algo
// that'd be used for merging facets across shards
// (distributed faceting). but this has much higher
// temp ram req'ts (sum of number of ords across all
// segs)
if ( ordinalMap != null ) {
final LongValues ordMap = ordinalMap.getGlobalOrds( segOrd );
int numSegOrds = (int) segValues.getValueCount();
if ( hits.totalHits < numSegOrds / 10 ) {
IntProcedure incrementCountForOrdinal = ord -> counts[ord]++;
// Remap every ord to global ord as we iterate:
for ( int doc = docs.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = docs.nextDoc() ) {
if ( !segValues.advanceExact( doc ) ) {
continue; // No value for this doc
}
while ( segValues.hasNextValue() ) {
int term = (int) segValues.nextOrd();
int globalOrd = (int) ordMap.get( term );
uniqueOrdinalsForDocument.add( globalOrd );
}
uniqueOrdinalsForDocument.forEach( incrementCountForOrdinal );
uniqueOrdinalsForDocument.clear();
}
}
else {
// First count in seg-ord space:
final int[] segCounts = new int[numSegOrds];
IntProcedure incrementCountForOrdinal = ord -> segCounts[ord]++;
for ( int doc = docs.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = docs.nextDoc() ) {
if ( !segValues.advanceExact( doc ) ) {
continue; // No value for this doc
}
while ( segValues.hasNextValue() ) {
int term = (int) segValues.nextOrd();
uniqueOrdinalsForDocument.add( term );
}
uniqueOrdinalsForDocument.forEach( incrementCountForOrdinal );
uniqueOrdinalsForDocument.clear();
}
// Then, migrate to global ords:
for ( int ord = 0; ord < numSegOrds; ord++ ) {
int count = segCounts[ord];
if ( count != 0 ) {
counts[(int) ordMap.get( ord )] += count;
}
}
}
}
else {
// No ord mapping (e.g., single segment index):
// just aggregate directly into counts.
IntProcedure incrementCountForOrdinal = ord -> counts[ord]++;
for ( int doc = docs.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = docs.nextDoc() ) {
if ( !segValues.advanceExact( doc ) ) {
continue; // No value for this doc
}
while ( segValues.hasNextValue() ) {
int term = (int) segValues.nextOrd();
uniqueOrdinalsForDocument.add( term );
}
uniqueOrdinalsForDocument.forEach( incrementCountForOrdinal );
uniqueOrdinalsForDocument.clear();
}
}
}
/**
* Does all the "real work" of tallying up the counts.
*/
private void count(IndexReader reader, TextMultiValuesSource valuesSource, List<MatchingDocs> matchingDocs) throws IOException {
OrdinalMap ordinalMap;
// TODO: is this right? really, we need a way to
// verify that this ordinalMap "matches" the leaves in
// matchingDocs...
if ( dv instanceof MultiSortedSetDocValues && matchingDocs.size() > 1 ) {
ordinalMap = ( (MultiSortedSetDocValues) dv ).mapping;
}
else {
ordinalMap = null;
}
for ( MatchingDocs hits : matchingDocs ) {
// LUCENE-5090: make sure the provided reader context "matches"
// the top-level reader passed to the
// SortedSetDocValuesReaderState, else cryptic
// AIOOBE can happen:
if ( ReaderUtil.getTopLevelContext( hits.context ).reader() != reader ) {
throw new IllegalStateException(
"the SortedSetDocValuesReaderState provided to this class does not match the reader being searched; you must create a new SortedSetDocValuesReaderState every time you open a new IndexReader" );
}
countOneSegment( ordinalMap, valuesSource.getValues( hits.context ), hits.context.ord, hits );
}
}
@Override
public Number getSpecificValue(String dim, String... path) {
throw new UnsupportedOperationException();
}
@Override
public List<FacetResult> getAllDims(int topN) throws IOException {
return Collections.singletonList( getTopChildren( topN, field ) );
}
}