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" ] 来操作。
相关推荐
狮歌~资深攻城狮6 小时前
HBase性能优化秘籍:让数据处理飞起来
大数据·hbase
Elastic 中国社区官方博客6 小时前
Elasticsearch Open Inference API 增加了对 Jina AI 嵌入和 Rerank 模型的支持
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索·jina
隔壁老王1567 小时前
mysql实时同步到es
数据库·mysql·elasticsearch
workflower7 小时前
Prompt Engineering的重要性
大数据·人工智能·设计模式·prompt·软件工程·需求分析·ai编程
SunnyRivers8 小时前
关于ES中text类型时间字段范围查询的结构化解决方案
elasticsearch·时间·text·范围查询
API_technology9 小时前
电商搜索API的Elasticsearch优化策略
大数据·elasticsearch·搜索引擎
黄雪超9 小时前
大数据SQL调优专题——引擎优化
大数据·数据库·sql
The god of big data9 小时前
MapReduce 第二部:深入分析与实践
大数据·mapreduce
G***技10 小时前
杰和科技GAM-AI视觉识别管理系统,让AI走进零售营销
大数据·人工智能·系统架构
天天爱吃肉821811 小时前
碳化硅(SiC)功率器件:新能源汽车的“心脏”革命与技术突围
大数据·人工智能