com.elefana.search.agg.RangeAggregation Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright 2018 Viridian Software Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.elefana.search.agg;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.elefana.api.exception.ElefanaException;
import com.elefana.search.PsqlQueryComponents;
import com.jsoniter.ValueType;
import com.jsoniter.any.Any;
public class RangeAggregation extends BucketAggregation {
private static final Logger LOGGER = LoggerFactory.getLogger(RangeAggregation.class);
private static final String KEY_FIELD = "field";
private static final String KEY_RANGES = "ranges";
private static final String KEY_FROM = "from";
private static final String KEY_TO = "to";
private final String aggregationName;
private final String fieldName;
private final Set ranges = new HashSet();
private boolean keyed = false;
public RangeAggregation(String aggregationName, Any context) {
super();
this.aggregationName = aggregationName;
this.fieldName = context.get(KEY_FIELD).toString();
Any rangesContext = context.get(KEY_RANGES);
if(rangesContext.valueType().equals(ValueType.INVALID)) {
return;
}
for(Any rangeContext : rangesContext.asList()) {
Range range = new Range();
Any fromContext = rangeContext.get(KEY_FROM);
Any toContext = rangeContext.get(KEY_TO);
if(!fromContext.valueType().equals(ValueType.INVALID)) {
if(fromContext.toString().indexOf('.') > 0) {
range.doubleFrom = fromContext.toDouble();
} else {
range.longFrom = fromContext.toLong();
}
}
if(!toContext.valueType().equals(ValueType.INVALID)) {
if(toContext.toString().indexOf('.') > 0) {
range.doubleTo = toContext.toDouble();
} else {
range.longTo = toContext.toLong();
}
}
ranges.add(range);
}
}
@Override
public void executeSqlQuery(AggregationExec aggregationExec) throws ElefanaException {
final Map result = new HashMap();
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy