@Test public void ResultMergerBasicTest() { LindenSearchRequest lindenRequest = new LindenSearchRequest().setOffset(0).setLength(10); LindenResult result1 = new LindenResult(); LindenResult result2 = new LindenResult(); LindenResult result3 = new LindenResult(); for (int i = 0; i < 10; ++i) { result1.addToHits(new LindenHit("1_" + (9 - i), 9-i)); result2.addToHits(new LindenHit("2_" + (9 - i), (9 - i) * 2)); result3.addToHits(new LindenHit("3_" + (9 - i), (9 - i) * 3)); } result1.setTotalHits(10); result2.setTotalHits(10); result3.setTotalHits(10); List<LindenResult> resultList = new ArrayList<>(); resultList.add(result1); resultList.add(result2); resultList.add(result3); LindenResult mergedResult = ResultMerger.merge(lindenRequest, resultList); Assert.assertEquals(10, mergedResult.getHitsSize()); Assert.assertEquals(30, mergedResult.getTotalHits()); Assert.assertEquals("3_9", mergedResult.getHits().get(0).getId()); Assert.assertEquals(27f, mergedResult.getHits().get(0).getScore(), DELTA); lindenRequest = new LindenSearchRequest().setOffset(0).setLength(40); mergedResult = ResultMerger.merge(lindenRequest, resultList); Assert.assertEquals(30, mergedResult.getHitsSize()); Assert.assertEquals(30, mergedResult.getTotalHits()); lindenRequest = new LindenSearchRequest().setOffset(10).setLength(20); mergedResult = ResultMerger.merge(lindenRequest, resultList); Assert.assertEquals(20, mergedResult.getHitsSize()); Assert.assertEquals(30, mergedResult.getTotalHits()); Assert.assertEquals("2_5", mergedResult.getHits().get(0).getId()); Assert.assertEquals(10f, mergedResult.getHits().get(0).getScore(), DELTA); lindenRequest = new LindenSearchRequest().setOffset(50).setLength(10); mergedResult = ResultMerger.merge(lindenRequest, resultList); Assert.assertEquals(0, mergedResult.getHitsSize()); Assert.assertEquals(30, mergedResult.getTotalHits()); }
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);  }