当前位置:   article > 正文

使用Elasticsearch映射定义索引结构_es 定义索引结构

es 定义索引结构

Elasticsearch中,**映射(Mapping)**是用于定义索引中文档字段的结构、类型及属性的重要组成部分。它相当于数据库表结构的设计,决定了如何对文档中的数据进行解析、存储和检索。本文将详细介绍映射的概念、支持的常规字段类型、如何忽略映射中不合法的数据、实现字段复制与字段存储,以及动态映射的运用。

映射概念与使用

映射定义了索引中每个字段的名称、类型及其特定属性。在创建索引或向索引中添加文档时,Elasticsearch会自动或手动应用映射规则。以下是一个简单的索引创建示例:

PUT first-index
{
  "settings": {
    "number_of_shards": "5",
    "number_of_replicas": "1"
  },
  "mappings": {
    "properties": {
      "content": {
        "type": "text"
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这个例子中,我们创建了一个名为first-index的索引,并指定了其分片数和副本数。在mappings部分,定义了一个名为content的字段,类型为text。这样,当我们向该索引添加文档时,Elasticsearch会根据映射规则正确解析和处理content字段的内容。

常规字段类型

Elasticsearch支持多种字段类型以适应不同的数据需求。以下是一些常见的字段类型:

  • text: 用于全文本搜索的字符串类型,会被分词器进行分词处理。
  • keyword: 不分词的字符串类型,用于精确值匹配、排序或聚合。
  • date: 存储日期和时间数据,支持各种格式指定。
  • boolean: 存储布尔值。
  • geo_point: 存储地理坐标信息。
  • integerfloatdouble: 存储数值类型数据。
  • binary: 存储二进制数据。
  • object: 定义嵌套结构,包含多个子字段。
  • array: 存储一组相同类型的值。

例如,定义一个包含userid(text类型)、visittime(date类型,指定时间格式)和sex(boolean类型)的索引映射:

PUT mysougoulog
{
  "mappings": {
    "properties": {
      "userid": {
        "type": "text"
      },
      "visittime": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss ||epoch_millis"
      },
      "sex": {
        "type": "boolean"
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

忽略映射中不合法的数据

在某些场景下,可能需要对索引中某些字段的非法数据进行忽略而非导致整个文档插入失败。可以通过设置ignore_malformed属性来实现这一功能:

PUT ignore-test
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer"
      },
      "born": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss",
        "ignore_malformed": true
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在上述映射中,born字段被设置为忽略不合法数据。当插入包含非法born值的文档时,Elasticsearch会忽略该字段的错误值并继续处理其他字段。

字段复制与字段存储

字段复制

通过copy_to属性,可以将一个或多个字段的值复制到另一个字段,便于进行全局搜索或其他复杂查询。例如,创建一个索引,将titleauthorabstract字段的值复制到full_text字段:

PUT copy-field
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "copy_to": "full_text"
      },
      "author": {
        "type": "text",
        "copy_to": "full_text"
      },
      "abstract": {
        "type": "text",
        "copy_to": "full_text"
      },
      "full_text": {
        "type": "text"
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

现在,可以通过查询full_text字段来同时搜索titleauthorabstract的内容:

POST copy-field/_search
{
  "query": {
    "match": {
      "full_text": "smith"
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

字段存储

默认情况下,Elasticsearch只存储原始文档的源数据和用于搜索的倒排索引。如果需要在搜索结果中直接返回某个字段的值,可以设置store属性为true,使字段值被持久化存储到磁盘:

PUT copy-store-field
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "copy_to": "full_text"
      },
      "author": {
        "type": "text",
        "copy_to": "full_text"
      },
      "abstract": {
        "type": "text",
        "copy_to": "full_text"
      },
      "full_text": {
        "type": "text",
        "store": true
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

查询时,使用stored_fields参数指定要返回的已存储字段:

POST copy-store-field/_search
{
  "stored_fields": ["full_text"]
}
  • 1
  • 2
  • 3
  • 4

动态映射

Elasticsearch具有动态映射功能,当索引中出现未事先定义的字段时,会自动为其创建映射。这在处理未知结构或快速迭代的数据时非常有用。例如,尝试向一个新索引date-test中添加一条包含create_date字段的文档:

PUT date-test/_doc/1
{
  "create_date": "2015/09/02 00:00:00"
}
  • 1
  • 2
  • 3
  • 4

查询映射信息,可以看到Elasticsearch已自动为create_date字段创建了映射:

GET date-test/_mapping
  • 1

若需要自定义动态映射的行为,如指定日期格式,可以在创建索引时设置dynamic_date_formats属性:

PUT date-test2
{
  "mappings": {
    "dynamic_date_formats": ["yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

总结起来,Elasticsearch的映射机制为索引结构提供了强大的灵活性和控制力。通过合理设计映射,可以优化数据存储、提升搜索性能,并确保数据的一致性和完整性。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/853880
推荐阅读
相关标签
  

闽ICP备14008679号