Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

DATASOLR-507 - simple facet pivot with ranges support #84

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/main/java/org/springframework/data/solr/core/ResultHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,34 @@ static Map<org.springframework.data.solr.core.query.PivotField, List<FacetPivotF
for (int i = 0; i < facetPivot.size(); i++) {
String name = facetPivot.getName(i);
List<PivotField> pivotResult = facetPivot.get(name);
facetResult.put(new SimplePivotField(name), convertPivotResult(pivotResult));
facetResult.put(new SimplePivotField(name), convertPivotResult(query, pivotResult));
}
}

return facetResult;
}

private static List<FacetPivotFieldEntry> convertPivotResult(List<PivotField> pivotResult) {
private static List<FacetPivotFieldEntry> convertPivotResult(FacetQuery query, List<PivotField> pivotResult) {
if (CollectionUtils.isEmpty(pivotResult)) {
return Collections.emptyList();
}

ArrayList<FacetPivotFieldEntry> pivotFieldEntries = new ArrayList<>();

for (PivotField pivotField : pivotResult) {
SimpleFacetPivotEntry pivotFieldEntry = new SimpleFacetPivotEntry(new SimpleField(pivotField.getField()),
String.valueOf(pivotField.getValue()), pivotField.getCount());
SimpleFacetPivotEntry pivotFieldEntry;
if(!CollectionUtils.isEmpty(pivotField.getFacetRanges())) {
pivotFieldEntry = new SimpleFacetPivotRangesEntry(new SimpleField(pivotField.getField()),
String.valueOf(pivotField.getValue()), pivotField.getCount(),
convertFacetRangesToRangeFacetPageMap(query, pivotField.getFacetRanges()));
} else {
pivotFieldEntry = new SimpleFacetPivotEntry(new SimpleField(pivotField.getField()),
String.valueOf(pivotField.getValue()), pivotField.getCount());
}

List<PivotField> pivot = pivotField.getPivot();
if (pivot != null) {
pivotFieldEntry.setPivot(convertPivotResult(pivot));
pivotFieldEntry.setPivot(convertPivotResult(query, pivot));
}

pivotFieldEntries.add(pivotFieldEntry);
Expand All @@ -177,9 +184,25 @@ static Map<Field, Page<FacetFieldEntry>> convertFacetQueryResponseToRangeFacetPa
}
Map<Field, Page<FacetFieldEntry>> facetResult = new LinkedHashMap<>();

return convertFacetRangesToRangeFacetPageMap(query, response.getFacetRanges());
}

/**
* @param query
* @param facetRanges
* @return
* @since 1.5
*/
static Map<Field, Page<FacetFieldEntry>> convertFacetRangesToRangeFacetPageMap(FacetQuery query,
List<RangeFacet> facetRanges) {
if (CollectionUtils.isEmpty(facetRanges)) {
return Collections.emptyMap();
}
Map<Field, Page<FacetFieldEntry>> facetResult = new LinkedHashMap<>();

Pageable pageable = query.getFacetOptions().getPageable();
int initalPageSize = pageable.getPageSize();
for (RangeFacet<?, ?> rangeFacet : response.getFacetRanges()) {
for (RangeFacet<?, ?> rangeFacet : facetRanges) {

if (rangeFacet == null || !StringUtils.hasText(rangeFacet.getName())) {
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed 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.springframework.data.solr.core.query.result;

import org.springframework.data.domain.Page;
import org.springframework.data.solr.core.query.Field;

import java.util.Map;
import java.util.Objects;

/**
* Trivial implementation of {@link FacetPivotFieldEntry} with ranges support.
*
* @author Vitezslav Zak
*/
public class SimpleFacetPivotRangesEntry extends SimpleFacetPivotEntry {
private Map<Field, Page<FacetFieldEntry>> facetRanges;

public SimpleFacetPivotRangesEntry(Field field, String value, long count, Map<Field, Page<FacetFieldEntry>> facetRanges) {
super(field, value, count);
this.facetRanges = facetRanges;
}

public Map<Field, Page<FacetFieldEntry>> getFacetRanges() {
return facetRanges;
}

public void setFacetRanges(Map<Field, Page<FacetFieldEntry>> facetRanges) {
this.facetRanges = facetRanges;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SimpleFacetPivotRangesEntry that = (SimpleFacetPivotRangesEntry) o;
return Objects.equals(facetRanges, that.facetRanges);
}

@Override
public int hashCode() {
return Objects.hash(facetRanges);
}

@Override
public String toString() {
return "SimpleFacetPivotRangesEntry [getField()=" + getField() + ", getValueCount()=" + getValueCount() + ", getValue()="
+ getValue() + ", getPivot()=" + getPivot() + ", getFacetRanges()=" + getFacetRanges() + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.springframework.data.solr.core.query.result.GroupResult;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightEntry.Highlight;
import org.springframework.data.solr.core.query.result.SimpleFacetPivotRangesEntry;
import org.springframework.data.solr.core.query.result.SolrResultPage;
import org.springframework.data.solr.core.query.result.StatsResult;
import org.springframework.data.solr.core.query.result.TermsFieldEntry;
Expand Down Expand Up @@ -398,6 +399,63 @@ public void testConvertFacetQueryResponseToFacetPivotMap() {

}

@Test // DATASOLR-507
public void testConvertFacetQueryResponseWithRangesToFacetPivotMap() {
NamedList<List<org.apache.solr.client.solrj.response.PivotField>> pivotData = new NamedList<>();
List<org.apache.solr.client.solrj.response.PivotField> vals = new ArrayList<>();
List<RangeFacet> ranges = new ArrayList<>();
RangeFacet.Numeric rangeFacet1 = new RangeFacet.Numeric("name", 10, 20, 2, 4, 6, 8);
rangeFacet1.addCount("count1", 1);
rangeFacet1.addCount("count2", 2);

RangeFacet.Numeric rangeFacet2 = new RangeFacet.Numeric("", 10, 20, 2, 4, 6, 8);
ranges.add(rangeFacet1);
ranges.add(rangeFacet2);
{
List<org.apache.solr.client.solrj.response.PivotField> pivotValues = new ArrayList<>();
pivotValues
.add(new org.apache.solr.client.solrj.response.PivotField("field_2", "value_1_1", 7, null, null, null, ranges));
pivotValues
.add(new org.apache.solr.client.solrj.response.PivotField("field_2", "value_1_2", 3, null, null, null, null));
vals.add(new org.apache.solr.client.solrj.response.PivotField("field_1", "value_1", 10, pivotValues, null, null,
ranges));
}
{
List<org.apache.solr.client.solrj.response.PivotField> pivotValues = new ArrayList<>();
pivotValues
.add(new org.apache.solr.client.solrj.response.PivotField("field_2", "value_2_1", 2, null, null, null, ranges));
vals.add(
new org.apache.solr.client.solrj.response.PivotField("field_1", "value_2", 2, pivotValues, null, null, null));
}
pivotData.add("field_1,field_2", vals);

Mockito.when(response.getFacetPivot()).thenReturn(pivotData);

Map<PivotField, List<FacetPivotFieldEntry>> result = ResultHelper
.convertFacetQueryResponseToFacetPivotMap(createFacetPivotQuery("field_1", "field_2"), response);

List<FacetPivotFieldEntry> resultPivot = result.get(new SimplePivotField("field_1", "field_2"));
Assert.assertNotNull(result);
Assert.assertEquals(2, resultPivot.size());

Assert.assertNotNull(resultPivot.get(0));
Assert.assertEquals(SimpleFacetPivotRangesEntry.class, resultPivot.get(0).getClass());

{
List<FacetPivotFieldEntry> pivot = resultPivot.get(0).getPivot();
Assert.assertNotNull(pivot.get(0));
Assert.assertEquals(SimpleFacetPivotRangesEntry.class, pivot.get(0).getClass());
Assert.assertNotNull(pivot.get(1));
Assert.assertNotEquals(SimpleFacetPivotRangesEntry.class, pivot.get(1).getClass());
}

{
List<FacetPivotFieldEntry> pivot = resultPivot.get(1).getPivot();
Assert.assertNotNull(pivot.get(0));
Assert.assertEquals(SimpleFacetPivotRangesEntry.class, pivot.get(0).getClass());
}
}

@Test
public void testConvertTermsQueryResponseReturnsTermsMapCorrectlyWhenOneFieldReturned() {
TermsResponse termsResponse = new TermsResponse(new NamedList<>());
Expand Down