use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric in project alfresco-remote-api by Alfresco.
the class ResultMapper method getFacetBucketsForFacetFieldsAsFacets.
protected List<GenericFacetResponse> getFacetBucketsForFacetFieldsAsFacets(Map<String, List<Pair<String, Integer>>> facetFields, SearchQuery searchQuery) {
if (facetFields != null && !facetFields.isEmpty()) {
List<GenericFacetResponse> ffcs = new ArrayList<>(facetFields.size());
for (Entry<String, List<Pair<String, Integer>>> facet : facetFields.entrySet()) {
if (facet.getValue() != null && !facet.getValue().isEmpty()) {
List<GenericBucket> buckets = new ArrayList<>(facet.getValue().size());
for (Pair<String, Integer> buck : facet.getValue()) {
Object display = null;
String filterQuery = null;
if (searchQuery != null && searchQuery.getFacetFields() != null && searchQuery.getFacetFields().getFacets() != null && !searchQuery.getFacetFields().getFacets().isEmpty()) {
Optional<FacetField> found = searchQuery.getFacetFields().getFacets().stream().filter(queryable -> facet.getKey().equals(queryable.getLabel() != null ? queryable.getLabel() : queryable.getField())).findFirst();
if (found.isPresent()) {
display = propertyLookup.lookup(found.get().getField(), buck.getFirst());
String fq = found.get().toFilterQuery(buck.getFirst());
if (fq != null) {
filterQuery = fq;
}
}
}
GenericBucket bucket = new GenericBucket(buck.getFirst(), filterQuery, display, new HashSet<Metric>(Arrays.asList(new SimpleMetric(METRIC_TYPE.count, String.valueOf(buck.getSecond())))), null, null);
buckets.add(bucket);
}
ffcs.add(new GenericFacetResponse(FACET_TYPE.field, facet.getKey(), buckets));
}
}
return ffcs;
}
return Collections.emptyList();
}
use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric in project alfresco-remote-api by Alfresco.
the class ResultMapperTests method testFacetingGroupResponse.
@Test
public /**
* Test facet group with out facet fields
* @throws Exception
*/
void testFacetingGroupResponse() throws Exception {
String jsonQuery = "{\"query\": {\"query\": \"alfresco\"}," + "\"facetQueries\": [" + "{\"query\": \"content.size:[o TO 102400]\", \"label\": \"small\",\"group\":\"foo\"}," + "{\"query\": \"content.size:[102400 TO 1048576]\", \"label\": \"medium\",\"group\":\"foo\"}," + "{\"query\": \"content.size:[1048576 TO 16777216]\", \"label\": \"large\",\"group\":\"foo\"}]" + "}";
String expectedResponse = "{\"responseHeader\":{\"status\":0,\"QTime\":9},\"_original_parameters_\":\"org.apache.solr.common.params.DefaultSolrParams:{params(df=TEXT&alternativeDic=DEFAULT_DICTIONARY&fl=DBID,score&start=0&fq={!afts}AUTHORITY_FILTER_FROM_JSON&fq={!afts}TENANT_FILTER_FROM_JSON&rows=1000&locale=en_US&wt=json),defaults(carrot.url=id&spellcheck.collateExtendedResults=true&carrot.produceSummary=true&spellcheck.maxCollations=3&spellcheck.maxCollationTries=5&spellcheck.alternativeTermCount=2&spellcheck.extendedResults=false&defType=afts&spellcheck.maxResultsForSuggest=5&spellcheck=false&carrot.outputSubClusters=false&spellcheck.count=5&carrot.title=mltext@m___t@{http://www.alfresco.org/model/content/1.0}title&carrot.snippet=content@s___t@{http://www.alfresco.org/model/content/1.0}content&spellcheck.collate=true)}\",\"_field_mappings_\":{},\"_date_mappings_\":{},\"_range_mappings_\":{},\"_pivot_mappings_\":{},\"_interval_mappings_\":{},\"_stats_field_mappings_\":{},\"_stats_facet_mappings_\":{},\"_facet_function_mappings_\":{},\"response\":{\"numFound\":6,\"start\":0,\"maxScore\":0.7849362,\"docs\":[{\"DBID\":565,\"score\":0.7849362},{\"DBID\":566,\"score\":0.7849362},{\"DBID\":521,\"score\":0.3540957},{\"DBID\":514,\"score\":0.33025497},{\"DBID\":420,\"score\":0.32440513},{\"DBID\":415,\"score\":0.2780319}]}," + "\"spellcheck\":{\"searchInsteadFor\":\"alfresco\"}," + "\"facet_counts\":{\"facet_queries\": {\"small\": 52,\"large\": 0,\"medium\": 0}}," + "\"processedDenies\":true, \"lastIndexedTx\":34}";
ResultSet results = mockResultset(expectedResponse);
SearchQuery searchQuery = helper.extractFromJson(jsonQuery);
SearchRequestContext searchRequest = SearchRequestContext.from(searchQuery);
SearchContext searchContext = mapper.toSearchContext((SolrJSONResultSet) results, searchRequest, searchQuery, 0);
assertEquals(34l, searchContext.getConsistency().getlastTxId());
assertEquals(null, searchContext.getFacetQueries());
assertEquals(1, searchContext.getFacets().size());
assertEquals(3, searchContext.getFacets().get(0).getBuckets().size());
assertEquals("small", searchContext.getFacets().get(0).getBuckets().get(0).getLabel());
assertEquals("content.size:[o TO 102400]", searchContext.getFacets().get(0).getBuckets().get(0).getFilterQuery());
assertFalse(searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().isEmpty());
Metric[] metrics = searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().toArray(new Metric[searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().size()]);
assertEquals(METRIC_TYPE.count, metrics[0].getType());
assertEquals("{count=52}", metrics[0].getValue().toString());
}
use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric in project alfresco-repository by Alfresco.
the class SolrJSONResultSet method buildPivot.
protected List<GenericFacetResponse> buildPivot(JSONObject facet_pivot, String pivotName, List<RangeParameters> rangeParameters) throws JSONException {
if (!facet_pivot.has(pivotName))
return Collections.emptyList();
JSONArray pivots = facet_pivot.getJSONArray(pivotName);
Map<String, List<GenericBucket>> pivotBuckets = new HashMap<>(pivots.length());
List<GenericFacetResponse> facetResponses = new ArrayList<>();
for (int i = 0; i < pivots.length(); i++) {
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")) {
JSONObject stats = piv.getJSONObject("stats");
Map<String, Map<String, Object>> pivotStats = buildStats(stats);
pivotStats.forEach((pKey, pVal) -> {
metrics.addAll(getMetrics(pVal));
});
}
Integer count = Integer.valueOf(piv.getInt("count"));
metrics.add(new SimpleMetric(METRIC_TYPE.count, count));
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);
}
for (Map.Entry<String, List<GenericBucket>> entry : pivotBuckets.entrySet()) {
facetResponses.add(new GenericFacetResponse(FACET_TYPE.pivot, entry.getKey(), entry.getValue()));
}
if (!facetResponses.isEmpty())
return facetResponses;
return Collections.emptyList();
}
use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric in project alfresco-remote-api by Alfresco.
the class ResultMapper method getGenericFacetsForIntervals.
/**
* Returns generic faceting responses for Intervals
* @param facetFields
* @param searchQuery
* @return GenericFacetResponse
*/
protected static List<GenericFacetResponse> getGenericFacetsForIntervals(Map<String, List<Pair<String, Integer>>> facetFields, SearchQuery searchQuery) {
if (facetFields != null && !facetFields.isEmpty()) {
List<GenericFacetResponse> ffcs = new ArrayList<>(facetFields.size());
for (Entry<String, List<Pair<String, Integer>>> facet : facetFields.entrySet()) {
if (facet.getValue() != null && !facet.getValue().isEmpty()) {
List<GenericBucket> buckets = new ArrayList<>(facet.getValue().size());
for (Pair<String, Integer> buck : facet.getValue()) {
String filterQuery = null;
Map<String, String> bucketInfo = new HashMap<>();
if (searchQuery != null && searchQuery.getFacetIntervals() != null && searchQuery.getFacetIntervals().getIntervals() != null && !searchQuery.getFacetIntervals().getIntervals().isEmpty()) {
Optional<Interval> found = searchQuery.getFacetIntervals().getIntervals().stream().filter(interval -> facet.getKey().equals(interval.getLabel() != null ? interval.getLabel() : interval.getField())).findFirst();
if (found.isPresent()) {
if (found.get().getSets() != null) {
Optional<IntervalSet> foundSet = found.get().getSets().stream().filter(aSet -> buck.getFirst().equals(aSet.getLabel())).findFirst();
if (foundSet.isPresent()) {
filterQuery = found.get().getField() + ":" + foundSet.get().toAFTSQuery();
bucketInfo.put(GenericFacetResponse.START, foundSet.get().getStart());
bucketInfo.put(GenericFacetResponse.END, foundSet.get().getEnd());
bucketInfo.put(GenericFacetResponse.START_INC, String.valueOf(foundSet.get().isStartInclusive()));
bucketInfo.put(GenericFacetResponse.END_INC, String.valueOf(foundSet.get().isEndInclusive()));
}
}
}
}
GenericBucket bucket = new GenericBucket(buck.getFirst(), filterQuery, null, new HashSet<Metric>(Arrays.asList(new SimpleMetric(METRIC_TYPE.count, String.valueOf(buck.getSecond())))), null, bucketInfo);
buckets.add(bucket);
}
ffcs.add(new GenericFacetResponse(FACET_TYPE.interval, facet.getKey(), buckets));
}
}
return ffcs;
}
return Collections.emptyList();
}
use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric in project alfresco-remote-api by Alfresco.
the class ResultMapper method getFacetBucketsFromFacetQueries.
/**
* Builds a facet field from facet queries.
* @param facetQueries
* @return
*/
protected List<GenericFacetResponse> getFacetBucketsFromFacetQueries(Map<String, Integer> facetQueries, SearchQuery searchQuery) {
List<GenericFacetResponse> facetResults = new ArrayList<GenericFacetResponse>();
Map<String, List<GenericBucket>> groups = new HashMap<>();
for (Entry<String, Integer> fq : facetQueries.entrySet()) {
String group = null;
String filterQuery = null;
if (searchQuery != null && searchQuery.getFacetQueries() != null) {
Optional<FacetQuery> found = searchQuery.getFacetQueries().stream().filter(facetQuery -> fq.getKey().equals(facetQuery.getLabel())).findFirst();
filterQuery = found.isPresent() ? found.get().getQuery() : fq.getKey();
if (found.isPresent() && found.get().getGroup() != null) {
group = found.get().getGroup();
}
}
// {
if (groups.containsKey(group)) {
Set<Metric> metrics = new HashSet<>(1);
metrics.add(new SimpleMetric(METRIC_TYPE.count, fq.getValue()));
groups.get(group).add(new GenericBucket(fq.getKey(), filterQuery, null, metrics, null));
} else {
List<GenericBucket> l = new ArrayList<GenericBucket>();
Set<Metric> metrics = new HashSet<>(1);
metrics.add(new SimpleMetric(METRIC_TYPE.count, fq.getValue()));
l.add(new GenericBucket(fq.getKey(), filterQuery, null, metrics, null));
groups.put(group, l);
}
}
// }
if (!groups.isEmpty()) {
groups.forEach((a, v) -> facetResults.add(new GenericFacetResponse(FACET_TYPE.query, a, v)));
}
return facetResults;
}
Aggregations