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" ] 来操作。
相关推荐
诗旸的技术记录与分享7 小时前
Flink-1.19.0源码详解-番外补充3-StreamGraph图
大数据·flink
资讯分享周7 小时前
Alpha系统联结大数据、GPT两大功能,助力律所管理降本增效
大数据·gpt
G皮T9 小时前
【Elasticsearch】深度分页及其替代方案
大数据·elasticsearch·搜索引擎·scroll·检索·深度分页·search_after
TDengine (老段)9 小时前
TDengine STMT2 API 使用指南
java·大数据·物联网·时序数据库·iot·tdengine·涛思数据
用户Taobaoapi201411 小时前
母婴用品社媒种草效果量化:淘宝详情API+私域转化追踪案例
大数据·数据挖掘·数据分析
G皮T11 小时前
【Elasticsearch】检索排序 & 分页
大数据·elasticsearch·搜索引擎·排序·分页·检索·深度分页
飞询14 小时前
Docker 安装 Elasticsearch 9
elasticsearch·docker
小新学习屋15 小时前
Spark从入门到熟悉(篇三)
大数据·分布式·spark
rui锐rui15 小时前
大数据学习2:HIve
大数据·hive·学习
G皮T15 小时前
【Elasticsearch】检索高亮
大数据·elasticsearch·搜索引擎·全文检索·kibana·检索·高亮