SolrSearchDao implements SearchDao { protected List<GroupResult> getGroupResults(GroupRequest groupRequest, int index, List<PivotField> pivotFields) { List<Group> groups = groupRequest.getGroups(); List<GroupResult> searchResultGroups = new ArrayList<>(); final GroupOrder groupOrder = groups.get(index).getOrder(); pivotFields.sort((o1, o2) -> { String s1 = groupOrder.getGroupOrderType() == GroupOrderType.TERM ? o1.getValue().toString() : Integer.toString(o1.getCount()); String s2 = groupOrder.getGroupOrderType() == GroupOrderType.TERM ? o2.getValue().toString() : Integer.toString(o2.getCount()); if (groupOrder.getSortOrder() == SortOrder.ASC) { return s1.compareTo(s2); } else { return s2.compareTo(s1); } }); for (PivotField pivotField : pivotFields) { GroupResult groupResult = new GroupResult(); groupResult.setKey(pivotField.getValue().toString()); groupResult.setTotal(pivotField.getCount()); Optional<String> scoreField = groupRequest.getScoreField(); if (scoreField.isPresent()) { groupResult .setScore((Double) pivotField.getFieldStatsInfo().get(scoreField.get()).getSum()); } if (index < groups.size() - 1) { groupResult.setGroupedBy(groups.get(index + 1).getField()); groupResult .setGroupResults(getGroupResults(groupRequest, index + 1, pivotField.getPivot())); } searchResultGroups.add(groupResult); } return searchResultGroups; } SolrSearchDao(SolrClient client, AccessConfig accessConfig); @Override SearchResponse search(SearchRequest searchRequest); SearchResponse search(SearchRequest searchRequest, String fieldList); @Override GroupResponse group(GroupRequest groupRequest);  }
@Test public void getGroupResultsShouldProperlyReturnGroupResults() { GroupRequest groupRequest = new GroupRequest(); Group group1 = new Group(); group1.setField("field1"); GroupOrder groupOrder1 = new GroupOrder(); groupOrder1.setSortOrder("ASC"); groupOrder1.setGroupOrderType("TERM"); group1.setOrder(groupOrder1); Group group2 = new Group(); group2.setField("field2"); GroupOrder groupOrder2 = new GroupOrder(); groupOrder2.setSortOrder("DESC"); groupOrder2.setGroupOrderType("COUNT"); group2.setOrder(groupOrder2); groupRequest.setGroups(Arrays.asList(group1, group2)); groupRequest.setScoreField("score"); PivotField level1Pivot1 = mock(PivotField.class); PivotField level1Pivot2 = mock(PivotField.class); PivotField level2Pivot1 = mock(PivotField.class); PivotField level2Pivot2 = mock(PivotField.class); FieldStatsInfo level1Pivot1FieldStatsInfo = mock(FieldStatsInfo.class); FieldStatsInfo level1Pivot2FieldStatsInfo = mock(FieldStatsInfo.class); FieldStatsInfo level2Pivot1FieldStatsInfo = mock(FieldStatsInfo.class); FieldStatsInfo level2Pivot2FieldStatsInfo = mock(FieldStatsInfo.class); List<PivotField> level1Pivots = Arrays.asList(level1Pivot1, level1Pivot2); List<PivotField> level2Pivots = Arrays.asList(level2Pivot1, level2Pivot2); when(level1Pivot1.getValue()).thenReturn("field1value1"); when(level1Pivot1.getCount()).thenReturn(1); when(level1Pivot1FieldStatsInfo.getSum()).thenReturn(1.0); when(level1Pivot1.getFieldStatsInfo()).thenReturn(new HashMap<String, FieldStatsInfo>() {{ put("score", level1Pivot1FieldStatsInfo); }}); when(level1Pivot2.getValue()).thenReturn("field1value2"); when(level1Pivot2.getCount()).thenReturn(2); when(level1Pivot2FieldStatsInfo.getSum()).thenReturn(2.0); when(level1Pivot2.getFieldStatsInfo()).thenReturn(new HashMap<String, FieldStatsInfo>() {{ put("score", level1Pivot2FieldStatsInfo); }}); when(level2Pivot1.getValue()).thenReturn("field2value1"); when(level2Pivot1.getCount()).thenReturn(3); when(level2Pivot1FieldStatsInfo.getSum()).thenReturn(3.0); when(level2Pivot1.getFieldStatsInfo()).thenReturn(new HashMap<String, FieldStatsInfo>() {{ put("score", level2Pivot1FieldStatsInfo); }}); when(level2Pivot2.getValue()).thenReturn("field2value2"); when(level2Pivot2.getCount()).thenReturn(4); when(level2Pivot2FieldStatsInfo.getSum()).thenReturn(4.0); when(level2Pivot2.getFieldStatsInfo()).thenReturn(new HashMap<String, FieldStatsInfo>() {{ put("score", level2Pivot2FieldStatsInfo); }}); when(level1Pivot1.getPivot()).thenReturn(level2Pivots); List<GroupResult> level1GroupResults = solrSearchDao .getGroupResults(groupRequest, 0, level1Pivots); assertEquals("field1value1", level1GroupResults.get(0).getKey()); assertEquals(1, level1GroupResults.get(0).getTotal()); assertEquals(1.0, level1GroupResults.get(0).getScore(), 0.00001); assertEquals("field2", level1GroupResults.get(0).getGroupedBy()); assertEquals("field1value2", level1GroupResults.get(1).getKey()); assertEquals(2, level1GroupResults.get(1).getTotal()); assertEquals(2.0, level1GroupResults.get(1).getScore(), 0.00001); assertEquals("field2", level1GroupResults.get(1).getGroupedBy()); assertEquals(0, level1GroupResults.get(1).getGroupResults().size()); List<GroupResult> level2GroupResults = level1GroupResults.get(0).getGroupResults(); assertEquals("field2value2", level2GroupResults.get(0).getKey()); assertEquals(4, level2GroupResults.get(0).getTotal()); assertEquals(4.0, level2GroupResults.get(0).getScore(), 0.00001); assertNull(level2GroupResults.get(0).getGroupedBy()); assertNull(level2GroupResults.get(0).getGroupResults()); assertEquals("field2value1", level2GroupResults.get(1).getKey()); assertEquals(3, level2GroupResults.get(1).getTotal()); assertEquals(3.0, level2GroupResults.get(1).getScore(), 0.00001); assertNull(level2GroupResults.get(1).getGroupedBy()); assertNull(level2GroupResults.get(1).getGroupResults()); }