Merged searchrep (5.2.1) to 5.2.N (5.2.1)

136989 gjames: SEARCH-430: Combining pivot with range


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@137080 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2017-06-01 11:13:25 +00:00
parent 6ab77f9459
commit fa49623fd8
4 changed files with 280 additions and 94 deletions

View File

@@ -1,66 +1,68 @@
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 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 <http://www.gnu.org/licenses/>.
* #L%
*/
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.search.impl.lucene;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.alfresco.repo.domain.node.NodeDAO;
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;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.search.LimitBy;
import org.alfresco.service.cmr.search.PermissionEvaluationMode;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.ResultSetMetaData;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.cmr.search.SpellCheckResult;
import org.alfresco.util.Pair;
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.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.alfresco.repo.domain.node.NodeDAO;
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.RangeResultMapper;
import org.alfresco.repo.search.impl.solr.facet.facetsresponse.SimpleMetric;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.search.LimitBy;
import org.alfresco.service.cmr.search.PermissionEvaluationMode;
import org.alfresco.service.cmr.search.RangeParameters;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.ResultSetMetaData;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.cmr.search.SpellCheckResult;
import org.alfresco.util.Pair;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
@@ -296,34 +298,17 @@ public class SolrJSONResultSet implements ResultSet, JSONResult
for(Iterator it = facet_pivot.keys(); it.hasNext(); /**/)
{
String pivotName = (String)it.next();
pivotFacets.addAll(buildPivot(facet_pivot, pivotName));
pivotFacets.addAll(buildPivot(facet_pivot, pivotName, searchParameters.getRanges()));
}
}
if(facet_counts.has("facet_ranges"))
{
JSONObject facet_ranges = facet_counts.getJSONObject("facet_ranges");
for(Iterator it = facet_ranges.keys(); it.hasNext();)
{
String fieldName = (String) it.next();
String end = facet_ranges.getJSONObject(fieldName).getString("end");
JSONArray rangeCollection = facet_ranges.getJSONObject(fieldName).getJSONArray("counts");
List<Map<String, String>> buckets = new ArrayList<Map<String, String>>();
for(int i = 0; i < rangeCollection.length(); i+=2)
{
Map<String,String> rangeMap = new HashMap<String,String>(3);
String rangeFrom = rangeCollection.getString(i);
String facetRangeCount = rangeCollection.getString(i+1);
String rangeTo = (i+2 < rangeCollection.length() ? rangeCollection.getString(i+2):end);
String label = rangeFrom + " - " + rangeTo;
rangeMap.put(GenericFacetResponse.LABEL, label);
rangeMap.put(GenericFacetResponse.COUNT, facetRangeCount);
rangeMap.put(GenericFacetResponse.START, rangeFrom);
rangeMap.put(GenericFacetResponse.END, rangeTo);
buckets.add(rangeMap);
}
facetRanges.put(fieldName, buckets);
}
Map<String, List<Map<String, String>>> builtRanges = buildRanges(facet_ranges);
builtRanges.forEach((pKey, buckets) -> {
facetRanges.put(pKey, buckets);
});
}
}
@@ -378,6 +363,35 @@ public class SolrJSONResultSet implements ResultSet, JSONResult
PermissionEvaluationMode.EAGER, searchParameters);
}
protected Map<String,List<Map<String,String>>> buildRanges(JSONObject facet_ranges) throws JSONException
{
Map<String,List<Map<String,String>>> ranges = new HashMap<>();
for(Iterator it = facet_ranges.keys(); it.hasNext();)
{
String fieldName = (String) it.next();
String end = facet_ranges.getJSONObject(fieldName).getString("end");
JSONArray rangeCollection = facet_ranges.getJSONObject(fieldName).getJSONArray("counts");
List<Map<String, String>> buckets = new ArrayList<Map<String, String>>();
for(int i = 0; i < rangeCollection.length(); i+=2)
{
Map<String,String> rangeMap = new HashMap<String,String>(3);
String rangeFrom = rangeCollection.getString(i);
String facetRangeCount = rangeCollection.getString(i+1);
String rangeTo = (i+2 < rangeCollection.length() ? rangeCollection.getString(i+2):end);
String label = rangeFrom + " - " + rangeTo;
rangeMap.put(GenericFacetResponse.LABEL, label);
rangeMap.put(GenericFacetResponse.COUNT, facetRangeCount);
rangeMap.put(GenericFacetResponse.START, rangeFrom);
rangeMap.put(GenericFacetResponse.END, rangeTo);
buckets.add(rangeMap);
}
ranges.put(fieldName, buckets);
}
return ranges;
}
protected Map<String, Map<String, Object>> buildStats(JSONObject statsObj) throws JSONException
{
if(statsObj.has("stats_fields"))
@@ -401,7 +415,7 @@ public class SolrJSONResultSet implements ResultSet, JSONResult
return Collections.emptyMap();
}
protected List<GenericFacetResponse> buildPivot(JSONObject facet_pivot, String pivotName) throws JSONException
protected List<GenericFacetResponse> buildPivot(JSONObject facet_pivot, String pivotName, List<RangeParameters> rangeParameters) throws JSONException
{
if (!facet_pivot.has(pivotName)) return Collections.emptyList();
@@ -412,6 +426,7 @@ public class SolrJSONResultSet implements ResultSet, JSONResult
{
JSONObject piv = pivots.getJSONObject(i);
Set<Metric> metrics = new HashSet<>(1);
List<GenericFacetResponse> nested = new ArrayList<>();
String field = piv.getString("field");
String value = piv.getString("value");
if (piv.has("stats"))
@@ -422,10 +437,20 @@ public class SolrJSONResultSet implements ResultSet, JSONResult
metrics.addAll(getMetrics(pVal));
});
}
Integer count = Integer.parseInt(piv.getString("count"));
metrics.add(new SimpleMetric(METRIC_TYPE.count,count));
List<GenericFacetResponse> innerPivot = buildPivot(piv, "pivot");
GenericBucket buck = new GenericBucket(piv.getString("value"), field+":"+value, null, metrics, innerPivot);
nested.addAll(buildPivot(piv, "pivot", rangeParameters));
if (piv.has("ranges"))
{
JSONObject ranges = piv.getJSONObject("ranges");
Map<String, List<Map<String, String>>> builtRanges = buildRanges(ranges);
List<GenericFacetResponse> rangefacets = RangeResultMapper.getGenericFacetsForRanges(builtRanges,rangeParameters);
nested.addAll(rangefacets);
}
GenericBucket buck = new GenericBucket(value, field+":"+value, null, metrics, nested);
List<GenericBucket> listBucks = pivotBuckets.containsKey(field)?pivotBuckets.get(field):new ArrayList<>();
listBucks.add(buck);
pivotBuckets.put(field, listBucks);

