use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project metron by apache.
the class ElasticsearchMetaAlertDao method getAllAlertsForMetaAlert.
@SuppressWarnings("unchecked")
protected List<Map<String, Object>> getAllAlertsForMetaAlert(Document update) throws IOException {
Document latest = indexDao.getLatest(update.getGuid(), MetaAlertDao.METAALERT_TYPE);
if (latest == null) {
return new ArrayList<>();
}
List<String> guids = new ArrayList<>();
List<Map<String, Object>> latestAlerts = (List<Map<String, Object>>) latest.getDocument().get(MetaAlertDao.ALERT_FIELD);
for (Map<String, Object> alert : latestAlerts) {
guids.add((String) alert.get(Constants.GUID));
}
List<Map<String, Object>> alerts = new ArrayList<>();
QueryBuilder query = QueryBuilders.idsQuery().addIds(guids.toArray(new String[0]));
SearchRequestBuilder request = elasticsearchDao.getClient().prepareSearch().setQuery(query);
org.elasticsearch.action.search.SearchResponse response = request.get();
for (SearchHit hit : response.getHits().getHits()) {
alerts.add(hit.sourceAsMap());
}
return alerts;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project pancm_project by xuwujing.
the class IpHandler method main.
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
try {
EsUtil.build("192.169.0.23:9200");
System.out.println("ES连接初始化成功!");
// createIndexTest();
// System.out.println("ES索引库创建成功!");
String index = "student";
String type = "_doc";
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Map<String, Object> map = new HashMap<>();
map.put("id", i);
if (i % 2 == 0) {
map.put("name", "张三");
map.put("age", 16 + i % 4);
} else if (i % 3 == 0) {
map.put("name", "李四");
map.put("age", 17 + i % 6);
} else {
map.put("name", "王五");
map.put("age", 18);
}
list.add(map);
}
EsUtil.setIsAutoClose(false);
saveBulk(list, index, type, "id");
System.out.println("批量写入成功!");
System.out.println("查询的结果1:" + queryById(index, type, "1"));
QueryBuilder queryBuilder = new TermQueryBuilder("name", "xuwujing");
System.out.println("更新的结果:" + updateByQuery(index, type, queryBuilder));
System.out.println("查询的结果2:" + queryById(index, type, "1"));
QueryBuilder queryBuilder3 = QueryBuilders.matchAllQuery();
System.out.println("查询的结果3:" + query(index, type, queryBuilder3));
QueryBuilder queryBuilder4 = QueryBuilders.rangeQuery("age").from(15);
QueryBuilder queryBuilder5 = QueryBuilders.rangeQuery("id").from(5);
System.out.println("查询的结果4:" + query(index, type, queryBuilder4, queryBuilder5));
EsQueryCondition esQueryCondition = new EsQueryCondition();
esQueryCondition.setCloseSource(true);
esQueryCondition.setIndex(1);
esQueryCondition.setPagesize(4);
esQueryCondition.setOrder("desc");
esQueryCondition.setOrderField(new String[] { "age" });
String[] incStrings = new String[] { "age", "name" };
esQueryCondition.setIncludeFields(incStrings);
esQueryCondition.setExcludeFields(new String[] { "id" });
System.out.println("查询的结果5:" + query(index, type, esQueryCondition, queryBuilder4));
// TODO:
} catch (IOException e) {
e.printStackTrace();
} finally {
// TODO: handle finally clause
try {
close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project pancm_project by xuwujing.
the class IpHandler method updateByQuery.
/**
* @return boolean
* @Author pancm
* @Description 根据条件更新
* @Date 2019/3/21
* @Param []
*/
public static Map<String, Object> updateByQuery(String index, String type, QueryBuilder... queryBuilders) throws IOException {
if (index == null || type == null) {
return null;
}
Map<String, Object> map = new HashMap<>();
try {
UpdateByQueryRequest request = new UpdateByQueryRequest();
request.indices(index);
request.setDocTypes(type);
if (queryBuilders != null) {
for (QueryBuilder queryBuilder : queryBuilders) {
request.setQuery(queryBuilder);
}
}
// 同步执行
BulkByScrollResponse bulkResponse = client.updateByQuery(request, RequestOptions.DEFAULT);
// 响应结果处理
map.put("time", bulkResponse.getTook().getMillis());
map.put("total", bulkResponse.getTotal());
} finally {
if (isAutoClose) {
close();
}
}
return map;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project pancm_project by xuwujing.
the class IpHandler method deleteByQuery.
/**
* @return Map
* @Author pancm
* @Description //根据条件删除数据
* @Date 2019/3/21
* @Param []
*/
public static Map<String, Object> deleteByQuery(String index, String type, QueryBuilder[] queryBuilders) throws IOException {
if (index == null || type == null || queryBuilders == null) {
return null;
}
Map<String, Object> map = new HashMap<>();
try {
DeleteByQueryRequest request = new DeleteByQueryRequest(index, type);
if (queryBuilders != null) {
for (QueryBuilder queryBuilder : queryBuilders) {
request.setQuery(queryBuilder);
}
}
// 同步执行
BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
// 响应结果处理
map.put("time", bulkResponse.getTook().getMillis());
map.put("total", bulkResponse.getTotal());
} finally {
if (isAutoClose) {
close();
}
}
return map;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project uavstack by uavorg.
the class InvokeChainQueryHandler method queryByTrace.
/**
* 已知trace id查所有关联trace
*
* @param data
*/
@SuppressWarnings("rawtypes")
private void queryByTrace(UAVHttpMessage data) {
String traceid = data.getRequest("traceid");
if (StringHelper.isEmpty(traceid)) {
data.putResponse("rs", "ERR");
data.putResponse("msg", "No TraceID Found");
return;
}
QueryBuilder qb = QueryBuilders.termQuery("traceid", traceid);
SortBuilder[] sorts = buildSort(data);
String indexDate = getIndexDateFromTraceId(traceid);
data.putRequest("indexdate", indexDate);
queryToList(data, qb, null, sorts);
}
Aggregations