apoorvprecisely commented on a change in pull request #597: [SOLR-13272] feat(facet/interval): support json facet requests for interval facet URL: https://github.com/apache/lucene-solr/pull/597#discussion_r263016047
########## File path: solr/core/src/java/org/apache/solr/search/facet/FacetIntervalProcessor.java ########## @@ -0,0 +1,154 @@ +/* + * 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.search.facet; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import org.apache.solr.common.SolrException; + +class FacetIntervalsProcessor extends FacetRangeProcessor { + final Object interval; + + FacetIntervalsProcessor(FacetContext fcontext, FacetIntervals freq) { + super(fcontext, freq); + if (freq.interval == null) { + throw new SolrException + (SolrException.ErrorCode.BAD_REQUEST, "Interval param is mandatory"); + } + interval = freq.interval; + } + + @Override + public void process() throws IOException { + handleDomainChanges(); + if (fcontext.facetInfo != null) { // refinement? + response = refineFacets(); + } else { + // phase#1: build list of all buckets and return full facets... + if (freq.gap != null) { + throw new SolrException + (SolrException.ErrorCode.BAD_REQUEST, "Cannot set gap and interval param together"); + } + createIntervalRangeList(); + response = getRangeCountsIndexed(); + } + } + + private void createIntervalRangeList() throws IOException { + rangeList = new ArrayList<>(); + otherList = new ArrayList<>(3); + + Comparable low = start; + Comparable loop_end = this.end; + List<Range> ranges = parseInterval(interval); + for (Range range : ranges) { + rangeList.add(range); + } + createOtherList(loop_end); + } + + private List<Range> parseInterval(Object input) { + //interval :[{key:"set1",value:"[0,10]"}] + //@todo apoorv handle exception, sort orders + List<Map<String, String>> intervals = (List<Map<String, String>>) input; + List<Range> ranges = new ArrayList<>(); + for (Map<String, String> interval : intervals) { + String key = interval.get("key"); + if (interval == null) { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "empty facet interval"); + } + String intervalStr = interval.get("value").trim(); + if (intervalStr.length() == 0) { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "empty facet interval"); + } + Boolean startOpen = false, endOpen = false; + Comparable start = null, end = null; + if (intervalStr.charAt(0) == '(') { + startOpen = true; + } else if (intervalStr.charAt(0) == '[') { + startOpen = false; + } else { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid start character " + intervalStr.charAt(0) + " in facet interval " + intervalStr); + } + + final int lastNdx = intervalStr.length() - 1; + if (intervalStr.charAt(lastNdx) == ')') { + endOpen = true; + } else if (intervalStr.charAt(lastNdx) == ']') { + endOpen = false; + } else { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid end character " + intervalStr.charAt(0) + " in facet interval " + intervalStr); + } + + StringBuilder startStr = new StringBuilder(lastNdx); + int i = unescape(intervalStr, 1, lastNdx, startStr); + if (i == lastNdx) { + if (intervalStr.charAt(lastNdx - 1) == ',') { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Empty interval limit"); + } + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Missing unescaped comma separating interval ends in " + intervalStr); + } + try { + start = calc.getValue(startStr.toString()); + } catch (SolrException e) { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, String.format(Locale.ROOT, "Invalid start interval for key '%s': %s", key, e.getMessage()), e); + } + StringBuilder endStr = new StringBuilder(lastNdx); + i = unescape(intervalStr, i, lastNdx, endStr); + if (i != lastNdx) { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Extra unescaped comma at index " + i + " in interval " + intervalStr); + } + try { + end = calc.getValue(endStr.toString()); + } catch (SolrException e) { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, String.format(Locale.ROOT, "Invalid end interval for key '%s': %s", key, e.getMessage()), e); + } + if (start != null && end != null && start.compareTo(end) > 0) { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Start is higher than end in interval for key: " + key); + } + if (interval.get("key") == null) key = startStr.toString(); Review comment: This is what `key` defaults to in the older apis, keeping whole interval range expression would make more sense, will change ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
