Es-脚本截取分组聚合

Es-脚本截取分组聚合
今天遇到一个问题,es中存储,字段数据为 农业部#high#US,以#分隔,我需要取出来第二个位置和第三个位置进行分组聚合

创建索引
PUT /my_index
{
 "settings": {
  "index": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "_doc": {
      "properties": {
	     "my_field": {
         "type": "text",
	      "fields": {
               "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
           }
      	}
      }
    }
  }
}
添加文档:
put /my_index/_doc/1
{
  "my_field":"信息部#low#china"
}
......多加几条
脚本截取分组聚合:
GET my_index/_search
{
  "query": {
    "match_all": {}
  },"aggs": {
    "NAME": {
      "terms":{
       "script": {
              "source": "def domain = doc['my_field.keyword'].value;def beginIndex1 =domain.indexOf('#');def domain1 = domain.substring(beginIndex1+1);def beginIndex2 =domain1.indexOf('#');def domain2 =domain1.substring(beginIndex2+1);def domain3 =domain.substring(beginIndex1+1,beginIndex1+beginIndex2+1);return domain3"
            }
      }
    }
  }
} 

在这里插入图片描述
//获取字符串#第一次出现的位置,如果没有则为-1
def beginIndex =doc[‘my_field.keyword’]…indexOf(’#’);
//获取字符串#最后一次出现的位置,如果没有则为-1
def lastIndex = doc[‘my_field.keyword’].value.lastIndexOf(’#’)
//截取字符串(beginIndex+1)到
def domain1 = domain.substring(beginIndex+1);
问题可以解决完成。

脚本的问题,脚本的语法需要再了解,for循环和*分割为数组没有找到准确的语法

正则查询:
put /my_index/_doc/12
{
  "my_field":"信息部abc"
}

GET /my_index/_search
{
"query": {
"regexp": {
"my_field.keyword": "信息部[a-z]{1,3}"
}
}
}


GET /my_index/_search
{
"query": {
"wildcard": {
"my_field.keyword": "信息部???"
}
}
}

在这里插入图片描述

******todo
建表自定义分区,分组查询时,有问题,一个思路,没走通

以#分词

PUT /my_index
{
 "settings": {
  "index": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "analysis": {
        "analyzer": {
            "comma": {
                 "type": "pattern",
                 "pattern":"#"
                }
            }
        }
    },
  "mappings": {
    "_doc": {
      "properties": {
	     "my_field": {
        "type": "text",
        "analyzer": "comma",
	      "fields": {
               "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
           }
      	}
      }
    }
  }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值