org.apache.solr.handler.component.PivotFacetHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-core Show documentation
Show all versions of solr-core Show documentation
Apache Solr (module: core)
/*
* 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.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.util.PivotListEntry;
public class PivotFacetHelper {
/**
* Encodes a value path as a string for the purposes of a refinement request
*
* @see PivotFacetValue#getValuePath
* @see #decodeRefinementValuePath
*/
public static String encodeRefinementValuePath(List values) {
// HACK: prefix flag every value to account for empty string vs null
// NOTE: even if we didn't have to worry about null's smartSplit is stupid about
// pruning empty strings from list
// "^" prefix = null
// "~" prefix = not null, may be empty string
assert null != values;
// special case: empty list => empty string
if (values.isEmpty()) {
return "";
}
StringBuilder out = new StringBuilder();
for (String val : values) {
if (null == val) {
out.append('^');
} else {
out.append('~');
StrUtils.appendEscapedTextToBuilder(out, val, ',');
}
out.append(',');
}
out.deleteCharAt(out.length() - 1); // prune the last separator
return out.toString();
// return StrUtils.join(values, ',');
}
/**
* Decodes a value path string specified for refinement.
*
* @see #encodeRefinementValuePath
*/
public static List decodeRefinementValuePath(String valuePath) {
List rawvals = StrUtils.splitSmart(valuePath, ",", true);
// special case: empty list => empty string
if (rawvals.isEmpty()) return rawvals;
List out = new ArrayList<>(rawvals.size());
for (String raw : rawvals) {
assert 0 < raw.length();
if ('^' == raw.charAt(0)) {
assert 1 == raw.length();
out.add(null);
} else {
assert '~' == raw.charAt(0);
out.add(raw.substring(1));
}
}
return out;
}
/**
* @see PivotListEntry#VALUE
*/
@SuppressWarnings({"rawtypes"})
public static Comparable getValue(NamedList