Elasticsearch中flattened字段类型

为了优化索引性能,需要统计索引的字段数量。
Elasticsearch默认索引字段不能超过1000,由index.mapping.total_fields.limit参数进行设置。字段和对象映射,以及字段别名都计入这个限制。该值越大会导致内存不足和性能下降,特别在高负载的集群环境。

实际应用mapping定义通常采用动态模板进行定义,那么如何统计索引中字段数量,以及如何解决字段数超过限制问题。

统计索引中字段数量

ES没有提供_stat统计字段API,下面介绍字段能力API,可以返回多个索引的字段信息:

GET /_field_caps?fields=<fields>

POST /_field_caps?fields=<fields>

GET /<target>/_field_caps?fields=<fields>

POST /<target>/_field_caps?fields=<fields>

下面示例查看字段ratingtitle信息:

GET _field_caps?fields=rating,title

响应:

{
  "indices": [ "index1", "index2", "index3", "index4", "index5" ],
  "fields": {
    "rating": {                                   
      "long": {
        "searchable": true,
        "aggregatable": false,
        "indices": [ "index1", "index2" ],
        "non_aggregatable_indices": [ "index1" ]  
      },
      "keyword": {
        "searchable": false,
        "aggregatable": true,
        "indices": [ "index3", "index4" ],
        "non_searchable_indices": [ "index4" ]    
      }
    },
    "title": {                                    
      "text": {
        "searchable": true,
        "aggregatable": false

      }
    }
  }
}

共5个索引,返回每个字段所属那些索引及其类型。如title在所有索引中都存在。

查询某个索引的字段信息,同时包括未映射的字段:

GET /index_name/_field_caps?fields=*&include_unmapped

超过字段数量限制

自然的想法是能不能调大index.mapping.total_fields.limit参数,如果要调大,最好同时调整indices.query.bool.max_clause_count参数,即查找中布尔查询子句的最大数量。

  • 使用flattened数据类型

对于对象中的子字段默认被单独映射、索引。扁平字段类型是解决字段过多问题的另一种方法,它把整个对象映射未单个字段。给定对象,flattened 类型解析出每个子字段值作为keyword类型,对象中的内容可以通过单个查询进行搜索或聚集。对于唯一字段类型未知的情况非常有用,对于真个json对象仅创建一个映射字段,可以有效防止映射有太多的字段————映射爆炸。

另外扁平对象字段在搜索功能方面提供了一种折衷方案。只允许基本查询,不支持数字范围查询或高亮显示。

示例:

PUT bug_reports
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "labels": {
        "type": "flattened"
      }
    }
  }
}

POST bug_reports/_doc/1
{
  "title": "Results are not sorted correctly.",
  "labels": {
    "priority": "urgent",
    "release": ["v1.2.5", "v1.3.0"],
    "timestamp": {
      "created": 1541458026,
      "closed": 1541457010
    }
  }
}

POST bug_reports/_doc/2
{
  "title": "Results are not descripted correctly.",
  "labels": {
    "priority": "urgent",
    "release": ["v1.3.5", "v1.4.0"],
    "creator": "tester",
    "timestamp": {
      "created": 1541458026,
      "closed": 1541457010
    }
  }
}

这里定义了flattened类型labels字段,实际插入数据未对象,可以包括多个属性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值