Search in sources :

Example 41 with SearchRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest 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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 42 with SearchRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest 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");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 43 with SearchRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project pancm_project by xuwujing.

the class EsHighLevelRestSearchTest method prefixSearch.

/**
 * @return void
 * @Author pancm
 * @Description 前缀查询
 * @Date 2019/9/30
 * @Param []
 */
private static void prefixSearch() throws IOException {
    String type = "_doc";
    String index = "p_test2";
    // 查询指定的索引库
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.types(type);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.prefixQuery("uid", "1"));
    searchRequest.source(sourceBuilder);
    System.out.println("前缀查询的DSL语句:" + sourceBuilder.toString());
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    // 结果
    searchResponse.getHits().forEach(hit -> {
        Map<String, Object> map = hit.getSourceAsMap();
        System.out.println("前缀查询的Map结果:" + map);
    });
    System.out.println("\n=================\n");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 44 with SearchRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project pancm_project by xuwujing.

the class EsHighLevelRestSearchTest method existSearch.

/**
 * @return void
 * @Author pancm
 * @Description exist查询
 * @Date 2019/9/17
 * @Param []
 */
private static void existSearch() throws IOException {
    String type = "_doc";
    String index = "test1";
    // 查询指定的索引库
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.types(type);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // 设置查询条件
    sourceBuilder.query(QueryBuilders.existsQuery("msgcode"));
    searchRequest.source(sourceBuilder);
    System.out.println("存在查询的DSL语句:" + sourceBuilder.toString());
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    // 结果
    searchResponse.getHits().forEach(hit -> {
        Map<String, Object> map = hit.getSourceAsMap();
        String string = hit.getSourceAsString();
        System.out.println("存在查询的Map结果:" + map);
        System.out.println("存在查询的String结果:" + string);
    });
    System.out.println("\n=================\n");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 45 with SearchRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project pancm_project by xuwujing.

the class EsHighLevelRestSearchTest method genSearch.

/**
 * @return void
 * @Author pancm
 * @Description 普通查询
 * @Date 2019/9/12
 * @Param []
 */
private static void genSearch() 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.from(0);
    sourceBuilder.size(5);
    sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
    // 设置路由
    // searchRequest.routing("routing");
    // 设置索引库表达式
    searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
    // 查询选择本地分片,默认是集群分片
    searchRequest.preference("_local");
    // 排序
    // 根据默认值进行降序排序
    // sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
    // 根据字段进行升序排序
    // sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC));
    // 关闭suorce查询
    // sourceBuilder.fetchSource(false);
    String[] includeFields = new String[] { "title", "user", "innerObject.*" };
    String[] excludeFields = new String[] { "_type" };
    // 包含或排除字段
    // sourceBuilder.fetchSource(includeFields, excludeFields);
    searchRequest.source(sourceBuilder);
    System.out.println("普通查询的DSL语句:" + sourceBuilder.toString());
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    // HTTP状态代码、执行时间或请求是否提前终止或超时
    RestStatus status = searchResponse.status();
    TimeValue took = searchResponse.getTook();
    Boolean terminatedEarly = searchResponse.isTerminatedEarly();
    boolean timedOut = searchResponse.isTimedOut();
    // 供关于受搜索影响的切分总数的统计信息,以及成功和失败的切分
    int totalShards = searchResponse.getTotalShards();
    int successfulShards = searchResponse.getSuccessfulShards();
    int failedShards = searchResponse.getFailedShards();
    // 失败的原因
    for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
    // failures should be handled here
    }
    // 结果
    searchResponse.getHits().forEach(hit -> {
        Map<String, Object> map = hit.getSourceAsMap();
        System.out.println("普通查询的结果:" + map);
    });
    System.out.println("\n=================\n");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) RestStatus(org.elasticsearch.rest.RestStatus) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) TimeValue(org.elasticsearch.common.unit.TimeValue)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)156 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)81 SearchResponse (org.elasticsearch.action.search.SearchResponse)69 Test (org.junit.Test)37 IOException (java.io.IOException)31 SearchHit (org.elasticsearch.search.SearchHit)25 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)22 ArrayList (java.util.ArrayList)21 SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)21 HashMap (java.util.HashMap)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 List (java.util.List)16 SearchHits (org.elasticsearch.search.SearchHits)15 Pipeline (com.hazelcast.jet.pipeline.Pipeline)14 Map (java.util.Map)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 SearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)11 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)11 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)10 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)10