elasticsearch参数copy_to,elasticsearch
非嵌套同级聚合多个字段
在使用elasticsearch时遇见了这样的需求:es聚合指定字段时聚合的结果里面只显示聚合的字段。但是在做报表时,我们发现一个问题:如果我们对员工进行聚合,但是我们还希望查看当前员工所在的班组,部门等信息。这时如果查询es两次,对于效率来说是不好的。所以我们在这里使用一个es的字段特性:copy_to
使用的步骤:
1、创建mapping
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"first_name": {
"type": "keyword",
"copy_to": "full_name"
},
"last_name": {
"type": "keyword",
"copy_to": "full_name"
},
"full_name": {
"type": "text",
"fielddata": true
}
}
}
}
}
2、插入数据
PUT my_index/my_type/1
{
"first_name": "John",
"last_name": "Smith"
}
3、查询校验
GET my_index/_search
{
"query": {
"match": {
"full_name": {
"query": "John Smith",
"operator": "and"
}
}
}
}
4、结果展示{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.51623213,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "1",
"_score": 0.51623213,
"_source": {
"first_name": "John",
"last_name": "Smith"
}
}
]
}
}
5、聚合查询校验
{
"query": {
},"aggs": {
"1": {
"terms": {
"field": "full_name",
"size": 10
}
}
}
}
6、查询结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "baobiaoceshi4",
"_type": "my_type",
"_id": "1",
"_score": 1,
"_source": {
"first_name": "John",
"last_name": "Smith"
}
}
]
},
"aggregations": {
"1": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "john",
"doc_count": 1
},
{
"key": "smith",
"doc_count": 1
}
]
}
}
}
注意有这几个问题:
1、我们copy_to指向的字段字段类型要为:text
2、text类型字段如果希望进行聚合,设置属性:"fielddata": true
3、copy_to指向的字段不会在head插件查看时显示,但是能通过查询语句作为条件
总结:通过这种方式对我们的结果进行聚合,能够满足一次查询聚合多个字段。非常棒!
官网文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。