Search in sources :

Example 31 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder in project pancm_project by xuwujing.

the class EsAggregationSearchTest method havingSearch.

/**
 * @Author pancm
 * @Description having
 * @Date  2020/8/21
 * @Param []
 * @return void
 */
private static void havingSearch() throws IOException {
    String index = "";
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.indices(index);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
    String alias_name = "nas_ip_address_group";
    String group_name = "nas_ip_address";
    String query_name = "acct_start_time";
    String query_type = "gte,lte";
    String query_name_value = "2020-08-05 13:25:55,2020-08-20 13:26:55";
    String[] query_types = query_type.split(",");
    String[] query_name_values = query_name_value.split(",");
    for (int i = 0; i < query_types.length; i++) {
        if ("gte".equals(query_types[i])) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery(query_name).gte(query_name_values[i]));
        }
        if ("lte".equals(query_types[i])) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery(query_name).lte(query_name_values[i]));
        }
    }
    AggregationBuilder aggregationBuilder = AggregationBuilders.terms(alias_name).field(group_name).size(Integer.MAX_VALUE);
    // 声明BucketPath,用于后面的bucket筛选
    Map<String, String> bucketsPathsMap = new HashMap<>(8);
    bucketsPathsMap.put("groupCount", "_count");
    // 设置脚本
    Script script = new Script("params.groupCount >= 1000");
    // 构建bucket选择器
    BucketSelectorPipelineAggregationBuilder bs = PipelineAggregatorBuilders.bucketSelector("having", bucketsPathsMap, script);
    aggregationBuilder.subAggregation(bs);
    sourceBuilder.aggregation(aggregationBuilder);
    // 不需要解释
    sourceBuilder.explain(false);
    // 不需要原始数据
    sourceBuilder.fetchSource(false);
    // 不需要版本号
    sourceBuilder.version(false);
    sourceBuilder.query(boolQueryBuilder);
    searchRequest.source(sourceBuilder);
    System.out.println(sourceBuilder);
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    // 查询条数
    long count = searchResponse.getHits().getHits().length;
    Aggregations aggregations = searchResponse.getAggregations();
    // agg(aggregations);
    Map<String, Object> map = new HashMap<>();
    List<Map<String, Object>> list = new ArrayList<>();
    agg(list, aggregations);
    // System.out.println(map);
    System.out.println(list);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Script(org.elasticsearch.script.Script) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) CardinalityAggregationBuilder(org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BucketSelectorPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder) HashMap(java.util.HashMap) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) BucketSelectorPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder in project pancm_project by xuwujing.

the class EsHighLevelRestTest1 method updateByQuery.

/**
 * 根据查询条件更新
 *
 * @throws IOException
 */
private static void updateByQuery() throws IOException {
    String type = "_doc";
    String index = "test1";
    // 
    UpdateByQueryRequest request = new UpdateByQueryRequest(index, type);
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.must(QueryBuilders.termQuery("user", "pancm"));
    // Script script = new Script(ScriptType.INLINE, SCRIPT_NAME, id, map);
    // boolQueryBuilder.must(QueryBuilders.scriptQuery(script));
    // boolQueryBuilder.must(QueryBuilders.scriptQuery(script));
    // // 设置查询条件
    // request.setQuery(new TermQueryBuilder("user", "pancm"));
    // 设置复制文档的数量
    request.setSize(10);
    // 设置一次批量处理的条数,默认是1000
    request.setBatchSize(100);
    // 设置超时时间
    request.setTimeout(TimeValue.timeValueMinutes(2));
    // 索引选项
    request.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
    // 同步执行
    BulkByScrollResponse bulkResponse = client.updateByQuery(request, RequestOptions.DEFAULT);
    // 异步执行
    // client.updateByQueryAsync(request, RequestOptions.DEFAULT, listener);
    // 返回结果
    TimeValue timeTaken = bulkResponse.getTook();
    boolean timedOut = bulkResponse.isTimedOut();
    long totalDocs = bulkResponse.getTotal();
    long updatedDocs = bulkResponse.getUpdated();
    long deletedDocs = bulkResponse.getDeleted();
    long batches = bulkResponse.getBatches();
    long noops = bulkResponse.getNoops();
    long versionConflicts = bulkResponse.getVersionConflicts();
    long bulkRetries = bulkResponse.getBulkRetries();
    long searchRetries = bulkResponse.getSearchRetries();
    TimeValue throttledMillis = bulkResponse.getStatus().getThrottled();
    TimeValue throttledUntilMillis = bulkResponse.getStatus().getThrottledUntil();
    List<ScrollableHitSource.SearchFailure> searchFailures = bulkResponse.getSearchFailures();
    List<BulkItemResponse.Failure> bulkFailures = bulkResponse.getBulkFailures();
    System.out.println("查询更新总共花费了:" + timeTaken.getMillis() + " 毫秒,总条数:" + totalDocs + ",更新数:" + updatedDocs);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) TimeValue(org.elasticsearch.common.unit.TimeValue) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 33 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder in project uavstack by uavorg.

