首页 > 其他分享 >Elasticsearch——terms聚合实现搜索热词统计

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

时间:2023-10-16 11:12:25浏览次数:24  
标签:index terms String 热词 client Elasticsearch new data public

   最近项目中遇到一个需求。需要实现热词功能,需要给用户展示检索频率最高的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

标签:index,terms,String,热词,client,Elasticsearch,new,data,public
From: https://www.cnblogs.com/huangrenhao/p/17766906.html

相关文章

  • elasticsearch安装dynamic-synonym插件
    今天就来和大家讲讲如何在es中安装dynamic-synonym插件,首先我们需要去github上下载与es版本对应的插件,一般github上基本都是本地词库和远程文本词库的,在gitee上可以找到采用数据库作为词库的源码,大致思路就是修改一些参数配置,然后自己创建一个表作为同义词词库,最后将打包好的jar包......
  • elasticsearch通过Java class类的@Setting和@Mapping来定义索引index
    今天就来和大家讲讲如何将es索引中的mapping和setting在索引index和class联系起来,其实在这个问题也困扰我好久了,一直没有解决,在elasticsearch7.x版本的时候貌似好像可以用request在程序中来建立索引,像Stringindex=“{“mapping”:...}”之类的操作,干起来比较复杂,在elasticsear......
  • elasticsearch
    lucenesolressolr和es都是基于lucene官网: https://www.elastic.co/cn/elasticsearch/ 倒排序索引lucene是类库solr基于lucene ......
  • Debian12安装elasticsearch实践及问题解决方案
    一、安装安装其实很简单,直接上官网链接:下载地址,官网提供了所有安装方式,总一款适合你。我的目标系统是Debian12,包管理是apt-get,所以就以这个为示例,仅供参考。1、先选择需要安装的版本2、导入ElasticsearchPGP密钥wget-qO-https://artifacts.elastic.co/GPG-KEY-elastic......
  • elasticsearch_exporter监控elasticsearch
    1、官网下载elasticsearch_exporter的安装包,地址如下:```bashhttps://github.com/prometheus-community/elasticsearch_exporter```2、配置成服务```bashvim/etc/systemd/system/elasticsearch_exporter.service###写入如下内容[Unit]Description=elasticsearch_exporterAfte......
  • ElasticSearch集群搭建
    2.1ES集群的好处es天然支持集群模式,其好处主要有两个:1.能够增大系统的容量,如内存、磁盘,使得es集群可以支持PB级的数据;2.能够提高系统可用性,即使部分节点停止服务,整个集群依然可以正常服务;2.2ES如何组集群单节点ES,如下图所示;如果单节点出现问题,服务就不可用了,如何新增一个es......
  • skywalking elasticsearch 版本匹配问题
     进入skywalking历史下载页面:https://archive.apache.org/dist/skywalking/8.6.0/  开始正常一整个流程应该有skywalking-oap-server、skywalking-oap-ui、es、skywalking-agent.jar;中间最大的问题是版本,然后是配置;个人建议直接用官方给的文档里面的docker镜像作为部署......
  • ElasticSearch集群处于yellow状态处理
    一般思路:1、查看集群状态curl-XGET-u{username}:{password}-s$prefix/_cat/health?v2、查看节点状态curl-XGET-u{username}:{password}-s$prefix/_cat/nodes?v3、查看索引状态curl-XGET-u{username}:{password}-s$prefix/_cat/indices?v4、查看原......
  • 单机版 ElasticSearch 和 Kibana 快速搭建
    ElasticSearch是一款底层是基于lucene实现,功能强大的搜索引擎中间件,也可以认为ElasticSearch是一款NoSql数据库。每一种NoSql数据库的诞生,都是为了解决传输关系型数据库无法解决的问题,ElasticSearch能够从海量数据中快速找到所需要的内容,专注于搜索、分析和计算。Kibana......
  • 安装 Elasticsearch
    安装Elasticsearch​#中间件Elasticsearch#​一、Docker容器https://hub.docker.com/_/elasticsearch创建所需目录和文件配置:./config​​​elasticsearch.ymlcluster.name:"docker-cluster"http.host:0.0.0.0-----------------------BEGINSECURITYAUTOCONFIG......