栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

elasticsearch copy_to字段在聚合中的行为不正常

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

elasticsearch copy_to字段在聚合中的行为不正常

您正在寻找的是两个字符串的串联。

copy_to
即使看起来正在这样做,也不会。从
copy_to
概念上讲,与您一起从
field1
和两者创建一组值,而
field2
不是将它们连接在一起。

对于您的用例,您有两种选择:

  1. 使用
    _source
    转换
  2. 执行脚本聚合

我建议进行

_source
转换,因为我认为它比编写脚本更有效。意思是,与进行繁重的脚本聚合相比,您在索引编制时付出的代价很小。

对于

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号