Search in sources :

Example 46 with SearchRequest

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

the class EsHighLevelRestSearchTest method search.

/**
 * search查询使用示例
 *
 * @throws IOException
 */
private static void search() throws IOException {
    /*
         * 全文查询使用示例
         */
    // 查询指定的索引库
    SearchRequest searchRequest = new SearchRequest("user");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // 搜索字段user为pancm的数据
    MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "pancm");
    // 设置模糊查询
    matchQueryBuilder.fuzziness(Fuzziness.AUTO);
    // 设置前缀长度
    matchQueryBuilder.prefixLength(3);
    // 设置最大扩展选项来控制查询的模糊过程
    matchQueryBuilder.maxExpansions(10);
    /*
         * QueryBuilder也可以
         */
    // QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
    // .fuzziness(Fuzziness.AUTO)
    // .prefixLength(3)
    // .maxExpansions(10);
    SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
    searchSourceBuilder2.query(matchQueryBuilder);
    SearchRequest searchRequest2 = new SearchRequest();
    searchRequest2.source(searchSourceBuilder2);
    // 同步查询
    SearchResponse searchResponse2 = client.search(searchRequest, RequestOptions.DEFAULT);
    SearchHits hits = searchResponse2.getHits();
    // 总条数和分值
    long totalHits = hits.getTotalHits();
    float maxScore = hits.getMaxScore();
    hits.forEach(hit -> {
        String index = hit.getIndex();
        String type = hit.getType();
        String id = hit.getId();
        float score = hit.getScore();
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        String string = hit.getSourceAsString();
        System.out.println("Match查询的Map结果:" + sourceAsMap);
        System.out.println("Match查询的String结果:" + string);
        String documentTitle = (String) sourceAsMap.get("title");
        // List<Object> users = (List<Object>) sourceAsMap.get("user");
        Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject");
    });
    System.out.println("\n=================\n");
    /*
         * 高亮查询
         */
    SearchSourceBuilder searchSourceBuilder3 = new SearchSourceBuilder();
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title");
    // 设置字段高亮字体
    highlightTitle.highlighterType("user");
    highlightBuilder.field(highlightTitle);
    HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
    highlightBuilder.field(highlightUser);
    searchSourceBuilder3.highlighter(highlightBuilder);
    SearchRequest searchRequest3 = new SearchRequest();
    searchRequest3.source(searchSourceBuilder3);
    // 同步查询
    SearchResponse searchResponse3 = client.search(searchRequest3, RequestOptions.DEFAULT);
    searchResponse3.getHits().forEach(hit -> {
        Map<String, Object> map = hit.getSourceAsMap();
        String string = hit.getSourceAsString();
        System.out.println("Highlight查询的Map结果:" + map);
        System.out.println("Highlight查询的String结果:" + string);
    });
    System.out.println("\n=================\n");
    /**
     * 聚合查询
     */
    SearchSourceBuilder searchSourceBuilder4 = new SearchSourceBuilder();
    // terms 就是分组统计 根据user进行分组并创建一个新的聚合user_
    TermsAggregationBuilder aggregation = AggregationBuilders.terms("user_").field("user");
    aggregation.subAggregation(AggregationBuilders.avg("average_age").field("age"));
    searchSourceBuilder4.aggregation(aggregation);
    SearchRequest searchRequest4 = new SearchRequest();
    searchRequest4.source(searchSourceBuilder4);
    // 同步查询
    SearchResponse searchResponse4 = client.search(searchRequest4, RequestOptions.DEFAULT);
    // 聚合查询返回条件
    Aggregations aggregations = searchResponse4.getAggregations();
    System.out.println("聚合查询");
    for (Aggregation agg : aggregations) {
        String type = agg.getType();
        String name = agg.getName();
        Terms terms = (Terms) aggregations.get(name);
        for (Terms.Bucket bucket : terms.getBuckets()) {
            System.out.println("条数:" + bucket.getDocCount());
            System.out.println("key:" + bucket.getKey());
            System.out.println("num:" + bucket.getKeyAsNumber());
            Avg avg = bucket.getAggregations().get("average_age");
            System.out.println("value:" + avg.getValue());
        }
        if (type.equals(TermsAggregationBuilder.NAME)) {
            Bucket elasticBucket = ((Terms) agg).getBucketByKey("average_age");
            long numberOfDocs = elasticBucket.getDocCount();
            System.out.println("条数:" + numberOfDocs);
        }
    }
    /*
         * 建议查询
         */
    SearchSourceBuilder searchSourceBuilder5 = new SearchSourceBuilder();
    SuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("pancm");
    SuggestBuilder suggestBuilder = new SuggestBuilder();
    suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
    searchSourceBuilder5.suggest(suggestBuilder);
    SearchRequest searchRequest5 = new SearchRequest();
    searchRequest5.source(searchSourceBuilder5);
    // 同步查询
    SearchResponse searchResponse5 = client.search(searchRequest5, RequestOptions.DEFAULT);
    Suggest suggest = searchResponse5.getSuggest();
    TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
    for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
        for (TermSuggestion.Entry.Option option : entry) {
            String suggestText = option.getText().string();
            System.out.println("返回结果:" + suggestText);
        }
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Aggregations(org.elasticsearch.search.aggregations.Aggregations) Suggest(org.elasticsearch.search.suggest.Suggest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) SearchHits(org.elasticsearch.search.SearchHits) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Map(java.util.Map) SuggestionBuilder(org.elasticsearch.search.suggest.SuggestionBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 47 with SearchRequest

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

the class EsHighLevelRestSearchTest method allSearch.

private static void allSearch() throws IOException {
    /*
         * 查询集群所有的索引
         *
         */
    SearchRequest searchRequestAll = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    searchRequestAll.source(searchSourceBuilder);
    // 同步查询
    SearchResponse searchResponseAll = client.search(searchRequestAll, RequestOptions.DEFAULT);
    System.out.println("--" + searchResponseAll.getClusters().toString());
    System.out.println("--" + searchResponseAll.status());
    searchResponseAll.getProfileResults().forEach((k, v) -> {
        System.out.println("==" + k);
        System.out.println("===" + v);
    });
    System.out.println("所有查询总数:" + searchResponseAll.getHits().getTotalHits());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 48 with SearchRequest

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

the class EsHighLevelRestSearchTest method rangeSearch.

/**
 * @return void
 * @Author pancm
 * @Description 范围查询
 * @Date 2019/9/30
 * @Param []
 */
private static void rangeSearch() throws IOException {
    String type = "_doc";
    String index = "test1";
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.types(type);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // 设置查询条件
    sourceBuilder.query(QueryBuilders.rangeQuery("sendtime").gte("2019-01-01 00:00:00").lte("2019-12-31 23:59:59"));
    searchRequest.source(sourceBuilder);
    System.out.println("范围查询的DSL语句:" + sourceBuilder.toString());
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    // 结果
    searchResponse.getHits().forEach(hit -> {
        String string = hit.getSourceAsString();
        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 49 with SearchRequest

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

the class EsAggregationSearchTest method search.

private static SearchResponse search(AggregationBuilder aggregation) throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("student");
    searchRequest.types("_doc");
    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 50 with SearchRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project nuxeo-drive-server by nuxeo.

the class ESAuditChangeFinder method getUpperBound.

@Override
public long getUpperBound() {
    SearchRequest request = new SearchRequest(getESIndexName()).types(ElasticSearchConstants.ENTRY_TYPE).searchType(SearchType.DFS_QUERY_THEN_FETCH);
    // TODO refactor this to use max clause
    request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).sort("id", SortOrder.DESC).size(1));
    logSearchRequest(request);
    SearchResponse searchResponse = getClient().search(request);
    logSearchResponse(searchResponse);
    List<LogEntry> entries = new ArrayList<>();
    SearchHits hits = searchResponse.getHits();
    ObjectMapper mapper = new ObjectMapper();
    for (SearchHit hit : hits) {
        try {
            entries.add(mapper.readValue(hit.getSourceAsString(), LogEntryImpl.class));
        } catch (IOException e) {
            log.error("Error while reading Audit Entry from ES", e);
        }
    }
    return entries.size() > 0 ? entries.get(0).getId() : -1;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) LogEntryImpl(org.nuxeo.ecm.platform.audit.impl.LogEntryImpl) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) IOException(java.io.IOException) LogEntry(org.nuxeo.ecm.platform.audit.api.LogEntry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

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