Skip to content

Commit baaea0e

Browse files
committed
add size param
1 parent 285ffd9 commit baaea0e

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregationBuilder.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class GeoLineAggregationBuilder
3939
static final ParseField SORT_FIELD = new ParseField("sort");
4040
static final ParseField ORDER_FIELD = new ParseField("sort_order");
4141
static final ParseField INCLUDE_SORT_FIELD = new ParseField("include_sort");
42+
static final ParseField SIZE_FIELD = new ParseField("size");
4243

4344
public static final String NAME = "geo_line";
4445

@@ -50,10 +51,12 @@ public class GeoLineAggregationBuilder
5051
MultiValuesSourceParseHelper.declareField(SORT_FIELD.getPreferredName(), PARSER, true, false, false);
5152
PARSER.declareString((builder, order) -> builder.sortOrder(SortOrder.fromString(order)), ORDER_FIELD);
5253
PARSER.declareBoolean(GeoLineAggregationBuilder::includeSort, INCLUDE_SORT_FIELD);
54+
PARSER.declareInt(GeoLineAggregationBuilder::size, SIZE_FIELD);
5355
}
5456

5557
private boolean includeSort;
5658
private SortOrder sortOrder = SortOrder.ASC;
59+
private int size = GeoLineAggregator.MAX_PATH_SIZE;
5760

5861
public static void registerUsage(ValuesSourceRegistry.Builder builder) {
5962
builder.registerUsage(NAME, CoreValuesSourceType.GEOPOINT);
@@ -87,6 +90,14 @@ public GeoLineAggregationBuilder sortOrder(SortOrder sortOrder) {
8790
return this;
8891
}
8992

93+
public GeoLineAggregationBuilder size(int size) {
94+
if (size > GeoLineAggregator.MAX_PATH_SIZE) {
95+
throw new IllegalArgumentException("invalid [size] value [" + size + "] must be <= " + GeoLineAggregator.MAX_PATH_SIZE);
96+
}
97+
this.size = size;
98+
return this;
99+
}
100+
90101
@Override
91102
protected AggregationBuilder shallowCopy(AggregatorFactories.Builder factoriesBuilder, Map<String, Object> metaData) {
92103
return new GeoLineAggregationBuilder(this, factoriesBuilder, metaData);
@@ -116,7 +127,7 @@ protected MultiValuesSourceAggregatorFactory innerBuild(AggregationContext aggre
116127
AggregatorFactory parent,
117128
AggregatorFactories.Builder subFactoriesBuilder) throws IOException {
118129
return new GeoLineAggregatorFactory(name, configs, format, aggregationContext, parent, subFactoriesBuilder, metadata,
119-
includeSort, sortOrder);
130+
includeSort, sortOrder, size);
120131
}
121132

122133
public GeoLineAggregationBuilder value(MultiValuesSourceFieldConfig valueConfig) {

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ final class GeoLineAggregator extends MetricsAggregator {
3232
private final GeoLineBucketedSort.Extra extra;
3333
private final boolean includeSorts;
3434
private final SortOrder sortOrder;
35+
private final int size;
3536

3637
GeoLineAggregator(String name, MultiValuesSource.AnyMultiValuesSource valuesSources, SearchContext context,
37-
Aggregator parent, Map<String,Object> metaData, boolean includeSorts, SortOrder sortOrder) throws IOException {
38+
Aggregator parent, Map<String,Object> metaData, boolean includeSorts, SortOrder sortOrder,
39+
int size) throws IOException {
3840
super(name, context, parent, metaData);
3941
this.valuesSources = valuesSources;
4042
if (valuesSources != null) {
@@ -46,6 +48,7 @@ final class GeoLineAggregator extends MetricsAggregator {
4648
}
4749
this.includeSorts = includeSorts;
4850
this.sortOrder = sortOrder;
51+
this.size = size;
4952
}
5053

5154
@Override

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregatorFactory.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,24 @@ final class GeoLineAggregatorFactory extends MultiValuesSourceAggregatorFactory
2424

2525
private boolean includeSort;
2626
private SortOrder sortOrder;
27+
private int size;
2728

2829
GeoLineAggregatorFactory(String name,
2930
Map<String, ValuesSourceConfig> configs,
3031
DocValueFormat format, AggregationContext aggregationContext, AggregatorFactory parent,
3132
AggregatorFactories.Builder subFactoriesBuilder,
32-
Map<String, Object> metaData, boolean includeSort, SortOrder sortOrder) throws IOException {
33+
Map<String, Object> metaData, boolean includeSort, SortOrder sortOrder, int size) throws IOException {
3334
super(name, configs, format, aggregationContext, parent, subFactoriesBuilder, metaData);
3435
this.includeSort = includeSort;
3536
this.sortOrder = sortOrder;
37+
this.size = size;
3638
}
3739

3840
@Override
3941
protected Aggregator createUnmapped(SearchContext searchContext,
4042
Aggregator parent,
4143
Map<String, Object> metaData) throws IOException {
42-
return new GeoLineAggregator(name, null, searchContext, parent, metaData, includeSort, sortOrder);
44+
return new GeoLineAggregator(name, null, searchContext, parent, metaData, includeSort, sortOrder, size);
4345
}
4446

4547
@Override
@@ -51,7 +53,7 @@ protected Aggregator doCreateInternal(SearchContext searchContext,
5153
Map<String, Object> metaData) throws IOException {
5254
MultiValuesSource.AnyMultiValuesSource valuesSources =
5355
new MultiValuesSource.AnyMultiValuesSource(configs, searchContext.getQueryShardContext());
54-
return new GeoLineAggregator(name, valuesSources, searchContext, parent, metaData, includeSort, sortOrder);
56+
return new GeoLineAggregator(name, valuesSources, searchContext, parent, metaData, includeSort, sortOrder, size);
5557
}
5658

5759
@Override

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/InternalGeoLine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public InternalAggregation reduce(List<InternalAggregation> aggregations, Reduce
8888
idx += 1;
8989
}
9090
}
91+
// the final reduce should always be in ascending order
9192

92-
new PathArraySorter(finalList, finalSortVals, sortOrder).sort();
93+
new PathArraySorter(finalList, finalSortVals, SortOrder.ASC).sort();
9394
long[] finalCappedList = Arrays.copyOf(finalList, Math.min(GeoLineAggregator.MAX_PATH_SIZE, mergedSize));
9495
double[] finalCappedSortVals = Arrays.copyOf(finalSortVals, Math.min(GeoLineAggregator.MAX_PATH_SIZE, mergedSize));
9596
return new InternalGeoLine(name, finalCappedList, finalCappedSortVals, getMetadata(), complete, includeSorts, sortOrder);

0 commit comments

Comments
 (0)