有没有办法将弹性搜索字段类型从小写转换为标题大写?

Is there any way to convert the elastic search field type from lower case to Title case?

我的关键字段之一的弹性搜索输出正在以全小写形式填充,如下所示。

   {
"key" : "activatedevice",
"doc_count" : 2
},

有什么方法可以将映射添加到这个字段,以便我的输出看起来像这样

   {
"key" : "ActivateDevice",
"doc_count" : 2
}'

目前我为这个键定义的映射如下

"CallingServiceName": {
                  "index": "not_analyzed",
                  "type": "string"
               },

有什么建议可以实现吗?

谢谢

猜测这种连接字符串的单词边界非常困难。

如果您的字符串是 space 分隔的(或具有任何其他可拆分模式),您可以使用以下聚合脚本将您的 CallingServiceName 转换为 TitleCase

{
  "size": 0,
  "aggs": {
    "by_csn": {
      "terms": {
        "script": {
          "source": """
             String toTitleCase(def s){
               def parts = / /.split(s);
               String titleCaseString = "";
               for (def part : parts){
                  titleCaseString = titleCaseString + toProperCase(part);
               }
               return titleCaseString;
            }
            
            String toProperCase(def s) {
                return s.substring(0, 1).toUpperCase() +
                           s.substring(1).toLowerCase();
            }
            

            return toTitleCase(doc['CallingServiceName.keyword'].value);
          """
        },
        "size": 10
      }
    }
  }
}

请注意,您传递给 toTitleCase 函数的字段需要是 keyword 类型或在映射中将 fielddata 设置为 true。