View File

@@ -41,6 +41,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
@@ -819,20 +820,42 @@ public class SolrQueryHTTPClient implements BeanFactoryAware, InitializingBean
List<String> pivotsList = new ArrayList<>();
pivotsList.addAll(pivotKeys);
url.append("&facet.pivot=");
StringBuilder prefix = new StringBuilder("{! ");
if (searchParameters.getStats() != null && !searchParameters.getStats().isEmpty())
{
for (StatsRequestParameters aStat:searchParameters.getStats())
{
if (pivotKeys.contains(aStat.getLabel()))
{
url.append(encoder.encode("{!stats=", "UTF-8"))
.append(encoder.encode(aStat.getLabel(), "UTF-8"))
.append(encoder.encode("}", "UTF-8"));
prefix.append("stats="+aStat.getLabel()+" ");
pivotsList.remove(aStat.getLabel());
break; //only do it once
}
}
}
if (searchParameters.getRanges() != null && !searchParameters.getRanges().isEmpty())
{
for (RangeParameters aRange:searchParameters.getRanges())
{
Optional<String> found = pivotKeys.stream().filter(aKey -> aRange.getTags().contains(aKey)).findFirst();
if (found.isPresent())
{
prefix.append("range="+found.get()+" ");
pivotsList.remove(found.get());
break; //only do it once
}
}
}
if (prefix.length() > 3) //We have add something
{
url.append(encoder.encode(prefix.toString().trim(), "UTF-8"));
url.append(encoder.encode("}", "UTF-8"));
}
url.append(encoder.encode(String.join(",", pivotsList), "UTF-8"));
}
}

View File

@@ -0,0 +1,133 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.search.impl.solr.facet.facetsresponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
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.Metric;
import org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric.METRIC_TYPE;
import org.alfresco.repo.search.impl.solr.facet.facetsresponse.SimpleMetric;
import org.alfresco.service.cmr.search.RangeParameters;
/**Helper to map range results.
*
* @author Michael Suzuki
*/
public class RangeResultMapper
{
/**
* Transforms the facet range response into generic facet response.
* @param facetFields
* @param searchQuery
* @return GenericFacetResponse
*/
public static List<GenericFacetResponse> getGenericFacetsForRanges(Map<String,List<Map<String,String>>> facetFields, List<RangeParameters> ranges)
{
List<GenericFacetResponse> ffcs = new ArrayList<>(facetFields.size());
if (facetFields != null && !facetFields.isEmpty())
{
for (Entry<String, List<Map<String, String>>> facet : facetFields.entrySet())
{
List<GenericBucket> buckets = new ArrayList<>();
facet.getValue().forEach(action -> buckets.add(buildGenericBucketFromRange(facet.getKey(),
(Map<String, String>) action, ranges)));
ffcs.add(new GenericFacetResponse(FACET_TYPE.range, facet.getKey(), buckets));
}
}
return ffcs;
}
/**
* Builds the generic facet response out of range results.
* @param facetField
* @param facet
* @return
*/
private static GenericBucket buildGenericBucketFromRange(String facetField, Map<String,String> facet, List<RangeParameters> ranges)
{
String start = facet.get(GenericFacetResponse.START);
String end = facet.get(GenericFacetResponse.END);
boolean startInclusive = true;
boolean endInclusive = false;
if (ranges!= null) {
for(RangeParameters range : ranges)
{
if(range.getField().equalsIgnoreCase(facetField))
{
List<String> includes = range.getInclude();
if(includes != null && !includes.isEmpty())
{
startInclusive = range.isRangeStartInclusive();
endInclusive = range.isRangeEndInclusive();
}
}
}
}
facet.put(GenericFacetResponse.START_INC.toString(), Boolean.toString(startInclusive));
facet.put(GenericFacetResponse.END_INC.toString(), Boolean.toString(endInclusive));
facet.remove(GenericFacetResponse.LABEL);
StringBuilder filterQ = new StringBuilder();
filterQ.append(facetField).append(":")
.append(startInclusive ? "[" :"<")
.append(start).append(" TO ")
.append(end)
.append(endInclusive ? "]" :">");
Set<Metric> metrics = new HashSet<Metric>(
Arrays.asList(new SimpleMetric(
METRIC_TYPE.count,facet.get(
GenericFacetResponse.COUNT))));
facet.remove("count");
StringBuilder label = new StringBuilder();
label.append(startInclusive ? "[" :"(")
.append(start)
.append(" - ")
.append(end)
.append(endInclusive ? "]" :")");
return new GenericBucket(label.toString(),
filterQ.toString(),
null,
metrics,
null,
facet);
}
}