国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

ElasticSearch - 索引增加字段并查詢?cè)黾幼侄吻暗臍v史數(shù)據(jù)

這篇具有很好參考價(jià)值的文章主要介紹了ElasticSearch - 索引增加字段并查詢?cè)黾幼侄吻暗臍v史數(shù)據(jù)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1. 問題引入

我們項(xiàng)目中有一個(gè)需求:ElasticSearch存在很多歷史數(shù)據(jù),然后需求中索引新增了一個(gè)字段,我們需要根據(jù)條件查詢出歷史數(shù)據(jù),但歷史數(shù)據(jù)中這個(gè)新增的字段并不存在,如何查詢到歷史數(shù)據(jù)呢?

1. 索引2個(gè)文檔
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個(gè)文檔

這個(gè)文檔新增了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
        }
      }
    ]
  }
}

從上面的結(jié)果可以看出,在ElasticSearch中為已有索引增加一個(gè)新字段以后,老的數(shù)據(jù)并不會(huì)自動(dòng)就擁有了這個(gè)新字段,也就不可能給他一個(gè)默認(rèn)值。因此前面2條數(shù)據(jù)都沒有 height 這個(gè)字段。

在ElasticSearch中,如果一個(gè)字段不存在或者這個(gè)字段的值為null,在檢索的時(shí)候該字段會(huì)被忽略,因此也就無法做空值搜索。

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

例如上面的2個(gè)文檔,都無法根據(jù) height 這個(gè)字段檢索。那么我們?nèi)绾尾樵兊經(jīng)]增加字段之前的歷史數(shù)據(jù)呢?

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 返回在原始字段中至少有一個(gè)非空值的文檔:

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. 給歷史數(shù)據(jù)賦初值

對(duì)現(xiàn)有索引新增字段時(shí)并不會(huì)影響歷史數(shù)據(jù),因此我們可以修改歷史數(shù)據(jù)文檔,對(duì)歷史數(shù)據(jù)設(shè)置默認(rèn)值,然后根據(jù)默認(rèn)值檢索。
使用腳本批量更新文檔:_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
        }
      }
    ]
  }
}

歷史數(shù)據(jù)中 height 字段都有了默認(rèn)值 0文章來源地址http://www.zghlxwxcb.cn/news/detail-495663.html

