Elasticsearch 数据建模的一些建议

如何处理关联关系

clipboard.png

  • Kibana 目前暂时不支持 nested 类型和 parent/child 类型 ,在将来有可能会支持,在Kibana界面上展现数据的时候不是很友好
  • 若是须要使用Kibana进行数据分析,在建模时须要对嵌套和父子类型进行取舍

避免过多字段

  1. 过多字段很差维护
  2. Mapping信息保存在Cluster State中,数据量过大,对集群性能有影响(Cluster State信息须要和全部节点同步)
  3. 删除或者修改数据须要reindex.
  4. 默认最大字段数是 1000,能够设置 index.mapping.total_fields.limt 限定最大字段数
  5. 生产环境尽可能不要打开Dynamic Dynamic Mapping
  • 考虑下这样一个场景,
##索引数据,dynamic mapping 会不断加入新增字段
PUT cookie_service/_doc/1
{
 "url":"www.google.com",
 "cookies":{
   "username":"tom",
   "age":32
 }
}

PUT cookie_service/_doc/2
{
 "url":"www.amazon.com",
 "cookies":{
   "login":"2019-01-01",
   "email":"xyz@abc.com"
 }
}

咱们的cookie字段使用了Dynamic=true默认值,因此随着写入的数据愈来愈多,若是不对cookies字段的子字段进行限制的话,字段数会愈来愈多,会影响性能,cookie

  • 解决方案
#使用 Nested 对象,增长key/value
PUT cookie_service
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "cookies": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword"
          },
          "dateValue": {
            "type": "date"
          },
          "keywordValue": {
            "type": "keyword"
          },
          "IntValue": {
            "type": "integer"
          }
        }
      },
      "url": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

须要说明几点的是:app

  • 设置"dynamic": "strict"阻止其余字段加入
  • 经过name和dateValue, keywordValue, IntValue这样的设计能够存储任意的key-value

避免正则查询

正则查询的性能不够好,前缀查询属于Term查询性能

  • 案例以下:

文档中某个字段包含了Elasticsearch的版本信息,例如version: "7.1.0",如今咱们须要查询朱版本是7,次要版本是2的文档,不要使用正则查询google

  • 解决方案以下: 咱们使用inner object 将几个版本信息拆开存储,
PUT softwares/
{
  "mappings": {
    "_meta": {
      "software_version_mapping": "1.1"
    },
    "properties": {
      "version": {
        "properties": {
          "display_name": {
            "type": "keyword"
          },
          "hot_fix": {
            "type": "byte"
          },
          "marjor": {
            "type": "byte"
          },
          "minor": {
            "type": "byte"
          }
        }
      }
    }
  }
}

而后咱们再使用查询url

POST softwares/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match":{
            "version.marjor":7
          }
        },
        {
          "match":{
            "version.minor":2
          }
        }

      ]
    }
  }
}

避免空值引发的聚合不许

  • 首先咱们先看一下示例
PUT ratings/_doc/1
{
 "rating":5
}
PUT ratings/_doc/2
{
 "rating":null
}

POST ratings/_search
{
  "size": 0,
  "aggs": {
    "avg": {
      "avg": {
        "field": "rating"
      }
    }
  }
}

# 查询结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg" : {
      "value" : 5.0
    }
  }
}

很明显,咱们查到了两条数据,可是平均值是5,这个很难以理解,spa

  • 解决方案,咱们给null值设置一个默认值
PUT ratings
{
  "mappings": {
    "properties": {
      "rating": {
        "type": "float",
        "null_value": 0
      }
    }
  }
}

再次插入上面的数据,咱们获得下面的结果设计

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg" : {
      "value" : 2.5
    }
  }
}

这样就比较对了,固然null_value的值是能够本身根据业务需求本身设定的3d

相关文章
相关标签/搜索