Java Code Examples for org.elasticsearch.client.RestHighLevelClient#search()
The following examples show how to use
org.elasticsearch.client.RestHighLevelClient#search() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrefixApiMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ PrefixQueryBuilder matchQueryBuilder = QueryBuilders.prefixQuery("cmd","get_fee");//查询某个字段存在的记录 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.from(0); searchSourceBuilder.size(1); SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index searchRequest.types("log");//限定type searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 2
Source File: PercentileRankAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "weather_hourforcast"); PercentileRanksAggregationBuilder aggregationBuilder = AggregationBuilders.percentileRanks("utm_ranks",new double[]{200,500,1000,3000,8000}).field("utm").keyed(false); SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.aggregation(aggregationBuilder); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 3
Source File: PercentilesAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "weather_hourforcast"); PercentilesAggregationBuilder aggregationBuilder = AggregationBuilders.percentiles("utm").field("utm").keyed(false); // PercentilesAggregationBuilder aggregationBuilder = AggregationBuilders.percentiles("utm").field("utm").percentiles(95,99,99.9).keyed(false);//自定义百分区间 SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.aggregation(aggregationBuilder); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 4
Source File: LogstashIT.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private static List<Map<String, Object>> queryDocuments( final RestHighLevelClient client ) throws IOException { final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() .size(LOG_EVENT_COUNT) .fetchSource(true); final SearchRequest searchRequest = new SearchRequest(MavenHardcodedConstants.ES_INDEX_NAME) .source(searchSourceBuilder); try { final SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); return Arrays .stream(searchResponse.getHits().getHits()) .map(SearchHit::getSourceAsMap) .collect(Collectors.toList()); } catch (ElasticsearchStatusException error) { if (RestStatus.NOT_FOUND.equals(error.status())) { return Collections.emptyList(); } throw new IOException(error); } }
Example 5
Source File: RestHighLevelClientCase.java From skywalking with Apache License 2.0 | 6 votes |
private void search(RestHighLevelClient client, String indexName) throws IOException { SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.termQuery("author", "Marker")); sourceBuilder.from(0); sourceBuilder.size(10); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices(indexName); searchRequest.source(sourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); if (!(searchResponse.getHits().totalHits > 0)) { String message = "elasticsearch search data fail."; logger.error(message); throw new RuntimeException(message); } }
Example 6
Source File: StatsAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "weather_hourforcast"); StatsAggregationBuilder aggregationBuilder = AggregationBuilders.stats("utm_stats").field("utm").missing(0); SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.aggregation(aggregationBuilder); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 7
Source File: DateRangeApiMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void dateRange1() throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ RangeQueryBuilder matchQueryBuilder = QueryBuilders.rangeQuery("ctm") .from("2018-07-01 07:27:59.733",true) .to("2018-07-01 07:30:00.000",false); // matchQueryBuilder.format("yyyy-MM-dd HH:mm:ss.SSS");//设置日期格式 // matchQueryBuilder.timeZone("+08:00");//设置时区 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.from(0); searchSourceBuilder.size(5); SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index searchRequest.types("log");//限定type searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 8
Source File: ChildrenAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ ChildrenAggregationBuilder aggregationBuilder = new ChildrenAggregationBuilder("utm","histogram"); aggregationBuilder.field("utm"); SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.aggregation(aggregationBuilder); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); Aggregations aggregations = searchResponse.getAggregations(); List<Aggregation> aggregationList = aggregations.asList(); for(Aggregation each: aggregationList){ System.out.println(each); } }finally{ HighLevelClient.close(); } }
Example 9
Source File: LogstashIT.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private static long queryDocumentCount( final RestHighLevelClient client) throws IOException { final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() .size(0) .fetchSource(false); final SearchRequest searchRequest = new SearchRequest(MavenHardcodedConstants.ES_INDEX_NAME) .source(searchSourceBuilder); try { final SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); return searchResponse.getHits().getTotalHits().value; } catch (ElasticsearchStatusException error) { if (RestStatus.NOT_FOUND.equals(error.status())) { return 0L; } throw new IOException(error); } }
Example 10
Source File: ElasticSearchUtil.java From ranger with Apache License 2.0 | 6 votes |
public SearchResponse searchResources(SearchCriteria searchCriteria, List<SearchField> searchFields, List<SortField> sortFields, RestHighLevelClient client, String index) throws IOException { // See Also: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-query-builders.html QueryAccumulator queryAccumulator = new QueryAccumulator(searchCriteria); if (searchCriteria.getParamList() != null) { searchFields.stream().forEach(queryAccumulator::addQuery); // For now assuming there is only date field where range query will // be done. If we there are more than one, then we should create a // hashmap for each field name if (queryAccumulator.fromDate != null || queryAccumulator.toDate != null) { queryAccumulator.queries.add(setDateRange(queryAccumulator.dateFieldName, queryAccumulator.fromDate, queryAccumulator.toDate)); } } BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); queryAccumulator.queries.stream().filter(x -> x != null).forEach(boolQueryBuilder::must); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); setSortClause(searchCriteria, sortFields, searchSourceBuilder); searchSourceBuilder.from(searchCriteria.getStartIndex()); searchSourceBuilder.size(searchCriteria.getMaxRows()); searchSourceBuilder.fetchSource(true); SearchRequest query = new SearchRequest(); query.indices(index); query.source(searchSourceBuilder.query(boolQueryBuilder)); return client.search(query, RequestOptions.DEFAULT); }
Example 11
Source File: MultiMatchQueryApidMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ QueryBuilder matchQueryBuilder = QueryBuilders.multiMatchQuery("token","retdesc", "reqpkg");//查询有问题?? SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.from(0); searchSourceBuilder.size(5); SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index searchRequest.types("log");//限定type searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 12
Source File: MatchQueryApiMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("status","success"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.from(0); searchSourceBuilder.size(5); SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index searchRequest.types("log");//限定type searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 13
Source File: MatchParseApiMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ QueryBuilder matchQueryBuilder = QueryBuilders.matchPhraseQuery("content","校车"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.from(0); searchSourceBuilder.size(5); SearchRequest searchRequest = new SearchRequest("jingma2");//限定index searchRequest.types("fulltext");//限定type searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 14
Source File: BoolApiMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ BoolQueryBuilder matchQueryBuilder = QueryBuilders.boolQuery(); matchQueryBuilder.must(QueryBuilders.termQuery("cpname","llbxinterface")); matchQueryBuilder.mustNot(QueryBuilders.termQuery("status","fail")); matchQueryBuilder.should(QueryBuilders.rangeQuery("utm").from(0,true).to(100,true)); matchQueryBuilder.minimumShouldMatch(1); // matchQueryBuilder.filter(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.from(0); searchSourceBuilder.size(3); SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index searchRequest.types("log");//限定type searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 15
Source File: DateHistogramAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ DateHistogramAggregationBuilder dateHistogramAggregationBuilder = AggregationBuilders.dateHistogram("ctm_date_histogram"); dateHistogramAggregationBuilder.field("ctm");//设置直方图针对的字段 dateHistogramAggregationBuilder.dateHistogramInterval(DateHistogramInterval.hours(6));//直方图每个分组对应的范围 dateHistogramAggregationBuilder.timeZone(DateTimeZone.forOffsetHours(8));//时区偏移 dateHistogramAggregationBuilder.keyed(true);//是否需要key名 dateHistogramAggregationBuilder.format("yyyy-MM-dd HH:mm");//key名格式 // dateHistogramAggregationBuilder.order(BucketOrder.aggregation("_key",true));//分组key的排序 // dateHistogramAggregationBuilder.minDocCount(0);//对于每个分组最少具有多少条数据,少于这个设置,则该分组不显示 // dateHistogramAggregationBuilder.extendedBounds(0,8000);//设置分组区间的下线和上线,只有当min_doc_count为0时有效 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("cmd","weather_info"); SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(termQueryBuilder); searchSourceBuilder.aggregation(dateHistogramAggregationBuilder); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 16
Source File: AvgAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ AvgAggregationBuilder aggregationBuilder = AggregationBuilders.avg("utm").field("utm").missing(0); SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.aggregation(aggregationBuilder); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); //统计结果 Aggregations aggregations = searchResponse.getAggregations(); Map<String, Aggregation> aggregationMap = aggregations.asMap(); for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){ System.out.println(((ParsedAvg)(each.getValue())).getValue()); } }finally{ HighLevelClient.close(); } }
Example 17
Source File: CountAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "get_fee_transfer_content_data"); ValueCountAggregationBuilder aggregationBuilder = AggregationBuilders.count("cmd_count").field("cmd"); SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.aggregation(aggregationBuilder); searchSourceBuilder.size(0);//不返回具体业务数据,只需要聚合结果 searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); //统计结果 Aggregations aggregations = searchResponse.getAggregations(); Map<String, Aggregation> aggregationMap = aggregations.asMap(); for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){ System.out.println(((ParsedValueCount)(each.getValue())).getValue()); } }finally{ HighLevelClient.close(); } }
Example 18
Source File: MaxMinAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ // MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("utm").field("utm"); MinAggregationBuilder aggregationBuilder = AggregationBuilders.min("utm").field("utm"); SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.aggregation(aggregationBuilder); searchSourceBuilder.size(0);//设置不需要文档数据,只需要返回聚合结果 searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); //统计结果 Aggregations aggregations = searchResponse.getAggregations(); Map<String, Aggregation> aggregationMap = aggregations.asMap(); for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){ // System.out.println(((ParsedMax)(each.getValue())).getValue()); System.out.println(((ParsedMin)(each.getValue())).getValue()); } }finally{ HighLevelClient.close(); } }
Example 19
Source File: KibanaExporter.java From arctic-sea with Apache License 2.0 | 4 votes |
public static void main(String args[]) throws Exception { if (args.length != 2) { System.out.printf("Usage: java KibanaExporter.jar %s %s%n", "localhost:9300", "my-cluster-name"); System.exit(0); } if (!args[0].contains(":")) { throw new IllegalArgumentException( String.format("%s not a valid format. Expected <hostname>:<port>.", args[0])); } // set ES address String split[] = args[0].split(":"); // set cluster name Builder tcSettings = Settings.builder(); tcSettings.put("cluster.name", args[1]); System.out.println("Connection to " + args[1]); client = new RestHighLevelClient(RestClient .builder(new HttpHost(InetAddress.getByName(split[0]), Integer.parseInt(split[1], 10), "http"))); // search index pattern for needle searchIndexPattern(); KibanaConfigHolderDto holder = new KibanaConfigHolderDto(); System.out.println("Reading .kibana index"); SearchResponse resp = client.search(new SearchRequest(".kibana").source(new SearchSourceBuilder().size(1000)), RequestOptions.DEFAULT); Arrays.asList(resp.getHits().getHits()).stream().map(KibanaExporter::parseSearchHit).forEach(holder::add); System.out.println("Reading finished"); ObjectMapper mapper = new ObjectMapper(); // we love pretty things mapper.enable(SerializationFeature.INDENT_OUTPUT); File f = new File("kibana_config.json"); try (FileOutputStream out = new FileOutputStream(f, false)) { mapper.writeValue(out, holder); } System.out.println("File outputted to: " + f.getAbsolutePath()); client.close(); }
Example 20
Source File: SearchApiMain.java From elasticsearch-pool with Apache License 2.0 | 4 votes |
public static void searchApi() throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try { SearchRequest searchRequest = new SearchRequest("jingma2_test");//限定index searchRequest.types("testlog");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); /*查询所有记录*/ // searchSourceBuilder.query(QueryBuilders.matchAllQuery()); /*根据匹配查询*/ QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "风雷"); /*设置中文分词器*/ // ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik"); // ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_max_word"); // ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_smart"); // ((MatchQueryBuilder) matchQueryBuilder).analyzer("standard"); searchSourceBuilder.query(matchQueryBuilder); /*限定查询条件和查询条数*/ // searchSourceBuilder.query(QueryBuilders.termQuery("name", "风雷")); searchSourceBuilder.from(0); searchSourceBuilder.size(5); // searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); /*限定查询结果排序*/ // searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); // searchSourceBuilder.sort(new FieldSortBuilder("age").order(SortOrder.ASC)); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); SearchHits hits = searchResponse.getHits(); long totalHits = hits.getTotalHits(); float maxScore = hits.getMaxScore(); SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) { String index = hit.getIndex(); String type = hit.getType(); String id = hit.getId(); float score = hit.getScore(); String sourceAsString = hit.getSourceAsString(); System.out.println(sourceAsString); // Map<String, Object> sourceAsMap = hit.getSourceAsMap(); } }finally { HighLevelClient.close(); } }