到了這里,關(guān)于ElasticSearch - 索引增加字段并查詢?cè)黾幼侄吻暗臍v史數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ES映射操作 已創(chuàng)建的ES索引 增加映射字段(類似DB庫增加給表增加字段)一樣

    ES已存在的索引下添加映射 解釋如下:給ticketing_order_info的索引,增加映射字段verificationCodeState,字段類型為integer 實(shí)現(xiàn): PUT /ticketing_order_info/_mapping/ { ????\\\"properties\\\": { ????????\\\"verificationCodeState\\\": { ????????????\\\"type\\\": \\\"integer\\\" ????????} ????} } PUT /ticketing_order_i

    2024年02月16日
    瀏覽(22)
  • ElasticSearch修改分片數(shù)和副本數(shù)及增加字段

    一、修改副本數(shù) PUT test/_settings { ? ? \\\"index\\\": { ? ? ? ? \\\"number_of_replicas\\\" : 1 ? ? } } 二、修改分片數(shù) ElasticSearch中的數(shù)據(jù)會(huì)被分別存儲(chǔ)在不同的分片上,索引庫的分片數(shù)量是在索引庫創(chuàng)建的時(shí)候通過settings去設(shè)置的,如果不設(shè)置,分片數(shù)默認(rèn)是5,分片數(shù)一旦確定就不能改變。如果

    2024年02月05日
    瀏覽(19)
  • ES查詢索引字段的分詞結(jié)果

    一、_termvectors? 1、查看文檔中某一個(gè)字段的分詞結(jié)果 GET /{index}/{type}/{_id}/_termvectors?fields=[field] 2、樣例: text的值為:https://www.b4d99.com/html/202204/45672.html 得到的結(jié)果: 二、_analyze 1、語法 2、樣例: text的值為:https://www.b4d99.com/html/202204/45672.html 得到的結(jié)果:

    2024年02月11日
    瀏覽(83)
  • ES新增字段后,查詢索引中不顯示這個(gè)字段

    修改ES結(jié)構(gòu),新增字段 注:為避免修改后該文件結(jié)構(gòu)損壞,修改前先備份 1.復(fù)制文檔 2.新增字段(text類型) 3.查詢字段是否添加成功 誒?!這個(gè)時(shí)候發(fā)現(xiàn)沒有添加成功? 然后我通過es的head插件可以看到這個(gè)字段 這個(gè)時(shí)候,我們只需要對(duì)字段進(jìn)行初始化,給一個(gè)默認(rèn)值就解決了

    2024年02月15日
    瀏覽(22)
  • 業(yè)務(wù)測(cè)試——?dú)v史數(shù)據(jù)

    業(yè)務(wù)測(cè)試歷史數(shù)據(jù)的必要性 1.保留上一版本的呈現(xiàn)效果以及數(shù)據(jù)正確性 2.做發(fā)版前后數(shù)據(jù)、樣式一致性校驗(yàn) 3.后端處理歷史數(shù)據(jù),覆蓋各類場景,保證客戶的現(xiàn)有數(shù)據(jù)不會(huì)被影響,造成線上事務(wù) 4.為測(cè)試過程的覆蓋度以及產(chǎn)品迭代的質(zhì)量保駕護(hù)航 如何做歷史數(shù)據(jù)(發(fā)版前截圖

    2024年02月14日
    瀏覽(18)
  • 新浪股票接口獲取歷史數(shù)據(jù)

    新浪股票接口獲取歷史數(shù)據(jù)

    這兩天做了一個(gè)調(diào)用新浪股票接口獲取實(shí)時(shí)以及歷史股票數(shù)據(jù)的應(yīng)用,因?yàn)樾吕藳]有公開關(guān)于其接口的官方文檔,所以通過各種百度差了很多關(guān)于新浪股票接口的使用,不過大家基本都是轉(zhuǎn)載或者直接復(fù)制,對(duì)于實(shí)時(shí)數(shù)據(jù)的獲取講的很詳細(xì),但是缺少獲取歷史數(shù)據(jù)的方法。

    2024年02月10日
    瀏覽(31)
  • 自動(dòng)清理 ES 歷史數(shù)據(jù)

    自動(dòng)清理 ES 歷史數(shù)據(jù)

    目錄 一、 背景 二、解決方案 三、實(shí)現(xiàn)操作 三、合并定時(shí)任務(wù)的例子 ??????? 隨著業(yè)務(wù)的增長和時(shí)間的變化,ES 數(shù)據(jù)庫的存儲(chǔ)空間越來越大,存儲(chǔ)數(shù)據(jù)多數(shù)為系統(tǒng)監(jiān)控日志,保存的數(shù)據(jù)不需要長期保留,多數(shù)情況只需要保留幾個(gè)月ES數(shù)據(jù)即可,既可以減輕ES服務(wù)器的負(fù)載和

    2024年02月08日
    瀏覽(22)
  • ElasticSearch修改索引字段類型

    ElasticSearch修改索引字段類型

    線上功能報(bào)錯(cuò),一看日志是往es中添加數(shù)據(jù)報(bào)錯(cuò),錯(cuò)誤日志如下: 說是數(shù)據(jù)中有個(gè)字段類型轉(zhuǎn)換錯(cuò)誤,一查es腳本工具,果然生產(chǎn)es索引中categoryId這個(gè)字段是integer類型,而實(shí)際是long類型。 es不能直接修改索引字段類型,需要?jiǎng)h除調(diào)新建,具體方法如下 我這次遇到問題的es索引

    2023年04月08日
    瀏覽(34)
  • 數(shù)據(jù)倉庫保存歷史數(shù)據(jù)方法之拉鏈表

    數(shù)據(jù)倉庫是一個(gè)面向主題的、集成的、相對(duì)穩(wěn)定的、反應(yīng)歷史變化的數(shù)據(jù)集合,用于支持管理決策。 面向主題:傳統(tǒng)的數(shù)據(jù)庫是面向事務(wù)處理的,而數(shù)據(jù)倉庫是面向某一領(lǐng)域而組織的數(shù)據(jù)集合,主題是指用戶關(guān)心的某一聯(lián)系緊密的集合。 集成:數(shù)據(jù)倉庫中數(shù)據(jù)來源于各個(gè)離

    2024年03月13日
    瀏覽(22)
  • sql server刪除歷史數(shù)據(jù)

    sql server刪除歷史數(shù)據(jù)

    datediff函數(shù) : datepart的取值可以是year,quarter,Month,dayofyear,Day,Week,Hour,minute,second,millisecond startdate 是從 enddate 減去。如果 startdate 比 enddate 晚,返回負(fù)值。 刪除2023年以前的數(shù)據(jù) 運(yùn)行結(jié)果如下:

    2024年02月10日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包