diff --git a/source/java/org/alfresco/repo/search/impl/lucene/SolrJSONResultSet.java b/source/java/org/alfresco/repo/search/impl/lucene/SolrJSONResultSet.java index b5a532ebea..2b7c3cc5d5 100644 --- a/source/java/org/alfresco/repo/search/impl/lucene/SolrJSONResultSet.java +++ b/source/java/org/alfresco/repo/search/impl/lucene/SolrJSONResultSet.java @@ -32,6 +32,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import org.alfresco.repo.domain.node.NodeDAO; @@ -39,8 +40,10 @@ import org.alfresco.repo.search.SimpleResultSetMetaData; import org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket; import org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse; import org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse.FACET_TYPE; +import org.alfresco.repo.search.impl.solr.facet.facetsresponse.ListMetric; import org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric; import org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric.METRIC_TYPE; +import org.alfresco.repo.search.impl.solr.facet.facetsresponse.PercentileMetric; import org.alfresco.repo.search.impl.solr.facet.facetsresponse.SimpleMetric; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; @@ -415,12 +418,21 @@ public class SolrJSONResultSet implements ResultSet, JSONResult { return metrics.entrySet().stream().map(aStat -> { METRIC_TYPE metricType = METRIC_TYPE.valueOf(aStat.getKey()); + Object val = aStat.getValue(); + if (JSONObject.NULL.equals(val)) return null; + switch (metricType) { + case distinctValues: + return new ListMetric(metricType, val); + case percentiles: + return new PercentileMetric(metricType, val); + case mean: + if ("NaN".equals(String.valueOf(val))) return null; //else fall through default: - return new SimpleMetric(metricType, String.valueOf(aStat.getValue())); + return new SimpleMetric(metricType, val); } - }).collect(Collectors.toList()); + }).filter(Objects::nonNull).collect(Collectors.toList()); } return Collections.emptyList(); } diff --git a/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/ListMetric.java b/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/ListMetric.java new file mode 100644 index 0000000000..83f49111cb --- /dev/null +++ b/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/ListMetric.java @@ -0,0 +1,81 @@ +/*- + * #%L + * Alfresco Repository + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.repo.search.impl.solr.facet.facetsresponse; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONArray; +import org.json.JSONException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * A metric with one value + */ +public class ListMetric implements Metric +{ + private static Log logger = LogFactory.getLog(ListMetric.class); + private final METRIC_TYPE type; + private final Map value = new HashMap<>(1); + + public ListMetric(METRIC_TYPE type, Object val) + { + this.type = type; + try + { + JSONArray jsonArray = (JSONArray) val; + List values = new ArrayList<>(jsonArray.length()); + for(int i = 0; i < jsonArray.length(); i++) + { + values.add(jsonArray.get(i)); + } + value.put(type.toString(), values); + } + catch (ClassCastException cce) + { + logger.debug("ClassCastException for "+val); + } + catch (JSONException e) + { + logger.debug("Failed to process "+val+ " "+e.getMessage()); + } + } + + @Override + public METRIC_TYPE getType() + { + return type; + } + + @Override + public Map getValue() + { + return value; + } +} diff --git a/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/Metric.java b/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/Metric.java index 37d8efae19..38dc0a6f68 100644 --- a/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/Metric.java +++ b/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/Metric.java @@ -32,7 +32,7 @@ import java.util.Map; */ public interface Metric { - public static enum METRIC_TYPE {count, min, max, sum, missing, sumOfSquares, mean, stddev}; + public static enum METRIC_TYPE {count, min, max, sum, missing, sumOfSquares, mean, stddev, countDistinct, cardinality, distinctValues, percentiles}; METRIC_TYPE getType(); Map getValue(); diff --git a/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/PercentileMetric.java b/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/PercentileMetric.java new file mode 100644 index 0000000000..e28a04720d --- /dev/null +++ b/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/PercentileMetric.java @@ -0,0 +1,82 @@ +/*- + * #%L + * Alfresco Repository + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.repo.search.impl.solr.facet.facetsresponse; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONArray; +import org.json.JSONException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * A metric with one value + */ +public class PercentileMetric implements Metric +{ + private static Log logger = LogFactory.getLog(PercentileMetric.class); + private final METRIC_TYPE type; + private final Map value = new HashMap<>(1); + + public PercentileMetric(METRIC_TYPE type, Object val) + { + this.type = type; + try + { + JSONArray jsonArray = (JSONArray) val; + Map percentiles = new HashMap<>(jsonArray.length()/2); + + for (int i = 0, length = jsonArray.length(); i < length; i++) + { + percentiles.put(jsonArray.getString(i++),jsonArray.getDouble(i)); + } + value.put(type.toString(), percentiles); + } + catch (ClassCastException cce) + { + logger.debug("ClassCastException for "+val); + } + catch (JSONException e) + { + logger.debug("Failed to process percentile for "+val+ " "+e.getMessage()); + } + } + + @Override + public METRIC_TYPE getType() + { + return type; + } + + @Override + public Map getValue() + { + return value; + } +} diff --git a/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/SimpleMetric.java b/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/SimpleMetric.java index b183221b3e..6b001564fb 100644 --- a/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/SimpleMetric.java +++ b/source/java/org/alfresco/repo/search/impl/solr/facet/facetsresponse/SimpleMetric.java @@ -1,3 +1,28 @@ +/*- + * #%L + * Alfresco Repository + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.repo.search.impl.solr.facet.facetsresponse; import java.util.HashMap; @@ -11,7 +36,7 @@ public class SimpleMetric implements Metric private final METRIC_TYPE type; private final Map value = new HashMap<>(1); - public SimpleMetric(METRIC_TYPE type, String val) + public SimpleMetric(METRIC_TYPE type, Object val) { this.type = type; value.put(type.toString(), val);