最新Java Elasticsearch 7.10教程(六)-词频统计

一、高亮查询方案

  • 1、直接基于http发送请求
  • 2、基于springboot Repositories
  • 3、基于原生Rest Client(HighRestClient)

 

二、具体实现

1、直接基于http发送请求

 

#词频统计
DELETE message_index
#创建索引数据结构
PUT message_index
{
   "mappings": {
       "properties":{
            "message": {
               "analyzer": "ik_smart",
                "type": "text",
                "fielddata":"true"
            }
        }
    }
}

#增加doc1
PUT /message_index/_doc/1
{
   "message":"沉溺于「轻易获得高成就感」的事情:有意无意地寻求用很小付出获得很大「回报」的偏方,哪怕回报是虚拟的"
 }
#增加doc2
PUT /message_index/_doc/2
{
    "message":"过度追求“短期回报”可以先思考这样一个问题:为什么玩王者荣耀沉溺我们总是停不下来回报"

 }
 
#增加doc3
PUT /message_index/_doc/3
{
   "message":"过度追求的努力无法带来超额的回报,就因此放弃了努力。这点在聪明人身上尤其明显。以前念本科的时候身在沉溺"
 }
 
#aggs为Aggregations(聚合)缩写
#size 10 为前10的统计结果
#默认热点降序出结果
POST /message_index/_search
{
   "size" : 0,  
    "aggs" : {   
        "messages" : {   
            "terms" : {   
               "size" : 10,
              "field" : "message"
            }  
        }
    }
}

当方法使用的是fielddata的方式,另外就是也有使用keyword的方式。keyword与fielddata类似。他们都有一些弊端,如fielddata会占用堆空间,keyword不支持分词(适合放一些类似邮件账号这种数据)。在官网还有另一种词频统计的方法,就是termvectors词条向量的方式,具体见以下链接。

Term vectors API | Elasticsearch Reference [7.9] | Elastic​www.elastic.co

 

 

2、基于springboot Repositories

暂时没有找到Repositories词频统计的代码,如果有这块代码的大神欢迎留言

 

3、基于原生Rest Client(HighRestClient)

      /**
     * 词频统计aggregate
     */
    @Test
    public void aggregate() throws IOException {
        SearchRequest searchRequest = new SearchRequest("person-index");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        TermsAggregationBuilder aggregation = AggregationBuilders.terms("person-aggregate")
                .field("firstname");
        searchSourceBuilder.aggregation(aggregation);
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        Aggregations aggregations = searchResponse.getAggregations();
        Terms byCompanyAggregation = aggregations.get("person-aggregate");
        List<? extends Terms.Bucket> elasticBucket = byCompanyAggregation.getBuckets();
        elasticBucket.forEach(el -> {
            log.info("key:" + el.getKeyAsString());
            log.info("doc_count:" + el.getDocCount());

        });

    }

 

源代码仓库:

https://gitee.com/hankzhousandyos/springboot-elasticsearch​gitee.com

 

最新Java Elasticsearch 7.10教程(汇总)

https://blog.csdn.net/citywu123/article/details/110240244

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

栈江湖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值