*/
@Deprecated
public List<Rule> getByIds(Collection<Integer> ids) {
- SearchResponse response = getClient().prepareSearch(this.getIndexName())
+ SearchResponse scrollResp = getClient().prepareSearch(this.getIndexName())
.setTypes(this.getIndexType())
+ .setSearchType(SearchType.SCAN)
+ .setScroll(TimeValue.timeValueSeconds(3L))
+ .setSize(100)
.setQuery(QueryBuilders.termsQuery(RuleNormalizer.RuleField.ID.field(), ids))
.get();
+
List<Rule> rules = newArrayList();
- for (SearchHit hit : response.getHits()) {
- rules.add(toDoc(hit.getSource()));
+ while (true) {
+ scrollResp = getClient()
+ .prepareSearchScroll(scrollResp.getScrollId())
+ .setScroll(TimeValue.timeValueSeconds(3L))
+ .get();
+ for (SearchHit hit : scrollResp.getHits()) {
+ rules.add(toDoc(hit.getSource()));
+ }
+ //Break condition: No hits are returned
+ if (scrollResp.getHits().getHits().length == 0) {
+ break;
+ }
}
return rules;
}
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
assertThat(index.search(availableSinceNowQuery, new QueryOptions()).getHits()).hasSize(0);
}
+ @Test
+ public void scroll_byIds() throws Exception {
+ Set<Integer> ids = new HashSet<Integer>();
+ for (int i = 0; i < 150; i++) {
+ RuleDto rule = RuleTesting.newDto(RuleKey.of("scroll", "r_" + i));
+ dao.insert(dbSession, rule);
+ dbSession.commit();
+ ids.add(rule.getId());
+ }
+ List<Rule> rules = index.getByIds(ids);
+ assertThat(rules).hasSize(ids.size());
+ }
+
private static List<String> ruleKeys(List<Rule> rules){
return newArrayList(Iterables.transform(rules, new Function<Rule, String>() {
@Override