Search in sources :

Example 51 with QueryBuilder

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;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.apache.metron.indexing.dao.update.Document)

Example 52 with QueryBuilder

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();
        }
    }
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) IOException(java.io.IOException)

Example 53 with QueryBuilder

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;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 54 with QueryBuilder

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;
}
Also used : DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 55 with QueryBuilder

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);
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SortBuilder(org.elasticsearch.search.sort.SortBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)371 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)245 Test (org.testng.annotations.Test)156 DynamicEntity (org.molgenis.data.support.DynamicEntity)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)58 QueryImpl (org.molgenis.data.support.QueryImpl)36 Map (java.util.Map)32 ArrayList (java.util.ArrayList)30 Test (org.junit.Test)30 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)29 HashMap (java.util.HashMap)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)27 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 IOException (java.io.IOException)23 SearchHit (org.elasticsearch.search.SearchHit)22 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)19 List (java.util.List)17 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15