java
复制代码
protected static final NamedXContentRegistry namedXContentRegistry;
static {
SearchModule searchModule = new SearchModule(Settings.builder().build(), false, new ArrayList<>());
List<NamedXContentRegistry.Entry> namedXContents = searchModule.getNamedXContents();
namedXContents.addAll(getDefaultNamedXContents());
namedXContentRegistry = new NamedXContentRegistry(namedXContents);
}
protected SearchResponse searchResponse(RestHighLevelClient restHighLevelClient, String statement,
Map<String, Object> params, String... indices) {
SearchResponse searchResponse = null;
SearchRequest searchRequest = new SearchRequest(indices);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
try {
searchSourceBuilder.parseXContent(XContentType.JSON.xContent().createParser(namedXContentRegistry,
DeprecationHandler.IGNORE_DEPRECATIONS, statement));
searchRequest.source(searchSourceBuilder);
Object scrollTimeoutObj = params.get(SCROLL_TIMEOUT);
if (null != scrollTimeoutObj) {
searchRequest.scroll(String.valueOf(scrollTimeoutObj));
}
long startTimeMillis = System.currentTimeMillis();
searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
executionSqlLog(statement, startTimeMillis, indices);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return searchResponse;
}
protected SearchResponse scrollSearchResponse(RestHighLevelClient restHighLevelClient, String scrollId) {
try {
return restHighLevelClient.scroll(new SearchScrollRequest(scrollId), RequestOptions.DEFAULT);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return null;
}
protected CountResponse countResponse(RestHighLevelClient restHighLevelClient, String statement,
Map<String, Object> params, String... indices) {
CountResponse countResponse = null;
CountRequest countRequest = new CountRequest(indices);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
try {
searchSourceBuilder.parseXContent(XContentType.JSON.xContent().createParser(namedXContentRegistry,
DeprecationHandler.IGNORE_DEPRECATIONS, statement));
countRequest.source(searchSourceBuilder);
long startTimeMillis = System.currentTimeMillis();
countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
executionSqlLog(statement, startTimeMillis, indices);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return countResponse;
}
private static List<NamedXContentRegistry.Entry> getDefaultNamedXContents() {
Map<String, ContextParser<Object, ? extends Aggregation>> map = new HashMap<>();
map.put(TopHitsAggregationBuilder.NAME, (p, c) -> ParsedTopHits.fromXContent(p, (String) c));
map.put(DateRangeAggregationBuilder.NAME, (p, c) -> ParsedDateRange.fromXContent(p, (String) c));
map.put(FilterAggregationBuilder.NAME, (p, c) -> ParsedFilter.fromXContent(p, (String) c));
map.put(SumAggregationBuilder.NAME, (p, c) -> ParsedSum.fromXContent(p, (String) c));
map.put(CardinalityAggregationBuilder.NAME, (p, c) -> ParsedCardinality.fromXContent(p, (String) c));
map.put(StringTerms.NAME, (p, c) -> ParsedStringTerms.fromXContent(p, (String) c));
map.put(LongTerms.NAME, (p, c) -> ParsedLongTerms.fromXContent(p, (String) c));
map.put(DoubleTerms.NAME, (p, c) -> ParsedDoubleTerms.fromXContent(p, (String) c));
return map.entrySet().stream().map(entry ->
new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))
.collect(Collectors.toList());
}
private void executionSqlLog(String statement, long startTimeMillis, String... indices) {
if (!esProperties.isExecutionSqlEnable()) {
return;
}
long spendTime = System.currentTimeMillis() - startTimeMillis;
log.info(
"\n============== SQL START ==============" +
"\nExecution INX :{} [{} ms]" +
"\nExecution SQL :{}" +
"\n============== SQL END ==============\n", String.join(Constants.SYMBOL_COMMA, indices),
spendTime, statement.replaceAll("\\s{2,}", " "));
}
}