且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Elasticsearch的现有映射中添加新字段时添加默认值

更新时间:2023-01-15 11:43:30

这里有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 -