最近项目中遇到一个需求。需要实现热词功能,需要给用户展示检索频率最高的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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端