use of org.apache.solr.analytics.request.FieldFacetRequest.FacetSortSpecification in project lucene-solr by apache.
the class AnalyticsRequestFactory method setFieldFacetParam.
private static void setFieldFacetParam(IndexSchema schema, Map<String, Map<String, FieldFacetRequest>> fieldFacetMap, String requestName, String field, String paramType, String[] params) {
Map<String, FieldFacetRequest> facetMap = fieldFacetMap.get(requestName);
if (facetMap == null) {
facetMap = new HashMap<>();
fieldFacetMap.put(requestName, facetMap);
}
FieldFacetRequest fr = facetMap.get(field);
if (fr == null) {
fr = new FieldFacetRequest(schema.getField(field));
facetMap.put(field, fr);
}
if (paramType.equals("limit") || paramType.equals("l")) {
fr.setLimit(Integer.parseInt(params[0]));
} else if (paramType.equals("offset") || paramType.equals("off")) {
fr.setOffset(Integer.parseInt(params[0]));
} else if (paramType.equals("hidden") || paramType.equals("h")) {
fr.setHidden(Boolean.parseBoolean(params[0]));
} else if (paramType.equals("showmissing") || paramType.equals("sm")) {
fr.showMissing(Boolean.parseBoolean(params[0]));
} else if (paramType.equals("sortstatistic") || paramType.equals("sortstat") || paramType.equals("ss")) {
fr.setSort(new FacetSortSpecification(params[0], fr.getDirection()));
} else if (paramType.equals("sortdirection") || paramType.equals("sd")) {
fr.setDirection(params[0]);
}
}
use of org.apache.solr.analytics.request.FieldFacetRequest.FacetSortSpecification in project lucene-solr by apache.
the class FacetingAccumulator method export.
@Override
@SuppressWarnings("unchecked")
public NamedList<?> export() {
final NamedList<Object> base = (NamedList<Object>) super.export();
NamedList<NamedList<?>> facetList = new NamedList<>();
// Add the field facet buckets to the output
base.add("fieldFacets", facetList);
for (FieldFacetRequest freq : request.getFieldFacets()) {
final String name = freq.getName();
if (hiddenFieldFacets.contains(name)) {
continue;
}
final Map<String, Expression[]> buckets = fieldFacetExpressions.get(name);
final NamedList<Object> bucketBase = new NamedList<>();
Iterable<Entry<String, Expression[]>> iter = buckets.entrySet();
final FieldFacetRequest fr = (FieldFacetRequest) freq;
final FacetSortSpecification sort = fr.getSort();
final int limit = fr.getLimit();
final int offset = fr.getOffset();
final boolean showMissing = fr.showsMissing();
if (!showMissing) {
buckets.remove(MISSING_VALUE);
}
// Sorting the buckets if a sort specification is provided
if (sort != null && buckets.values().iterator().hasNext()) {
int sortPlace = Arrays.binarySearch(expressionNames, sort.getStatistic());
final Expression first = buckets.values().iterator().next()[sortPlace];
final Comparator<Expression> comp = (Comparator<Expression>) first.comparator(sort.getDirection());
final List<Entry<String, Expression[]>> sorted = new ArrayList<>(buckets.size());
Iterables.addAll(sorted, iter);
Collections.sort(sorted, new EntryComparator(comp, sortPlace));
iter = sorted;
}
// apply the limit
if (limit > AnalyticsContentHandler.DEFAULT_FACET_LIMIT) {
if (offset > 0) {
iter = Iterables.skip(iter, offset);
}
iter = Iterables.limit(iter, limit);
}
// Export each expression in the bucket.
for (Entry<String, Expression[]> bucket : iter) {
bucketBase.add(bucket.getKey(), export(bucket.getValue()));
}
facetList.add(name, bucketBase);
}
// Add the range facet buckets to the output
facetList = new NamedList<>();
base.add("rangeFacets", facetList);
for (RangeFacetRequest freq : request.getRangeFacets()) {
final String name = freq.getName();
final Map<String, Expression[]> buckets = rangeFacetExpressions.get(name);
final NamedList<Object> bucketBase = new NamedList<>();
Iterable<Entry<String, Expression[]>> iter = buckets.entrySet();
for (Entry<String, Expression[]> bucket : iter) {
bucketBase.add(bucket.getKey(), export(bucket.getValue()));
}
facetList.add(name, bucketBase);
}
// Add the query facet buckets to the output
facetList = new NamedList<>();
base.add("queryFacets", facetList);
for (QueryFacetRequest freq : request.getQueryFacets()) {
final String name = freq.getName();
final Map<String, Expression[]> buckets = queryFacetExpressions.get(name);
final NamedList<Object> bucketBase = new NamedList<>();
Iterable<Entry<String, Expression[]>> iter = buckets.entrySet();
for (Entry<String, Expression[]> bucket : iter) {
bucketBase.add(bucket.getKey(), export(bucket.getValue()));
}
facetList.add(name, bucketBase);
}
return base;
}
Aggregations