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}"
}
}
再次查看索引的文檔:文章來源:http://www.zghlxwxcb.cn/news/detail-495663.html
{
"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)!