use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project dq-easy-cloud by dq-open-cloud.
the class TransportClient method page.
/**
* 分页
*
* @throws IOException
*/
@Test
public void page() throws IOException {
int pageNumber = 1;
int pageSize = 10;
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.queryStringQuery("JAVA"));
// 设置起始页
searchSourceBuilder.from((pageNumber - 1) * pageSize);
// 设置页大小
searchSourceBuilder.size(pageSize);
Search search = // 索引名称
new Search.Builder(searchSourceBuilder.toString()).addIndex(indexName).build();
SearchResult result = jestClient.execute(search);
// 自动解析
JsonObject jsonObject = result.getJsonObject();
JsonObject hitsobject = jsonObject.getAsJsonObject("hits");
long took = jsonObject.get("took").getAsLong();
long total = hitsobject.get("total").getAsLong();
System.out.println("took:" + took + " " + "total:" + total);
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project dq-easy-cloud by dq-open-cloud.
the class TransportClient method wildcardQuery.
/**
* 通配符和正则表达式查询
*
* @throws Exception
*/
@Test
public void wildcardQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 通配符和正则表达式查询
QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("title", "*:*");
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
Search search = new Search.Builder(query).addIndex(indexName).addType(typeName).build();
SearchResult result = jestClient.execute(search);
List<Hit<Object, Void>> hits = result.getHits(Object.class);
System.out.println("Size:" + hits.size());
for (Hit<Object, Void> hit : hits) {
Object news = hit.source;
System.out.println(news.toString());
}
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder 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);
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project pancm_project by xuwujing.
the class EsAggregationSearchTest2 method search.
private static SearchResponse search(AggregationBuilder aggregation) throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("mt_task_j20200401");
searchRequest.types("mt_task_hh");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 不需要解释
searchSourceBuilder.explain(false);
// 不需要原始数据
searchSourceBuilder.fetchSource(false);
// 不需要版本号
searchSourceBuilder.version(false);
searchSourceBuilder.aggregation(aggregation);
logger.info("查询的语句:" + searchSourceBuilder.toString());
searchRequest.source(searchSourceBuilder);
// 同步查询
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse;
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project pancm_project by xuwujing.
the class EsHighLevelRestSearchTest method countSearch.
private static void countSearch() throws IOException {
String type = "_doc";
String index = "test1";
// 查询指定的索引库
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(type);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 设置查询条件
sourceBuilder.query(QueryBuilders.termQuery("uid", "1234"));
// 包含或排除字段
// sourceBuilder.fetchSource(includeFields, excludeFields);
sourceBuilder.fetchSource(false);
searchRequest.source(sourceBuilder);
System.out.println("总数查询的DSL语句:" + sourceBuilder.toString());
// 同步查询
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总数查询的结果:" + searchResponse.getHits().getTotalHits());
System.out.println("\n=================\n");
}
Aggregations