@Test public void testGroupSearch() { LindenResult result1 = new LindenResult(); LindenResult result2 = new LindenResult(); LindenResult result3 = new LindenResult(); result1.setHits(new ArrayList<LindenHit>()); result2.setHits(new ArrayList<LindenHit>()); result3.setHits(new ArrayList<LindenHit>()); for (int i = 0; i < 4; ++i) { result1.addToHits(buildGroup("group" + i, i)); result2.addToHits(buildGroup("group" + i, i + 1)); result3.addToHits(buildGroup("group" + i, i + 2)); } result1.setTotalHits(30); result1.setTotalGroups(4); result1.setTotalGroupHits(20); result2.setTotalHits(30); result2.setTotalGroups(4); result2.setTotalGroupHits(20); result3.setTotalHits(30); result3.setTotalGroups(4); result3.setTotalGroupHits(20); List<LindenResult> resultList = new ArrayList<>(); resultList.add(result1); resultList.add(result2); resultList.add(result3); LindenSearchRequest searchRequest = new LindenSearchRequest(); GroupParam groupParam = new GroupParam("group_name"); groupParam.setGroupInnerLimit(10); searchRequest.setGroupParam(groupParam); LindenResult result = ResultMerger.merge(searchRequest, resultList); Assert.assertEquals(90, result.getTotalHits()); Assert.assertEquals(60, result.getTotalGroupHits()); Assert.assertEquals(4, result.getTotalGroups()); Assert.assertEquals(4, result.getHitsSize()); List<LindenHit> groups = result.getHits(); Assert.assertEquals("group3", groups.get(0).getFields().get("group_name")); Assert.assertEquals(10f, groups.get(0).score, DELTA); Assert.assertEquals(10f, groups.get(0).getGroupHits().get(0).score, DELTA); Assert.assertEquals(10, groups.get(0).getGroupHitsSize()); Assert.assertEquals("group2", groups.get(1).getFields().get("group_name")); Assert.assertEquals(9f, groups.get(1).getGroupHits().get(0).score, DELTA); }
public static LindenResult merge(final LindenSearchRequest lindenRequest, final List<LindenResult> resultList) { if (resultList == null || resultList.isEmpty()) { return EMPTY_RESULT; } Iterator<LindenResult> iterator = resultList.iterator(); int failureCount = 0; LindenResult failedResult = null; while (iterator.hasNext()) { LindenResult result = iterator.next(); if (!result.isSuccess()) { failureCount++; failedResult = result; iterator.remove(); } } if (resultList.isEmpty()) { if (failureCount == 1) { LOGGER.error("The shard failed for search request {}", lindenRequest.toString()); return failedResult; } LOGGER.error("All shards failed for search request {}", lindenRequest.toString()); return ALL_SHARDS_FAILED_RESULT; } LindenResult mergedResult; if (lindenRequest.isSetGroupParam()) { mergedResult = mergeGroupSearch(lindenRequest, resultList); } else { mergedResult = new LindenResult().setTotalHits(0).setQueryInfo(resultList.get(0).queryInfo); List<List<LindenHit>> hits = new ArrayList<>(); for (LindenResult result : resultList) { mergedResult.totalHits += result.getTotalHits(); hits.add(result.getHits()); } List<LindenSortField> sortFields = lindenRequest.isSetSort() ? lindenRequest.getSort().getFields() : null; Iterable<LindenHit> mergedHits = Iterables.mergeSorted(hits, new LindenHitCmp(sortFields)); List<LindenHit> topNHits = Lists.newArrayList(mergedHits); if (lindenRequest.getOffset() <= topNHits.size()) { List<LindenHit> subHits = topNHits.subList(lindenRequest.getOffset(), Math.min(lindenRequest.getOffset() + lindenRequest.getLength(), topNHits.size())); mergedResult.setHits(subHits); } else { mergedResult.setHits(new ArrayList<LindenHit>()); } } if (lindenRequest.isSetFacet()) { mergeFacet(lindenRequest, resultList, mergedResult); } if (failureCount > 0) { mergedResult.setError(failureCount + " shards failed."); } return mergedResult; }
ResultMerger { public static LindenResult merge(final LindenSearchRequest lindenRequest, final List<LindenResult> resultList) { if (resultList == null || resultList.isEmpty()) { return EMPTY_RESULT; } Iterator<LindenResult> iterator = resultList.iterator(); int failureCount = 0; LindenResult failedResult = null; while (iterator.hasNext()) { LindenResult result = iterator.next(); if (!result.isSuccess()) { failureCount++; failedResult = result; iterator.remove(); } } if (resultList.isEmpty()) { if (failureCount == 1) { LOGGER.error("The shard failed for search request {}", lindenRequest.toString()); return failedResult; } LOGGER.error("All shards failed for search request {}", lindenRequest.toString()); return ALL_SHARDS_FAILED_RESULT; } LindenResult mergedResult; if (lindenRequest.isSetGroupParam()) { mergedResult = mergeGroupSearch(lindenRequest, resultList); } else { mergedResult = new LindenResult().setTotalHits(0).setQueryInfo(resultList.get(0).queryInfo); List<List<LindenHit>> hits = new ArrayList<>(); for (LindenResult result : resultList) { mergedResult.totalHits += result.getTotalHits(); hits.add(result.getHits()); } List<LindenSortField> sortFields = lindenRequest.isSetSort() ? lindenRequest.getSort().getFields() : null; Iterable<LindenHit> mergedHits = Iterables.mergeSorted(hits, new LindenHitCmp(sortFields)); List<LindenHit> topNHits = Lists.newArrayList(mergedHits); if (lindenRequest.getOffset() <= topNHits.size()) { List<LindenHit> subHits = topNHits.subList(lindenRequest.getOffset(), Math.min(lindenRequest.getOffset() + lindenRequest.getLength(), topNHits.size())); mergedResult.setHits(subHits); } else { mergedResult.setHits(new ArrayList<LindenHit>()); } } if (lindenRequest.isSetFacet()) { mergeFacet(lindenRequest, resultList, mergedResult); } if (failureCount > 0) { mergedResult.setError(failureCount + " shards failed."); } return mergedResult; } }
ResultMerger { public static LindenResult merge(final LindenSearchRequest lindenRequest, final List<LindenResult> resultList) { if (resultList == null || resultList.isEmpty()) { return EMPTY_RESULT; } Iterator<LindenResult> iterator = resultList.iterator(); int failureCount = 0; LindenResult failedResult = null; while (iterator.hasNext()) { LindenResult result = iterator.next(); if (!result.isSuccess()) { failureCount++; failedResult = result; iterator.remove(); } } if (resultList.isEmpty()) { if (failureCount == 1) { LOGGER.error("The shard failed for search request {}", lindenRequest.toString()); return failedResult; } LOGGER.error("All shards failed for search request {}", lindenRequest.toString()); return ALL_SHARDS_FAILED_RESULT; } LindenResult mergedResult; if (lindenRequest.isSetGroupParam()) { mergedResult = mergeGroupSearch(lindenRequest, resultList); } else { mergedResult = new LindenResult().setTotalHits(0).setQueryInfo(resultList.get(0).queryInfo); List<List<LindenHit>> hits = new ArrayList<>(); for (LindenResult result : resultList) { mergedResult.totalHits += result.getTotalHits(); hits.add(result.getHits()); } List<LindenSortField> sortFields = lindenRequest.isSetSort() ? lindenRequest.getSort().getFields() : null; Iterable<LindenHit> mergedHits = Iterables.mergeSorted(hits, new LindenHitCmp(sortFields)); List<LindenHit> topNHits = Lists.newArrayList(mergedHits); if (lindenRequest.getOffset() <= topNHits.size()) { List<LindenHit> subHits = topNHits.subList(lindenRequest.getOffset(), Math.min(lindenRequest.getOffset() + lindenRequest.getLength(), topNHits.size())); mergedResult.setHits(subHits); } else { mergedResult.setHits(new ArrayList<LindenHit>()); } } if (lindenRequest.isSetFacet()) { mergeFacet(lindenRequest, resultList, mergedResult); } if (failureCount > 0) { mergedResult.setError(failureCount + " shards failed."); } return mergedResult; }  }
ResultMerger { public static LindenResult merge(final LindenSearchRequest lindenRequest, final List<LindenResult> resultList) { if (resultList == null || resultList.isEmpty()) { return EMPTY_RESULT; } Iterator<LindenResult> iterator = resultList.iterator(); int failureCount = 0; LindenResult failedResult = null; while (iterator.hasNext()) { LindenResult result = iterator.next(); if (!result.isSuccess()) { failureCount++; failedResult = result; iterator.remove(); } } if (resultList.isEmpty()) { if (failureCount == 1) { LOGGER.error("The shard failed for search request {}", lindenRequest.toString()); return failedResult; } LOGGER.error("All shards failed for search request {}", lindenRequest.toString()); return ALL_SHARDS_FAILED_RESULT; } LindenResult mergedResult; if (lindenRequest.isSetGroupParam()) { mergedResult = mergeGroupSearch(lindenRequest, resultList); } else { mergedResult = new LindenResult().setTotalHits(0).setQueryInfo(resultList.get(0).queryInfo); List<List<LindenHit>> hits = new ArrayList<>(); for (LindenResult result : resultList) { mergedResult.totalHits += result.getTotalHits(); hits.add(result.getHits()); } List<LindenSortField> sortFields = lindenRequest.isSetSort() ? lindenRequest.getSort().getFields() : null; Iterable<LindenHit> mergedHits = Iterables.mergeSorted(hits, new LindenHitCmp(sortFields)); List<LindenHit> topNHits = Lists.newArrayList(mergedHits); if (lindenRequest.getOffset() <= topNHits.size()) { List<LindenHit> subHits = topNHits.subList(lindenRequest.getOffset(), Math.min(lindenRequest.getOffset() + lindenRequest.getLength(), topNHits.size())); mergedResult.setHits(subHits); } else { mergedResult.setHits(new ArrayList<LindenHit>()); } } if (lindenRequest.isSetFacet()) { mergeFacet(lindenRequest, resultList, mergedResult); } if (failureCount > 0) { mergedResult.setError(failureCount + " shards failed."); } return mergedResult; }  static LindenResult merge(final LindenSearchRequest lindenRequest, final List<LindenResult> resultList); }
ResultMerger { public static LindenResult merge(final LindenSearchRequest lindenRequest, final List<LindenResult> resultList) { if (resultList == null || resultList.isEmpty()) { return EMPTY_RESULT; } Iterator<LindenResult> iterator = resultList.iterator(); int failureCount = 0; LindenResult failedResult = null; while (iterator.hasNext()) { LindenResult result = iterator.next(); if (!result.isSuccess()) { failureCount++; failedResult = result; iterator.remove(); } } if (resultList.isEmpty()) { if (failureCount == 1) { LOGGER.error("The shard failed for search request {}", lindenRequest.toString()); return failedResult; } LOGGER.error("All shards failed for search request {}", lindenRequest.toString()); return ALL_SHARDS_FAILED_RESULT; } LindenResult mergedResult; if (lindenRequest.isSetGroupParam()) { mergedResult = mergeGroupSearch(lindenRequest, resultList); } else { mergedResult = new LindenResult().setTotalHits(0).setQueryInfo(resultList.get(0).queryInfo); List<List<LindenHit>> hits = new ArrayList<>(); for (LindenResult result : resultList) { mergedResult.totalHits += result.getTotalHits(); hits.add(result.getHits()); } List<LindenSortField> sortFields = lindenRequest.isSetSort() ? lindenRequest.getSort().getFields() : null; Iterable<LindenHit> mergedHits = Iterables.mergeSorted(hits, new LindenHitCmp(sortFields)); List<LindenHit> topNHits = Lists.newArrayList(mergedHits); if (lindenRequest.getOffset() <= topNHits.size()) { List<LindenHit> subHits = topNHits.subList(lindenRequest.getOffset(), Math.min(lindenRequest.getOffset() + lindenRequest.getLength(), topNHits.size())); mergedResult.setHits(subHits); } else { mergedResult.setHits(new ArrayList<LindenHit>()); } } if (lindenRequest.isSetFacet()) { mergeFacet(lindenRequest, resultList, mergedResult); } if (failureCount > 0) { mergedResult.setError(failureCount + " shards failed."); } return mergedResult; }  static LindenResult merge(final LindenSearchRequest lindenRequest, final List<LindenResult> resultList);  }