Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.solr.handler.component;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Date;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.FacetParams;
import org.apache.solr.common.params.GroupParams;
import org.apache.solr.common.params.RequiredSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.schema.CurrencyFieldType;
import org.apache.solr.schema.CurrencyValue;
import org.apache.solr.schema.DatePointField;
import org.apache.solr.schema.DateRangeField;
import org.apache.solr.schema.ExchangeRateProvider;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.TrieDateField;
import org.apache.solr.schema.TrieField;
import org.apache.solr.util.DateMathParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Encapsulates a single facet.range request along with all its parameters. This class calculates
* all the ranges (gaps) required to be counted.
*/
public class RangeFacetRequest extends FacetComponent.FacetBase {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
protected final SchemaField schemaField;
protected final String start;
protected final String end;
protected final String gap;
protected final boolean hardEnd;
protected final EnumSet include;
protected final EnumSet others;
protected final FacetParams.FacetRangeMethod method;
protected final int minCount;
protected final boolean groupFacet;
protected final List facetRanges;
/** The computed start value of this range */
protected final Object startObj;
/** The computed end value of this range taking into account facet.range.hardend */
protected final Object endObj;
/** The computed gap between each range */
protected final Object gapObj;
public RangeFacetRequest(ResponseBuilder rb, String f) {
super(rb, FacetParams.FACET_RANGE, f);
IndexSchema schema = rb.req.getSchema();
this.schemaField = schema.getField(facetOn);
SolrParams params = SolrParams.wrapDefaults(localParams, rb.req.getParams());
SolrParams required = new RequiredSolrParams(params);
String methodStr = params.get(FacetParams.FACET_RANGE_METHOD);
FacetParams.FacetRangeMethod method =
(methodStr == null
? FacetParams.FacetRangeMethod.getDefault()
: FacetParams.FacetRangeMethod.get(methodStr));
if ((schemaField.getType() instanceof DateRangeField)
&& method.equals(FacetParams.FacetRangeMethod.DV)) {
// the user has explicitly selected the FacetRangeMethod.DV method
log.warn(
"Range facet method '{}' is not supported together with field type '{}'. Will use method '{}' instead",
FacetParams.FacetRangeMethod.DV,
DateRangeField.class,
FacetParams.FacetRangeMethod.FILTER);
method = FacetParams.FacetRangeMethod.FILTER;
}
if (method.equals(FacetParams.FacetRangeMethod.DV)
&& !schemaField.hasDocValues()
&& (schemaField.getType().isPointField())) {
log.warn(
"Range facet method '{}' is not supported on PointFields without docValues. Will use method '{}' instead",
FacetParams.FacetRangeMethod.DV,
FacetParams.FacetRangeMethod.FILTER);
method = FacetParams.FacetRangeMethod.FILTER;
}
this.start = required.getFieldParam(facetOn, FacetParams.FACET_RANGE_START);
this.end = required.getFieldParam(facetOn, FacetParams.FACET_RANGE_END);
this.gap = required.getFieldParam(facetOn, FacetParams.FACET_RANGE_GAP);
this.minCount = params.getFieldInt(facetOn, FacetParams.FACET_MINCOUNT, 0);
this.include =
FacetParams.FacetRangeInclude.parseParam(
params.getFieldParams(facetOn, FacetParams.FACET_RANGE_INCLUDE));
this.hardEnd = params.getFieldBool(facetOn, FacetParams.FACET_RANGE_HARD_END, false);
this.others = EnumSet.noneOf(FacetParams.FacetRangeOther.class);
final String[] othersP = params.getFieldParams(facetOn, FacetParams.FACET_RANGE_OTHER);
if (othersP != null && othersP.length > 0) {
for (final String o : othersP) {
others.add(FacetParams.FacetRangeOther.get(o));
}
}
this.groupFacet = params.getBool(GroupParams.GROUP_FACET, false);
if (groupFacet && method.equals(FacetParams.FacetRangeMethod.DV)) {
// the user has explicitly selected the FacetRangeMethod.DV method
log.warn(
"Range facet method '{}' is not supported together with '{}'. Will use method '{}' instead",
FacetParams.FacetRangeMethod.DV,
GroupParams.GROUP_FACET,
FacetParams.FacetRangeMethod.FILTER);
method = FacetParams.FacetRangeMethod.FILTER;
}
this.method = method;
RangeEndpointCalculator> calculator = createCalculator();
this.facetRanges = calculator.computeRanges();
this.gapObj = calculator.getGap();
this.startObj = calculator.getStart();
this.endObj = calculator.getComputedEnd();
}
/**
* Creates the right instance of {@link
* org.apache.solr.handler.component.RangeFacetRequest.RangeEndpointCalculator} depending on the
* field type of the schema field
*/
private RangeEndpointCalculator> createCalculator() {
RangeEndpointCalculator calc;
FieldType ft = schemaField.getType();
if (ft instanceof TrieField) {
switch (ft.getNumberType()) {
case FLOAT:
calc = new FloatRangeEndpointCalculator(this);
break;
case DOUBLE:
calc = new DoubleRangeEndpointCalculator(this);
break;
case INTEGER:
calc = new IntegerRangeEndpointCalculator(this);
break;
case LONG:
calc = new LongRangeEndpointCalculator(this);
break;
case DATE:
calc = new DateRangeEndpointCalculator(this, null);
break;
default:
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST,
"Unable to range facet on Trie field of unexpected type:" + this.facetOn);
}
} else if (ft instanceof DateRangeField) {
calc = new DateRangeEndpointCalculator(this, null);
} else if (ft.isPointField()) {
switch (ft.getNumberType()) {
case FLOAT:
calc = new FloatRangeEndpointCalculator(this);
break;
case DOUBLE:
calc = new DoubleRangeEndpointCalculator(this);
break;
case INTEGER:
calc = new IntegerRangeEndpointCalculator(this);
break;
case LONG:
calc = new LongRangeEndpointCalculator(this);
break;
case DATE:
calc = new DateRangeEndpointCalculator(this, null);
break;
default:
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST,
"Unable to range facet on Point field of unexpected type:" + this.facetOn);
}
} else if (ft instanceof CurrencyFieldType) {
calc = new CurrencyRangeEndpointCalculator(this);
} else {
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST, "Unable to range facet on field:" + schemaField);
}
return calc;
}
/**
* @return the start of this range as specified by {@link FacetParams#FACET_RANGE_START} parameter
*/
public String getStart() {
return start;
}
/**
* The end of this facet.range as specified by {@link FacetParams#FACET_RANGE_END} parameter
*
*
Note that the actual computed end value can be different depending on the {@link
* FacetParams#FACET_RANGE_HARD_END} parameter. See {@link #endObj}
*/
public String getEnd() {
return end;
}
/**
* @return an {@link EnumSet} containing all the values specified via {@link
* FacetParams#FACET_RANGE_INCLUDE} parameter. Defaults to {@link
* org.apache.solr.common.params.FacetParams.FacetRangeInclude#LOWER} if no parameter is
* supplied. Includes all values from {@link
* org.apache.solr.common.params.FacetParams.FacetRangeInclude} enum if {@link
* FacetParams#FACET_RANGE_INCLUDE} includes {@link
* org.apache.solr.common.params.FacetParams.FacetRangeInclude#ALL}
*/
public EnumSet getInclude() {
return include;
}
/**
* @return the gap as specified by {@link FacetParams#FACET_RANGE_GAP} parameter
*/
public String getGap() {
return gap;
}
/**
* @return the computed gap object
*/
public Object getGapObj() {
return gapObj;
}
/**
* @return the boolean value of {@link FacetParams#FACET_RANGE_HARD_END} parameter
*/
public boolean isHardEnd() {
return hardEnd;
}
/**
* @return an {@link EnumSet} of {@link org.apache.solr.common.params.FacetParams.FacetRangeOther}
* values specified by {@link FacetParams#FACET_RANGE_OTHER} parameter
*/
public EnumSet getOthers() {
return others;
}
/**
* @return the {@link org.apache.solr.common.params.FacetParams.FacetRangeMethod} to be used for
* computing ranges determined either by the value of {@link FacetParams#FACET_RANGE_METHOD}
* parameter or other internal constraints.
*/
public FacetParams.FacetRangeMethod getMethod() {
return method;
}
/**
* @return the minimum allowed count for facet ranges as specified by {@link
* FacetParams#FACET_MINCOUNT}
*/
public int getMinCount() {
return minCount;
}
/**
* @return the {@link SchemaField} instance representing the field on which ranges have to be
* calculated
*/
public SchemaField getSchemaField() {
return schemaField;
}
/**
* @return the boolean value specified by {@link GroupParams#GROUP_FACET} parameter
*/
public boolean isGroupFacet() {
return groupFacet;
}
/**
* @return a {@link List} of {@link
* org.apache.solr.handler.component.RangeFacetRequest.FacetRange} objects representing the
* ranges (gaps) for which range counts are to be calculated.
*/
public List getFacetRanges() {
return facetRanges;
}
/**
* @return The computed start value of this range
*/
public Object getStartObj() {
return startObj;
}
/**
* The end of this facet.range as calculated using the value of facet.range.end parameter and
* facet.range.hardend. This can be different from the value specified in facet.range.end if
* facet.range.hardend=true
*/
public Object getEndObj() {
return endObj;
}
/**
* Represents a range facet response combined from all shards. Provides helper methods to merge
* facet_ranges response from a shard. See {@link
* #mergeFacetRangesFromShardResponse(LinkedHashMap, SimpleOrderedMap)} and {@link
* #mergeContributionFromShard(SimpleOrderedMap)}
*/
static class DistribRangeFacet {
public SimpleOrderedMap