您正在寻找的是两个字符串的串联。copy_to
即使看起来正在这样做,也不会。从copy_to
概念上讲,与您一起从field1
和两者创建一组值,而field2
不是将它们连接在一起。
对于您的用例,您有两种选择:
我建议进行_source
转换,因为我认为它比编写脚本更有效。意思是,与进行繁重的脚本聚合相比,您在索引编制时付出的代价很小。
对于 :
PUT /lastseen
{
"mappings": {
"test": {
"transform": {
"script": "ctx._source['all_fields'] = ctx._source['field1'] + ' ' + ctx._source['field2']"
},
"properties": {
"field1": {
"type": "string"
},
"field2": {
"type": "string"
},
"lastseen": {
"type": "long"
},
"all_fields": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
和查询:
GET /lastseen/test/_search
{
"aggs": {
"NAME": {
"terms": {
"field": "all_fields",
"size": 10
}
}
}
}
对于 ,为了易于执行(意味着使用doc['field'].value
而不是使用更昂贵的_source.field
),请.raw
向field1
和添加子字段field2
:
PUT /lastseen
{
"mappings": {
"test": {
"properties": {
"field1": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"field2": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"lastseen": {
"type": "long"
}
}
}
}
}
脚本将使用以下.raw
子字段:
{
"aggs": {
"NAME": {
"terms": {
"script": "doc['field1.raw'].value + ' ' + doc['field2.raw'].value",
"size": 10,
"lang": "groovy"
}
}
}
}
如果没有.raw
子字段(是故意创建的not_analyzed
),您将需要执行以下操作,这会变得更加昂贵:
{
"aggs": {
"NAME": {
"terms": {
"script": "_source.field1 + ' ' + _source.field2",
"size": 10,
"lang": "groovy"
}
}
}
}