elasticsearch-java应用-对数据进行聚合分析

本文档介绍了在Elasticsearch中进行数据聚合分析时遇到的问题,包括按照国家和入职年限分组并计算平均薪资。在使用过程中,由于版本差异(从5.2.2升级到6.0.1),导致了API变化和`Fielddata is disabled on text fields by default`异常。解决方案包括理解不同版本API的区别,以及针对text字段设置fielddata为true以支持聚合操作。总结强调了在不同版本间进行开发需要注意版本特性并适时调整代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【问题描述】:

需求:对员工信息进行聚合分析

1.先对员工按照国家country分组,然后再按入职年限join_date进行分组,分组后在计算组内平均薪资

员工数据如下:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "company",
        "_type": "employee",
        "_id": "5",
        "_score": 1,
        "_source": {
          "name": "mike",
          "age": 37,
          "position": "finance manager",
          "country": "usa",
          "join_date": "2015-01-01",
          "salary": 15000
        }
      },
      {
        "_index": "company",
        "_type": "employee",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "marry",
          "age": 35,
          "position": "technique manager",
          "country": "china",
          "join_date": "2017-01-01",
          "salary": 12000
        }
      },
      {
        "_index": "company",
        "_type": "employee",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "jen",
          "age": 25,
          "position": "junior finance",
          "country": "usa",
          "join_date": "2016-01-01",
          "salary": 7000
        }
      },
      {
        "_index": "company",
        "_type": "employee",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "jack",
          "age": 27,
          "position": "technique",
          "country": "china",
          "join_date": "2017-01-01",
          "salary": 10000
        }
      },
      {
        "_index": "company",
        "_type": "employee",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "tom",
          "age": 32,
          "position": "senior technique software",
          "country": "china",
          "join_date": "2016-01-01",
          "salary": 11000
        }
      }
    ]
  }
}
java 学习视频中使用的是5.2版本,代码如下


《图1》

no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
Exception in thread "main" Failed to execute phase [query], all shards failed; shardFailures {[yWbGC_XwQsyJXKZD3kIDgQ][company][0]: RemoteTransportException[[yWbGC_X][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.]; }{[yWbGC_XwQsyJXKZD3kIDgQ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值