the class InvokeChainQueryHandler method parseSearchCondition.

/**
 * parseSearchCondition
 *
 * 如果用空格分开多个关键字,则默认为或的关系
 *
 * 如果某些关键字是以+连接,代表与关系
 *
 * 注意这里只支持或优先的操作
 *
 * 举例: <kwd1>+<kwd2> <kwd3> <kwd4>
 *
 * 则代表的是意思是(kwd1 and kwd2 同时存在)或kwd3存在或kwd4存在
 *
 * @param queryBuilder
 * @param content
 */
private void parseSearchCondition(BoolQueryBuilder queryBuilder, String content, String field) {
    if (content == null) {
        return;
    }
    boolean hasCompandSearch = (content.indexOf("+") > -1) ? true : false;
    if (hasCompandSearch == false) {
        if (content.indexOf("*") == -1) {
            queryBuilder.must(QueryBuilders.matchQuery(field, content));
        } else {
            queryBuilder.must(QueryBuilders.wildcardQuery(field + ".keyword", content));
        }
    } else {
        BoolQueryBuilder orQueryBuilder = QueryBuilders.boolQuery();
        String[] orKwds = content.split(" ");
        for (String orKwd : orKwds) {
            /**
             * 如果没有与关系,就检查是否有*
             */
            if (orKwd.indexOf("+") == -1) {
                if (orKwd.indexOf("*") == -1) {
                    orQueryBuilder.should(QueryBuilders.matchQuery(field, orKwd));
                } else {
                    orQueryBuilder.should(QueryBuilders.wildcardQuery(field + ".keyword", orKwd));
                }
            } else {
                /**
                 * 或关系,检查是否有*
                 */
                String[] andKwds = orKwd.split("\\+");
                BoolQueryBuilder andQueryBuilder = QueryBuilders.boolQuery();
                for (String andKwd : andKwds) {
                    if (andKwd.indexOf("*") == -1) {
                        andQueryBuilder.must(QueryBuilders.matchQuery(field, andKwd));
                    } else {
                        andQueryBuilder.must(QueryBuilders.wildcardQuery(field + ".keyword", andKwd));
                    }
                }
                orQueryBuilder.should(andQueryBuilder);
            }
        }
        queryBuilder.must(orQueryBuilder);
    }
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 34 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder in project uavstack by uavorg.

the class InvokeChainQueryHandler method queryByApp.

/**
 * 从应用维度对调用链进行查询
 *
 * @param data
 */
@SuppressWarnings("rawtypes")
private void queryByApp(UAVHttpMessage data) {
    long startTime = DataConvertHelper.toLong(data.getRequest("stime"), -1);
    long endTime = DataConvertHelper.toLong(data.getRequest("etime"), -1);
    if (startTime == -1 || endTime == -1 || endTime < startTime) {
        data.putResponse("rs", "ERR");
        data.putResponse("msg", "The Time Range Error: startTime=" + startTime + ",endTime=" + endTime);
        return;
    }
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    queryBuilder.must(QueryBuilders.rangeQuery("stime").gte(startTime).lte(endTime));
    String ipport = data.getRequest("ipport");
    if (ipport != null) {
        queryBuilder.must(QueryBuilders.matchQuery("ipport", ipport));
    }
    String appid = data.getRequest("appid");
    if (appid != null) {
        queryBuilder.must(QueryBuilders.matchQuery("appid", appid));
    }
    String appuuid = data.getRequest("appuuid");
    if (appuuid != null) {
        queryBuilder.must(QueryBuilders.termQuery("appuuid", appuuid));
    }
    String appgroup = data.getRequest("appgroup");
    if (appgroup != null) {
        queryBuilder.must(QueryBuilders.matchQuery("appgroup", appgroup));
    }
    String eptype = data.getRequest("eptype");
    if (eptype != null) {
        BoolQueryBuilder typeQuery = QueryBuilders.boolQuery();
        String[] types = eptype.split(",");
        for (String type : types) {
            typeQuery.should(QueryBuilders.matchQuery("eptype", type));
        }
        queryBuilder.must(typeQuery);
    }
    String cls = data.getRequest("class");
    if (cls != null) {
        parseSearchCondition(queryBuilder, cls, "class");
    }
    String method = data.getRequest("method");
    if (method != null) {
        parseSearchCondition(queryBuilder, method, "method");
    }
    String url = data.getRequest("url");
    if (url != null) {
        parseSearchCondition(queryBuilder, url, "url");
    }
    String state = data.getRequest("state");
    if (state != null) {
        parseSearchCondition(queryBuilder, state, "state");
    }
    SortBuilder[] sorts = buildSort(data);
    this.queryToList(data, queryBuilder, null, sorts);
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SortBuilder(org.elasticsearch.search.sort.SortBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 35 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder in project uavstack by uavorg.

the class SlowOperQueryHandler method queryByParams.

/**
 * 自由组合参数查询
 *
 * @param data
 */
@SuppressWarnings("rawtypes")
private void queryByParams(UAVHttpMessage data) {
    String appid = data.getRequest("appid");
    if (appid == null) {
        data.putResponse("rs", "ERR");
        data.putResponse("msg", "appid is required");
        return;
    }
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    queryBuilder.must(QueryBuilders.matchQuery("appid", appid));
    if (data.getRequest("traceid") != null) {
        queryBuilder.must(QueryBuilders.matchQuery("traceid", data.getRequest("traceid")));
    }
    if (data.getRequest("spanid") != null) {
        queryBuilder.must(QueryBuilders.matchQuery("spanid", data.getRequest("spanid")));
    }
    if (data.getRequest("epinfo") != null) {
        queryBuilder.must(QueryBuilders.matchQuery("epinfo", data.getRequest("epinfo")));
    }
    BoolQueryBuilder queryBuilderInner = QueryBuilders.boolQuery();
    if (data.getRequest("rpc_req_head") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("rpc_req_head", data.getRequest("rpc_req_head")));
    }
    if (data.getRequest("rpc_req_body") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("rpc_req_body", data.getRequest("rpc_req_body")));
    }
    if (data.getRequest("rpc_rsp_head") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("rpc_rsp_head", data.getRequest("rpc_rsp_head")));
    }
    if (data.getRequest("rpc_rsp_body") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("rpc_rsp_body", data.getRequest("rpc_rsp_body")));
    }
    if (data.getRequest("rpc_rsp_exception") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("rpc_rsp_exception", data.getRequest("rpc_rsp_exception")));
    }
    if (data.getRequest("sql_req") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("sql_req", data.getRequest("sql_req")));
    }
    if (data.getRequest("sql_ret") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("sql_ret", data.getRequest("sql_ret")));
    }
    if (data.getRequest("method_req") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("method_req", data.getRequest("method_req")));
    }
    if (data.getRequest("method_ret") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("method_ret", data.getRequest("method_ret")));
    }
    if (data.getRequest("mq_head") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("mq_head", data.getRequest("mq_head")));
    }
    if (data.getRequest("mq_body") != null) {
        queryBuilderInner.should(QueryBuilders.matchQuery("mq_body", data.getRequest("mq_body")));
    }
    queryBuilder.must(queryBuilderInner);
    SortBuilder[] sorts = buildSort(data);
    this.queryToList(data, queryBuilder, null, sorts);
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SortBuilder(org.elasticsearch.search.sort.SortBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)201 SearchResponse (org.elasticsearch.action.search.SearchResponse)73 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)58 SearchHit (org.elasticsearch.search.SearchHit)41 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)38 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)30 Map (java.util.Map)29 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 List (java.util.List)23 SearchRequest (org.elasticsearch.action.search.SearchRequest)21 Test (org.junit.Test)18 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)16 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)16 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)14 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)13 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 LinkedList (java.util.LinkedList)11 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)11