Elasticsearch——terms聚合实现搜索热词统计

发布时间 2023-10-16 11:07:51作者: 丿屿

   最近项目中遇到一个需求。需要实现热词功能,需要给用户展示检索频率最高的10个关键字;由于项目中使用到了es,所以就使用es实现,具体实现如下:

前提,拥有es环境;

1、创建索引:

POST  http://localhost:9200/hotwords_test/_mapping
 
 
{
  "properties": {
    "search_txt": {
      "type": "keyword"
    },
    "user_name":{
        "type": "text",
        "analyzer": "keyword"
    },
    "happend_time":{
        "type": "date",
        "format": "yyy-MM-dd HH:mm:ss"
    }
  }
}

2、项目中配置连接es

<dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.48</version>
        </dependency>
    </dependencies>

3、插入测试数据

public class ElasticsearchTesl {
    public static final String host = "localhost";
    public static final Integer port = 9200;
    public static final String index = "hotwords_test";
 
    public static void main(String[] args) throws IOException{
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost(host, port, "http")));
 
        JSONObject data = new JSONObject();
        data.put("search_txt", "大枣");
        data.put("user_name", "test");
        data.put("happend_time", "2021-10-17 15:11:30");
        String docId = indexDoc(client, index, data);
        System.out.println(docId);
        client.close();
 
    }
 
    public static String indexDoc(RestHighLevelClient client, String index, JSONObject data){
        IndexRequest request = new IndexRequest(index);
        request.source(data);
        try {
            IndexResponse response = client.index(request, RequestOptions.DEFAULT);
            return response.getId();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

4、最后实现聚合查询,部分代码如下:

AggregationBuilder aggregationBuilder = AggregationBuilders
                .terms("value_count").field("search_txt").size(5);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.aggregation(aggregationBuilder);
        sourceBuilder.query(QueryBuilders.termQuery("user_name", "test"));
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Aggregations aggregations = searchResponse.getAggregations();
        for(Aggregation a:aggregations){
            Terms terms = (Terms) a;
            for(Terms.Bucket bucket:terms.getBuckets()){
                System.out.println(bucket.getKeyAsString() +":" + bucket.getDocCount());
            }

}

5、输出如下:

甘蔗:4
芒果:4
榴莲:3
大枣:2
桃子:2

以上便完全实现了热词检索功能;

转载:

————————————————
原文链接:https://blog.csdn.net/qq_28757391/article/details/120836023