elasticsearch实现热门词汇展示

搜索页面经常会有展示热门词汇的需求。把用户经常搜索的词汇按出现的次数展示出来。这其实就是一个经典的wordcount,是数据分析方面典型的入门案例。相对于hadoop和strorm。用es可以很方便的实现。

            //获取搜索日志
            SearchRequestBuilder requestBuilder = esClient.prepareSearch("index").setTypes("log").setQuery(QueryBuilders.matchAllQuery());
            //聚合分析查询出现次数最多的10个词汇
            SearchResponse actionGet = requestBuilder.addAggregation(AggregationBuilders.terms("hotWord").field("keywords").size(10)).execute().actionGet();
            //获取分析后的数据
            Aggregations aggregations = actionGet.getAggregations();
            Terms trem = aggregations.get("hotWord");
            List buckets = trem.getBuckets();
            List hotWords = new ArrayList<>();
            for (Bucket bucket : buckets) {
                String key = (String) bucket.getKey();
                hotWords.add(key);
            }

你可能感兴趣的:(elasticsearch)