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
}
}
}
}
}
}
}