如何在Elasticsearch的现有映射中添加新字段时添加默认值 [英] How to add default values while adding a new field in existing mapping in elasticsearch

查看:656
本文介绍了如何在Elasticsearch的现有映射中添加新字段时添加默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现有的弹性搜索子文档之一的映射

This is my existing mapping in elastic search for one of the child document

sessions" : {
    "_routing" : {
    "required" : true
    },
    "properties" : {
       "operatingSystem" : {
       "index" : "not_analyzed",
       "type" : "string"
    },
    "eventDate" : {
       "format" : "dateOptionalTime",
       "type" : "date"
    },
    "durations" : {
       "type" : "integer"
    },
    "manufacturer" : {
       "index" : "not_analyzed",
       "type" : "string"
    },
    "deviceModel" : {
       "index" : "not_analyzed",
       "type" : "string"
    },
    "applicationId" : {
       "type" : "integer"
    },
    "deviceId" : {
       "type" : "string"
    }
    },
    "_parent" : {
       "type" : "userinfo"
    }
}

在上面的映射持续时间字段是一个整数数组。我需要添加一个名为 durationCount的新字段来更新现有的映射,该字段的默认值应为durations数组的大小。

in above mapping "durations" field is an integer array. I need to update the existing mapping by adding a new field called "durationCount" whose default value should be the size of durations array.

PUT sessions/_mapping
{
    "properties" : {
    "sessionCount" : {
      "type" :    "integer"
    }
  }
}

使用上述映射,我可以更新现有映射,但无法找出在更新映射时如何分配值(每个会话文档的值会有所不同,就像应该为durations array size一样)。有什么想法吗?

using above mapping I am able to update the existing mapping but I am not able to figure out how to assign a value ( which would vary for each session document like it should be durations array size ) while updating the mapping. any ideas ?

推荐答案

这里有2条建议-


  1. 您可以使用缺少的过滤器在查询中调整默认值,而不是添加默认值。假设您要基于匹配查询进行搜索-而不是仅使用匹配查询,请使用布尔查询以及具有匹配项和缺少过滤器。在经过过滤的查询中。这样,那些没有该字段的文档也会被考虑。

  2. 如果您绝对需要现有文档在该字段中的值,则需要重新索引整个文档集。或者,使用开箱即用的插件通过查询进行更新-

  1. Instead of adding default value , you can adjust it in the query using missing filter. Lets say , you want to search based on a match query - Instead of just match query , use a bool query with should clause having the match and missing filter. inside filtered query. This way , those documents which did not have the field is also accounted.
  2. If you absolutely need the value in that field for existing documents , you need to reindex the whole set of documents. Or , use the out of box plugin , update by query -

这篇关于如何在Elasticsearch的现有映射中添加新字段时添加默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