Elasticsearch:flattened 数据类型 (7.3 发行版新功能)

1023 篇文章 596 订阅

默认情况下,对象中的每个子字段都需要分别进行映射和索引。如果事先不知道子字段的名称或类型,则将动态映射它们。

flattened 数据类型提供了一种替代方法,其中将整个对象映射为单个字段。对于给定的对象,flatten 类型映射将解析出其 leaf 值并将它们作为关键字索引到一个字段中。然后可以通过简单的查询和汇总来搜索对象的内容。

此数据类型对于索引具有大量或未知数量的唯一键的对象很有用。仅为整个 JSON 对象创建一个字段映射,这可以帮助防止由于大量不同的字段映射而导致映射爆炸

另一方面,flatten 的对象字段在搜索功能方面存在折衷。仅允许基本查询,不支持数字范围查询或突出显示(highlighting)。

在使用 flattened 数据类型时,必须注意的是:

flattened 的映射类型不应用于索引所有文档内容,因为它将所有值都视为关键字,并且不提供完整的搜索功能。 在大多数情况下,默认方法(每个子字段在映射中都有其自己相对应的项)有效。

下面我们来用一个例子来展示如何使用 flattened 数据类型的用法。我们首先来创建一个叫做 bug_reports 的索引及它的 mapping:

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

在上面,我们定义 labels 的数据类型为 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
    }
  }
}

在上面我们可以看出来: labels 是一个对象,它包含 priority, release 及 timestamp leaf 项。在建立索引的时候,Elasticsearch 为 JSON 对象的每一个 leaf 建立 token,这些值被索引为字符串关键字,无需对数字或日期进行特殊处理。针对我们的情况,Elasticsearch 在建立索引时,会对 urgent,v1.2.5, v1.3.0, 1541458026 及1541457010 建立 token, 并供我们进行搜索。

我们可以通过如下的方式来进行查询:

POST bug_reports/_search
{
  "query": {
    "term": {
      "labels": "urgent"
    }
  }
}

查询的结果是:

  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.42763555,
    "hits" : [
      {
        "_index" : "bug_reports",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.42763555,
        "_source" : {
          "title" : "Results are not sorted correctly.",
          "labels" : {
            "priority" : "urgent",
            "release" : [
              "v1.2.5",
              "v1.3.0"
            ],
            "timestamp" : {
              "created" : 1541458026,
              "closed" : 1541457010
            }
          }
        }
      }
    ]
  }

同样,我们可以对数值进行查询:

POST bug_reports/_search
{
  "query": {
    "term": {
      "labels": 1541458026
    }
  }
}

返回结果:

  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.42763555,
    "hits" : [
      {
        "_index" : "bug_reports",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.42763555,
        "_source" : {
          "title" : "Results are not sorted correctly.",
          "labels" : {
            "priority" : "urgent",
            "release" : [
              "v1.2.5",
              "v1.3.0"
            ],
            "timestamp" : {
              "created" : 1541458026,
              "closed" : 1541457010
            }
          }
        }
      }
    ]
  }

要查询 flattened 对象中的特定键,请使用 “." 来表示:

POST bug_reports/_search
{
  "query": {
    "term": {
      "labels.release": "v1.3.0"
    }
  }
}

返回结果:

  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.42763555,
    "hits" : [
      {
        "_index" : "bug_reports",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.42763555,
        "_source" : {
          "title" : "Results are not sorted correctly.",
          "labels" : {
            "priority" : "urgent",
            "release" : [
              "v1.2.5",
              "v1.3.0"
            ],
            "timestamp" : {
              "created" : 1541458026,
              "closed" : 1541457010
            }
          }
        }
      }
    ]
  }

支持的操作

由于索引值的方式相似,flattened 字段与 keyword 字段共享许多相同的映射和搜索功能,这是因为它们在建立索引时的方式非常相似。

目前,flattened的对象字段可以与以下查询类型一起使用:

  • termterms, and terms_set
  • prefix
  • range
  • match and multi_match
  • query_string and simple_query_string
  • exists

查询时,不可能使用通配符来引用字段关键字,例如 {“ term”:{“ labels.time *”:1541457010}}。 请注意,所有查询(包括范围)都将值视为字符串关键字。 拼合的字段不支持突出显示(highlighting)。

可以对 flattened 的对象字段进行排序,以及执行简单的关键字样式聚合(例如terms aggregation)。 与查询一样,对数字没有特殊支持-将 JSON 对象中的所有值都视为关键字。 排序时,这意味着按字典顺序对值进行比较。

展平的对象字段当前无法存储。 无法在映射中指定 store 参数。

参考:

【1】Flattened field type | Elasticsearch Guide [master] | Elastic

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值