ElasticSearch - 索引增加字段并查询增加字段前的历史数据

这篇具有很好参考价值的文章主要介绍了ElasticSearch - 索引增加字段并查询增加字段前的历史数据。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. 问题引入

我们项目中有一个需求:ElasticSearch存在很多历史数据,然后需求中索引新增了一个字段,我们需要根据条件查询出历史数据,但历史数据中这个新增的字段并不存在,如何查询到历史数据呢?

1. 索引2个文档
PUT /user/_doc/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

PUT /user/_doc/2
{
    "first_name" : "zhangsan",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
2. 给索引增加新的字段
PUT /user/_mapping
{
  "properties": {
      "height": {
        "type": "long"
      }
  }
}
3. 再次索引1个文档

这个文档新增了height字段的值

PUT /user/_doc/3
{
    "first_name" : "lisi",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ],
    "height":175
}
4. 查看索引中的文档
GET /user/_search
{
  "took" : 817,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "zhangsan",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "lisi",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ],
          "height" : 175
        }
      }
    ]
  }
}

从上面的结果可以看出,在ElasticSearch中为已有索引增加一个新字段以后,老的数据并不会自动就拥有了这个新字段,也就不可能给他一个默认值。因此前面2条数据都没有 height 这个字段。

在ElasticSearch中,如果一个字段不存在或者这个字段的值为null,在检索的时候该字段会被忽略,因此也就无法做空值搜索。

PUT my_index/my_type/1
{
  "first_name": "zhangsan"
}
PUT my_index/my_type/2
{
  "first_name": "wangwu",
  "height": null
}

例如上面的2个文档,都无法根据 height 这个字段检索。那么我们如何查询到没增加字段之前的历史数据呢?

2. must_not & exist

POST /user/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field" : "height" 
          }
        }
      ]
    }
  }
}
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "first_name" : "zhangsan",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      }
    ]
  }
}

exists 返回在原始字段中至少有一个非空值的文档:

GET /user/_search
{
    "query": {
        "exists" : { "field" : "height" }
    }
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "lisi",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ],
          "height" : 175
        }
      }
    ]
  }
}

3. 给历史数据赋初值

对现有索引新增字段时并不会影响历史数据,因此我们可以修改历史数据文档,对历史数据设置默认值,然后根据默认值检索。
使用脚本批量更新文档:_update_by_query,如果字段的值为null,则给该字段赋初值为0

POST /user/_update_by_query
{
  "script": {
    "lang": "painless",
    "inline": "if (ctx._source.height== null) {ctx._source.height=0}"
  }
}

再次查看索引的文档:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "about" : "I love to go rock climbing",
          "last_name" : "Smith",
          "interests" : [
            "sports",
            "music"
          ],
          "first_name" : "John",
          "age" : 25,
          "height" : 0
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "about" : "I love to go rock climbing",
          "last_name" : "Smith",
          "interests" : [
            "sports",
            "music"
          ],
          "first_name" : "zhangsan",
          "age" : 25,
          "height" : 0
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "about" : "I love to go rock climbing",
          "last_name" : "Smith",
          "interests" : [
            "sports",
            "music"
          ],
          "first_name" : "lisi",
          "age" : 25,
          "height" : 175
        }
      }
    ]
  }
}

历史数据中 height 字段都有了默认值 0文章来源地址https://www.toymoban.com/news/detail-495663.html

到了这里,关于ElasticSearch - 索引增加字段并查询增加字段前的历史数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ES映射操作 已创建的ES索引 增加映射字段(类似DB库增加给表增加字段)一样

    ES已存在的索引下添加映射 解释如下:给ticketing_order_info的索引,增加映射字段verificationCodeState,字段类型为integer 实现: PUT /ticketing_order_info/_mapping/ {     \\\"properties\\\": {         \\\"verificationCodeState\\\": {             \\\"type\\\": \\\"integer\\\"         }     } } PUT /ticketing_order_i

    2024年02月16日
    浏览(46)
  • ElasticSearch修改分片数和副本数及增加字段

    一、修改副本数 PUT test/_settings {     \\\"index\\\": {         \\\"number_of_replicas\\\" : 1     } } 二、修改分片数 ElasticSearch中的数据会被分别存储在不同的分片上,索引库的分片数量是在索引库创建的时候通过settings去设置的,如果不设置,分片数默认是5,分片数一旦确定就不能改变。如果

    2024年02月05日
    浏览(36)
  • ES查询索引字段的分词结果

    一、_termvectors  1、查看文档中某一个字段的分词结果 GET /{index}/{type}/{_id}/_termvectors?fields=[field] 2、样例: text的值为:https://www.b4d99.com/html/202204/45672.html 得到的结果: 二、_analyze 1、语法 2、样例: text的值为:https://www.b4d99.com/html/202204/45672.html 得到的结果:

    2024年02月11日
    浏览(48)
  • ES新增字段后,查询索引中不显示这个字段

    修改ES结构,新增字段 注:为避免修改后该文件结构损坏,修改前先备份 1.复制文档 2.新增字段(text类型) 3.查询字段是否添加成功 诶?!这个时候发现没有添加成功? 然后我通过es的head插件可以看到这个字段 这个时候,我们只需要对字段进行初始化,给一个默认值就解决了

    2024年02月15日
    浏览(33)
  • 业务测试——历史数据

    业务测试历史数据的必要性 1.保留上一版本的呈现效果以及数据正确性 2.做发版前后数据、样式一致性校验 3.后端处理历史数据,覆盖各类场景,保证客户的现有数据不会被影响,造成线上事务 4.为测试过程的覆盖度以及产品迭代的质量保驾护航 如何做历史数据(发版前截图

    2024年02月14日
    浏览(28)
  • 新浪股票接口获取历史数据

    这两天做了一个调用新浪股票接口获取实时以及历史股票数据的应用,因为新浪没有公开关于其接口的官方文档,所以通过各种百度差了很多关于新浪股票接口的使用,不过大家基本都是转载或者直接复制,对于实时数据的获取讲的很详细,但是缺少获取历史数据的方法。

    2024年02月10日
    浏览(35)
  • 自动清理 ES 历史数据

    目录 一、 背景 二、解决方案 三、实现操作 三、合并定时任务的例子         随着业务的增长和时间的变化,ES 数据库的存储空间越来越大,存储数据多数为系统监控日志,保存的数据不需要长期保留,多数情况只需要保留几个月ES数据即可,既可以减轻ES服务器的负载和

    2024年02月08日
    浏览(35)
  • ElasticSearch修改索引字段类型

    线上功能报错,一看日志是往es中添加数据报错,错误日志如下: 说是数据中有个字段类型转换错误,一查es脚本工具,果然生产es索引中categoryId这个字段是integer类型,而实际是long类型。 es不能直接修改索引字段类型,需要删除调新建,具体方法如下 我这次遇到问题的es索引

    2023年04月08日
    浏览(31)
  • 数据仓库保存历史数据方法之拉链表

    数据仓库是一个面向主题的、集成的、相对稳定的、反应历史变化的数据集合,用于支持管理决策。 面向主题:传统的数据库是面向事务处理的,而数据仓库是面向某一领域而组织的数据集合,主题是指用户关心的某一联系紧密的集合。 集成:数据仓库中数据来源于各个离

    2024年03月13日
    浏览(36)
  • sql server删除历史数据

    datediff函数 : datepart的取值可以是year,quarter,Month,dayofyear,Day,Week,Hour,minute,second,millisecond startdate 是从 enddate 减去。如果 startdate 比 enddate 晚,返回负值。 删除2023年以前的数据 运行结果如下:

    2024年02月10日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包