import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import org.elasticsearch.action.search.SearchRequestBuilder;
+import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequestBuilder;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
+import org.elasticsearch.search.sort.SortOrder;
import org.joda.time.format.ISODateTimeFormat;
import org.sonar.core.util.stream.MoreCollectors;
return null;
}
- public static <D extends BaseDoc> Iterator<D> scroll(EsClient esClient, String scrollId, Function<Map<String, Object>, D> docConverter) {
- return new DocScrollIterator<>(esClient, scrollId, docConverter);
+ /**
+ * Optimize scolling, by specifying document sorting.
+ * See https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-scroll.html#search-request-scroll
+ */
+ public static void optimizeScrollRequest(SearchRequestBuilder esSearch) {
+ esSearch.addSort("_doc", SortOrder.ASC);
}
/**
private final Queue<SearchHit> hits = new ArrayDeque<>();
- private DocScrollIterator(EsClient esClient, String scrollId, Function<Map<String, Object>, D> docConverter) {
+ private DocScrollIterator(EsClient esClient, SearchResponse scrollResponse, Function<Map<String, Object>, D> docConverter) {
this.esClient = esClient;
- this.scrollId = scrollId;
+ this.scrollId = scrollResponse.getScrollId();
this.docConverter = docConverter;
+ Collections.addAll(hits, scrollResponse.getHits().getHits());
}
@Override
}
}
- public static <ID> Iterator<ID> scrollIds(EsClient esClient, String scrollId, Function<String, ID> idConverter) {
- return new IdScrollIterator<>(esClient, scrollId, idConverter);
+ public static <ID> Iterator<ID> scrollIds(EsClient esClient, SearchResponse scrollResponse, Function<String, ID> idConverter) {
+ return new IdScrollIterator<>(esClient, scrollResponse, idConverter);
}
private static class IdScrollIterator<ID> implements Iterator<ID> {
private final Queue<SearchHit> hits = new ArrayDeque<>();
- private IdScrollIterator(EsClient esClient, String scrollId, Function<String, ID> idConverter) {
+ private IdScrollIterator(EsClient esClient, SearchResponse scrollResponse, Function<String, ID> idConverter) {
this.esClient = esClient;
- this.scrollId = scrollId;
+ this.scrollId = scrollResponse.getScrollId();
this.idConverter = idConverter;
+ Collections.addAll(hits, scrollResponse.getHits().getHits());
}
@Override
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
-import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.HasParentQueryBuilder;
import static org.sonar.server.es.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
import static org.sonar.server.es.EsUtils.SCROLL_TIME_IN_MINUTES;
import static org.sonar.server.es.EsUtils.escapeSpecialRegexChars;
+import static org.sonar.server.es.EsUtils.optimizeScrollRequest;
import static org.sonar.server.es.EsUtils.scrollIds;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_UUID;
public Iterator<RuleKey> searchAll(RuleQuery query) {
SearchRequestBuilder esSearch = client
.prepareSearch(INDEX_TYPE_RULE)
- .setSearchType(SearchType.SCAN)
.setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
+ optimizeScrollRequest(esSearch);
QueryBuilder qb = buildQuery(query);
Map<String, QueryBuilder> filters = buildFilters(query);
esSearch.setQuery(boolQuery().must(qb).filter(fb));
SearchResponse response = esSearch.get();
- return scrollIds(client, response.getScrollId(), RuleKey::parse);
+ return scrollIds(client, response, RuleKey::parse);
}
/* Build main query (search based) */
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
-import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.unit.TimeValue;
*/
public List<SearchHit> getDocuments(IndexType indexType) {
SearchRequestBuilder req = client.nativeClient().prepareSearch(indexType.getIndex()).setTypes(indexType.getType()).setQuery(QueryBuilders.matchAllQuery());
- req.setSearchType(SearchType.SCAN)
- .setScroll(new TimeValue(60000))
+ EsUtils.optimizeScrollRequest(req);
+ req.setScroll(new TimeValue(60000))
.setSize(100);
SearchResponse response = req.get();