feats: 规则命中查询

Signed-off-by: feihu.wang <wfh45678@163.com>
This commit is contained in:
feihu.wang
2019-12-25 14:01:37 +08:00
parent 44799b72a0
commit 7c921bef79
5 changed files with 37 additions and 14 deletions

View File

@@ -66,7 +66,7 @@ logging:
root: info
com.pgmmers.radar: info
com.pgmmers.radar.mapper: debug
org.elasticsearch: debug
org.elasticsearch: info
sys:
conf:
app: admin # admin 或者 engine 根据启动的项目名称进行选择

View File

@@ -140,6 +140,7 @@ public class RiskAnalysisEngineServiceImpl implements RiskAnalysisEngineService
* @param info event info and analyze result.
*/
private void sendResult(String modelGuid, String reqId, String info) {
// 这里可以根据情况进行异步处理。
send2ES(modelGuid, info);
}

View File

@@ -17,6 +17,7 @@ import com.pgmmers.radar.service.search.SearchEngineService;
import com.pgmmers.radar.util.GroovyScriptUtil;
import com.pgmmers.radar.vo.model.*;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -44,7 +45,7 @@ public class RuleServiceImpl implements RuleService, SubscribeHandle {
@Autowired
private SearchEngineService searchService;
public Map<Long, List<RuleVO>> contextMap = new HashMap<Long, List<RuleVO>>();
public Map<Long, List<RuleVO>> contextMap = new HashMap<>();
public List<RuleVO> listRule(Long modelId, Long activationId, Integer status) {
return modelDal.listRules(modelId, activationId, status);
@@ -147,9 +148,9 @@ public class RuleServiceImpl implements RuleService, SubscribeHandle {
@Override
public CommonResult getHitSorts(Long modelId) {
CommonResult result = new CommonResult();
Set<RuleHitsVO> treeSet = new TreeSet<RuleHitsVO>();
Set<RuleHitsVO> treeSet = new TreeSet<>();
ModelVO model = modelDal.getModelById(modelId);
String elaQuery = "{\"query\":{\"term\":{\"hitsDetail.${activationName}.rule_${ruleId}.key\":\"${ruleId}\"}}}";
String keyTempl = "hitsDetail.${activationName}.rule_${ruleId}.key";
ActivationQuery actQuery = new ActivationQuery();
actQuery.setModelId(modelId);
PageResult<ActivationVO> actResult = activationDal.query(actQuery);
@@ -161,20 +162,17 @@ public class RuleServiceImpl implements RuleService, SubscribeHandle {
PageResult<RuleVO> page = ruleDal.query(query);
List<RuleVO> list = page.getList();
for (RuleVO rule : list) {
String tmpQuery = elaQuery.replace("${activationName}",
act.getActivationName());
tmpQuery = tmpQuery.replace("${ruleId}", rule.getId() + "");
String keyStr = keyTempl.replace("${activationName}", act.getActivationName());
keyStr = keyStr.replace("${ruleId}", rule.getId() + "");
long qty = 0;
try {
qty = searchService.count(model.getGuid().toLowerCase(),
"radar", tmpQuery, null);
"radar", QueryBuilders.termQuery(keyStr,rule.getId() + ""), null);
} catch (Exception e) {
logger.error("search error", e);
qty = 0;
}
if (qty <= 0) {
continue;
} else {
if (qty > 0){
RuleHitsVO hit = new RuleHitsVO();
hit.setId(rule.getId());
hit.setActivationName(act.getActivationName());

View File

@@ -103,7 +103,29 @@ public class SearchEngineServiceImpl implements SearchEngineService {
@Override
public Long count(String index, String type, String query, String filter) {
return null;
return 0L;
}
@Override
public Long count(String index, String type, QueryBuilder query, QueryBuilder filter) {
SearchResponse response = null;
SearchRequestBuilder builder = client.prepareSearch(index)
.setTypes(type).setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setSize(0).setExplain(true);
if (!StringUtils.isEmpty(query)) {
builder.setQuery(query);
}
if (!StringUtils.isEmpty(filter)) {
builder.setPostFilter(filter);
}
response = builder.execute().actionGet();
RestStatus status = response.status();
if (status.equals(RestStatus.OK)) {
SearchHits hits = response.getHits();
return hits.getTotalHits();
} else {
return null;
}
}
@Override

View File

@@ -21,7 +21,9 @@ public interface SearchEngineService {
SearchHits search(String index, String type, QueryBuilder query, QueryBuilder filter, Integer offset, Integer limit);
Long count(String index, String type, String query, String filter);
Long count(String index, String type, QueryBuilder query, QueryBuilder filter);
double avg(String index, String type, String field, String filter);
double max(String index, String type, String field, String filter);