Skip to content

Commit 83c34f4

Browse files
authored
Revert 74559 (Avoid global ordinals in composite) (#78848)
* Revert "Update docs that composite agg no longer uses global ords (#74754)" This reverts commit ec799ab. * Revert "Avoid global ordinals in composite aggregation (#74559)" This reverts commit 5cfcb2f. Conflicts: server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesCollectorQueue.java server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/OrdinalValuesSource.java
1 parent e9933ea commit 83c34f4

File tree

8 files changed

+222
-425
lines changed

8 files changed

+222
-425
lines changed

docs/reference/mapping/params/eager-global-ordinals.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ ordinal for each segment.
2727
Global ordinals are used if a search contains any of the following components:
2828

2929
* Certain bucket aggregations on `keyword`, `ip`, and `flattened` fields. This
30-
includes `terms` aggregations as mentioned above, as well as
31-
`diversified_sampler` and `significant_terms`.
30+
includes `terms` aggregations as mentioned above, as well as `composite`,
31+
`diversified_sampler`, and `significant_terms`.
3232
* Bucket aggregations on `text` fields that require <<fielddata, `fielddata`>>
3333
to be enabled.
3434
* Operations on parent and child documents from a `join` field, including

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesCollectorQueue.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import org.elasticsearch.search.aggregations.LeafBucketCollector;
1919

2020
import java.io.IOException;
21-
import java.util.ArrayList;
2221
import java.util.HashMap;
23-
import java.util.List;
2422
import java.util.Map;
2523

2624
import static org.elasticsearch.core.Types.forciblyCast;
@@ -60,7 +58,6 @@ public int hashCode() {
6058

6159
private LongArray docCounts;
6260
private boolean afterKeyIsSet = false;
63-
private int leafReaderOrd = -1; // current LeafReaderContext ordinal
6461

6562
/**
6663
* Constructs a composite queue with the specified size and sources.
@@ -235,26 +232,14 @@ LeafBucketCollector getLeafCollector(Comparable<?> forceLeadSourceValue,
235232
LeafReaderContext context, LeafBucketCollector in) throws IOException {
236233
int last = arrays.length - 1;
237234
LeafBucketCollector collector = in;
238-
boolean requiresRehashingWhenSwitchingLeafReaders = false;
239235
while (last > 0) {
240-
SingleDimensionValuesSource<?> valuesSource = arrays[last--];
241-
requiresRehashingWhenSwitchingLeafReaders |= valuesSource.requiresRehashingWhenSwitchingLeafReaders();
242-
collector = valuesSource.getLeafCollector(context, collector);
236+
collector = arrays[last--].getLeafCollector(context, collector);
243237
}
244-
SingleDimensionValuesSource<?> valuesSource = arrays[last];
245-
requiresRehashingWhenSwitchingLeafReaders |= valuesSource.requiresRehashingWhenSwitchingLeafReaders();
246238
if (forceLeadSourceValue != null) {
247-
collector = valuesSource.getLeafCollector(forciblyCast(forceLeadSourceValue), context, collector);
239+
collector = arrays[last].getLeafCollector(forciblyCast(forceLeadSourceValue), context, collector);
248240
} else {
249-
collector = valuesSource.getLeafCollector(context, collector);
241+
collector = arrays[last].getLeafCollector(context, collector);
250242
}
251-
boolean switchedLeafReaders = context.ord != leafReaderOrd;
252-
if (map.isEmpty() == false && requiresRehashingWhenSwitchingLeafReaders && switchedLeafReaders) {
253-
List<Map.Entry<Slot, Integer>> entries = new ArrayList<>(map.entrySet());
254-
map.clear();
255-
entries.forEach(e -> map.put(e.getKey(), e.getValue()));
256-
}
257-
leafReaderOrd = context.ord;
258243
return collector;
259244
}
260245

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.search.aggregations.bucket.composite;
10+
11+
import org.apache.lucene.index.IndexReader;
12+
import org.apache.lucene.index.LeafReaderContext;
13+
import org.apache.lucene.index.SortedSetDocValues;
14+
import org.apache.lucene.search.MatchAllDocsQuery;
15+
import org.apache.lucene.search.Query;
16+
import org.apache.lucene.util.BytesRef;
17+
import org.elasticsearch.common.util.BigArrays;
18+
import org.elasticsearch.common.util.LongArray;
19+
import org.elasticsearch.core.CheckedFunction;
20+
import org.elasticsearch.core.Releasables;
21+
import org.elasticsearch.index.mapper.MappedFieldType;
22+
import org.elasticsearch.index.mapper.StringFieldType;
23+
import org.elasticsearch.search.DocValueFormat;
24+
import org.elasticsearch.search.aggregations.LeafBucketCollector;
25+
26+
import java.io.IOException;
27+
28+
import static org.apache.lucene.index.SortedSetDocValues.NO_MORE_ORDS;
29+
30+
/**
31+
* A {@link SingleDimensionValuesSource} for global ordinals.
32+
*/
33+
class GlobalOrdinalValuesSource extends SingleDimensionValuesSource<BytesRef> {
34+
private final CheckedFunction<LeafReaderContext, SortedSetDocValues, IOException> docValuesFunc;
35+
private LongArray values;
36+
private SortedSetDocValues lookup;
37+
private long currentValue;
38+
private Long afterValueGlobalOrd;
39+
private boolean isTopValueInsertionPoint;
40+
41+
private long lastLookupOrd = -1;
42+
private BytesRef lastLookupValue;
43+
44+
GlobalOrdinalValuesSource(
45+
BigArrays bigArrays,
46+
MappedFieldType type,
47+
CheckedFunction<LeafReaderContext, SortedSetDocValues, IOException> docValuesFunc,
48+
DocValueFormat format,
49+
boolean missingBucket,
50+
int size,
51+
int reverseMul
52+
) {
53+
super(bigArrays, format, type, missingBucket, size, reverseMul);
54+
this.docValuesFunc = docValuesFunc;
55+
this.values = bigArrays.newLongArray(Math.min(size, 100), false);
56+
}
57+
58+
@Override
59+
void copyCurrent(int slot) {
60+
values = bigArrays.grow(values, slot + 1);
61+
values.set(slot, currentValue);
62+
}
63+
64+
@Override
65+
int compare(int from, int to) {
66+
return Long.compare(values.get(from), values.get(to)) * reverseMul;
67+
}
68+
69+
@Override
70+
int compareCurrent(int slot) {
71+
return Long.compare(currentValue, values.get(slot)) * reverseMul;
72+
}
73+
74+
@Override
75+
int compareCurrentWithAfter() {
76+
int cmp = Long.compare(currentValue, afterValueGlobalOrd);
77+
if (cmp == 0 && isTopValueInsertionPoint) {
78+
// the top value is missing in this shard, the comparison is against
79+
// the insertion point of the top value so equality means that the value
80+
// is "after" the insertion point.
81+
return reverseMul;
82+
}
83+
return cmp * reverseMul;
84+
}
85+
86+
@Override
87+
int hashCode(int slot) {
88+
return Long.hashCode(values.get(slot));
89+
}
90+
91+
@Override
92+
int hashCodeCurrent() {
93+
return Long.hashCode(currentValue);
94+
}
95+
96+
@Override
97+
void setAfter(Comparable<?> value) {
98+
if (missingBucket && value == null) {
99+
afterValue = null;
100+
afterValueGlobalOrd = -1L;
101+
} else if (value.getClass() == String.class || (missingBucket && fieldType == null)) {
102+
// the value might be not string if this field is missing in this shard but present in other shards
103+
// and doesn't have a string type
104+
afterValue = format.parseBytesRef(value.toString());
105+
} else {
106+
throw new IllegalArgumentException("invalid value, expected string, got " + value.getClass().getSimpleName());
107+
}
108+
}
109+
110+
@Override
111+
BytesRef toComparable(int slot) throws IOException {
112+
long globalOrd = values.get(slot);
113+
if (missingBucket && globalOrd == -1) {
114+
return null;
115+
} else if (globalOrd == lastLookupOrd) {
116+
return lastLookupValue;
117+
} else {
118+
lastLookupOrd = globalOrd;
119+
lastLookupValue = BytesRef.deepCopyOf(lookup.lookupOrd(values.get(slot)));
120+
return lastLookupValue;
121+
}
122+
}
123+
124+
@Override
125+
LeafBucketCollector getLeafCollector(LeafReaderContext context, LeafBucketCollector next) throws IOException {
126+
final SortedSetDocValues dvs = docValuesFunc.apply(context);
127+
if (lookup == null) {
128+
initLookup(dvs);
129+
}
130+
return new LeafBucketCollector() {
131+
@Override
132+
public void collect(int doc, long bucket) throws IOException {
133+
if (dvs.advanceExact(doc)) {
134+
long ord;
135+
while ((ord = dvs.nextOrd()) != NO_MORE_ORDS) {
136+
currentValue = ord;
137+
next.collect(doc, bucket);
138+
}
139+
} else if (missingBucket) {
140+
currentValue = -1;
141+
next.collect(doc, bucket);
142+
}
143+
}
144+
};
145+
}
146+
147+
@Override
148+
LeafBucketCollector getLeafCollector(Comparable<BytesRef> value, LeafReaderContext context, LeafBucketCollector next)
149+
throws IOException {
150+
if (value.getClass() != BytesRef.class) {
151+
throw new IllegalArgumentException("Expected BytesRef, got " + value.getClass());
152+
}
153+
BytesRef term = (BytesRef) value;
154+
final SortedSetDocValues dvs = docValuesFunc.apply(context);
155+
if (lookup == null) {
156+
initLookup(dvs);
157+
}
158+
return new LeafBucketCollector() {
159+
boolean currentValueIsSet = false;
160+
161+
@Override
162+
public void collect(int doc, long bucket) throws IOException {
163+
if (currentValueIsSet == false) {
164+
if (dvs.advanceExact(doc)) {
165+
long ord;
166+
while ((ord = dvs.nextOrd()) != NO_MORE_ORDS) {
167+
if (term.equals(lookup.lookupOrd(ord))) {
168+
currentValueIsSet = true;
169+
currentValue = ord;
170+
break;
171+
}
172+
}
173+
}
174+
}
175+
assert currentValueIsSet;
176+
next.collect(doc, bucket);
177+
}
178+
};
179+
}
180+
181+
@Override
182+
SortedDocsProducer createSortedDocsProducerOrNull(IndexReader reader, Query query) {
183+
if (checkIfSortedDocsIsApplicable(reader, fieldType) == false
184+
|| fieldType instanceof StringFieldType == false
185+
|| (query != null && query.getClass() != MatchAllDocsQuery.class)) {
186+
return null;
187+
}
188+
return new TermsSortedDocsProducer(fieldType.name());
189+
}
190+
191+
@Override
192+
public void close() {
193+
Releasables.close(values);
194+
}
195+
196+
private void initLookup(SortedSetDocValues dvs) throws IOException {
197+
lookup = dvs;
198+
if (afterValue != null && afterValueGlobalOrd == null) {
199+
afterValueGlobalOrd = lookup.lookupTerm(afterValue);
200+
if (afterValueGlobalOrd < 0) {
201+
// convert negative insert position
202+
afterValueGlobalOrd = -afterValueGlobalOrd - 1;
203+
isTopValueInsertionPoint = true;
204+
}
205+
}
206+
}
207+
}

0 commit comments

Comments
 (0)