Elasticsearch:索引mapping

这里写目录标题

一、介绍

二、动态mapping

三、mapping属性

(1)analyzer(分析器)

该analyzer参数指定在索引或搜索字段时用于 文本分析的分析器。text除非使用映射参数覆盖search_analyzer,否则此分析器将用于索引和搜索分析。

  • search_quote_analyzer
    该search_quote_analyzer设置允许您指定短语分析器,这在处理禁用短语查询的停用词时特别有用。要禁用短语的停用词,需要一个利用三个分析器设置的字段:

说明:只有text字段支持analyzer映射参数。

(2) coerce(强制类型转换)

数据不总是干净的.根据它的生成方式,一个数字可能会在 JSON body中呈现为一个真正的 JSON 数字。例如5,但它也可能呈现为字符串,例如 "5" 。或者,应该是整型的数字被替代地呈现为浮点型.例如, 5.0 或者"5.0".

Coercion尝试着清理脏数据让字段符合相应的数据类型.例如 :

  • 字符串被强制转换为数字。
  • 浮点型被截断为整型。

例如:

c 复制代码
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "properties": {
        "number_one": {
          "type": "integer"
        },
        "number_two": {
          "type": "integer",
          "coerce": false
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "number_one": "10"  # 1
}
'
curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -H 'Content-Type: application/json' -d'
{
  "number_two": "10"  # 2
}
'
  • number_one 字段会包含整型 10。
  • 由于强制功能已被禁用,因此该文件将被拒绝。

(3)copy_to(合并参数)

copy_to参数允许你创建自定义的 _all 字段.换句换来说,可以将多个字段的值复制到group field(组字段),然后可以作为单个字段进行查询.例如, first_name和 last_name可以复制到 full_name字段中,如下所示 :

c 复制代码
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "properties": {
        "first_name": {
          "type": "text",
          "copy_to": "full_name"  # 1
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name"  # 2
        },
        "full_name": {
          "type": "text"
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "full_name": {  # 3
        "query": "John Smith",
        "operator": "and"
      }
    }
  }
}
'
  • 1 , 2 - 》first_name(名字)和 last_name(姓氏)字段复制到full_name 字段。
  • 3 -》first_name(名字)和last_name(姓氏)字段仍然可以分别查询,full_name可以通过first_name(名字)和last_name(姓氏)来查询。

查看数据:

c 复制代码
GET /my_index/my_type/_search
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "1",
        "_score": 1,
        "_source": {
          "first_name": "John",
          "last_name": "Smith"
        }
      }
    ]
  }
}

一些要点:

  • 复制的是字段值,而不是 term(词条)(由分析过程产生)。
  • _source字段不会被修改来显示复制的值.。
  • 相同的值可以复制到多个字段,通过"copy_to": [ "field_1", "field_2" ] 来操作。
相关推荐
Elasticsearch3 小时前
需要知道某个同义词是否实际匹配了你的 Elasticsearch 查询吗?
elasticsearch
SelectDB20 小时前
易车 × Apache Doris:构建湖仓一体新架构,加速 AI 业务融合实践
大数据·agent·mcp
武子康1 天前
大数据-241 离线数仓 - 实战:电商核心交易数据模型与 MySQL 源表设计(订单/商品/品类/店铺/支付)
大数据·后端·mysql
IvanCodes1 天前
一、消息队列理论基础与Kafka架构价值解析
大数据·后端·kafka
武子康2 天前
大数据-240 离线数仓 - 广告业务 Hive ADS 实战:DataX 将 HDFS 分区表导出到 MySQL
大数据·后端·apache hive
洛森唛3 天前
ElasticSearch查询语句Query String详解:从入门到精通
后端·elasticsearch
字节跳动数据平台3 天前
5000 字技术向拆解 | 火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据
武子康3 天前
大数据-239 离线数仓 - 广告业务实战:Flume 导入日志到 HDFS,并完成 Hive ODS/DWD 分层加载
大数据·后端·apache hive
洛森唛4 天前
Elasticsearch DSL 查询语法大全:从入门到精通
后端·elasticsearch
字节跳动数据平台4 天前
代码量减少 70%、GPU 利用率达 95%:火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据