场景:
XX对象的告警个数字段是数字, 优先级字段是数字
排序要求: 优先告警个数大->小, 其次优先级大->小
问题:
告警个数字段不存在或者值为0的情况是等价的,排序上应该是平等的.
不应该存在0的大于字段不存在的
处理:
对没有告警个数字段的默认设置0 , 那么排序的时候才正确
使用Missing - 给字段设置默认值
一般用于排序时,该字段没有值,使用missing赋值默认值,然后就用赋的值进行排序
GET indexname/_search
{
"sort": [
{
"alarmNum": {
"order": "asc",
"missing": 0 -- 此字段不存在的时候, 值为0 . 与值0是一样排序
}
},
{
"priority_code":{
"order": "asc"
}
}
],
"from":0,
"size":100
}
unmapped_type - 容错不存在的字段
如果mapping中无此字段,则搜索请求将失败.unmapped_type选项允许您忽略没有mapping的字段,也不按它们排序。参数的值为该字段对应值的类型。
这种场景一般不存在.因为index至少有一条数据存在此字段有值,mapper里面就有
如何不报错?文章来源:https://www.toymoban.com/news/detail-551748.html
GET project_base_info/_search
{
"sort": [
{
"alarmNum1": {
"order": "asc",
"missing": 0,
"unmapped_type":"long" -- 有了此配置 ,mapper没有也不会报错
}
},
{
"project_priority_code":{
"order": "asc"
}
}
],
"from":0,
"size":100
}
pipeline 设置默认值:属于前置设置默认值
在插入数据时候,给字段设置给默认值. 但问题是,后续也无法修改此字段. 这个应该很少有这个需求文章来源地址https://www.toymoban.com/news/detail-551748.html
PUT _ingest/pipeline/add_zcy_pipeline
{
"processors": [
{
"set": {
"field": "projectCode",
"value": "10000000"
}
}
]
}
设置projectCode 默认值10000000
创建索引:
PUT projecy_zcy3
{
"settings": {
"refresh_interval": "1s",
"number_of_shards": 1,
"number_of_replicas": 1,
"index":{
"default_pipeline":"add_zcy_pipeline"
}
},
"mappings": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
},
"keylong": {
"type": "long"
}
}
},
"projectCode": {
"type": "text"
},
"projectName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"projectStatus": {
"type": "keyword"
}
}
}
}
插入数据
PUT projecy_zcy3/_doc/2
{
"id":"1022",
"projectName":"ssss"
}
结果:
"_source" : {
"projectCode" : "10000000",
"id" : "1022",
"projectName" : "ssss"
}
PUT projecy_zcy3/_doc/2
{
"id":"1022",
"projectName":"ssss",
"projectCode":"111"
}
结果还是
"_source" : {
"projectCode" : "10000000",
"id" : "1022",
"projectName" : "ssss"
}
设置默认值--script执行批量
# 批量更新脚本
POST customer/_update_by_query
{
"script": {
"lang": "painless",
"source": "if (ctx._source.major == null) {ctx._source.major = 'student'}"
}
}
设置默认值--借助 pipeline script 更新
PUT _ingest/pipeline/update_pipeline
{
"processors": [
{
"script": {
"lang": "painless",
"source": """
if (ctx['major'] == null) {ctx['major'] = 'student'}
"""
}
}
]
}
POST customer/_doc/4
{
"major":null
}
POST customer/_update_by_query?pipeline=update_pipeline
{
"query": {
"match_all": {}
}
}
到了这里,关于ES: 设置默认值的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!