Elasticsearch根据业务创建映射mapping结构分析:keyword和text(一)

1.概述:

es数据库的mapping映射在创建时需要根据业务需求的不同进行创建。在创建时需要考虑字段的约束,例如字段名的定义、数据类型、是否参与检索(index是否为false)、是否需要分词用于创建倒排索引、使用的话需要使用什么分词器。
本文参照了网上教程和官网的查询

2.业务分析:

在mysql中存在hotel数据表,需要在es中进行对应mapping的创建
在这里插入图片描述

2.1mapping映射的创建

#创建酒店索引和映射
PUT /hotel
{
  "mappings": {
    "properties": {
      #id的类型为keyword类型是因为在es中id字段本身就是文本类型,并且不参与检索
      "id": { 
        "type": "keyword"
      },
      #酒店名称必然参与检索,因此type为text,分词类型设置为ik_max_word(最细粒度划分)
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      #酒店地址作为一个数据展示,且根据业务上的需求并不需要参数检索,因此设置index为false
      "address": {
        "type": "keyword",
        "index": false
      },
      #价格和评分作为普通的数值类型,类型为integer即可
      "price": {
        "type": "integer"
      },
      "score": {
        "type": "integer"
      },
      #品牌需要进行检索,同时品牌作为不可分割的整体,因此类型为keyword
      "brand": {
        "type": "keyword"
      },
      #同品牌
      "city": {
        "type": "keyword"
      },
       #同品牌
      "star_name": {
        "type": "keyword"
      },
       #同品牌
      "business": {
        "type": "keyword"
      },
      #经纬度作为地址位置,在es中使用特殊的geo_point类型用于表示经度和纬度坐标
      "location": {
        "type": "geo_point"
      },
      #酒店封面主要用于展示,并且如果参与检索没有意义,所以设置index为false
      "pic": {
        "type": "keyword",
        "index": false
      }
    }
  }
}

2.2mapping映射的介绍

关于文本类型的官方文档说明:

1.官方文档对于_mapping 的说明

Explicit mapping 显式映射
Explicit mapping allows you to precisely choose how to define the mapping definition, such as:
显式映射允许您精确地选择如何定义映射定义,例如:
Which string fields should be treated as full text fields.
应将哪些字符串字段视为全文字段
Which fields contain numbers, dates, or geolocations.
哪些字段包含数字、日期或地理位置
The format of date values. 日期值
Custom rules to control the mapping for dynamically added fields
控制映射的自定义规则动态添加字段.
总结:说白了就是在创建映射字段时需要根据需求对于字段是否需要全文检索和是否作为结构化的数据参与聚合等

2.官方文档对于keyword的说明

keyword, which is used for structured content such as IDs, email addresses, hostnames, status codes, zip codes, or tags.
constant_keyword for keyword fields that always contain the same value.
wildcard for unstructured machine-generated content. The wildcard type is optimized for fields with large values or high cardinality.
大致含义:就是keyword这是用于结构化内容,如 id,电子邮件地址,主机名,对于总是包含相同值的关键字字段。
Keyword fields are often used in sorting, aggregations, and term-level queries, such as term. Avoid using keyword fields for full-text search. Use the text field type instead.
关键字字段通常用于排序、聚合和术语级查询(如 term)。避免在全文搜索中使用关键字字段。改为使用文本字段类型。
总结:keyword类型主要用于结构化且总是包含相同值的文本内容,通常用于排序、聚合和术语级别的查询,并且避免参与全文检索

3.官方文档对于text的说明

(1) The text family includes the following field types 文本系列包含字段类型
text, the traditional field type for full-text content such as the body of an email or the description of a product.
text,全文内容的传统字段类型,例如电子邮件正文或产品描述。

match_only_text, a space-optimized variant of text that disables scoring and performs slower on queries that need positions. It is best suited for indexing log messages.
match_only_text,一种空间优化的文本变体,它禁用评分并在需要位置的查询上执行较慢。
它最适合索引日志消息。

(2) Text field type 文本字段类型
A field to index full-text values, such as the body of an email or the description of a product. These fields are analyzed, that is they are passed through an analyzer to convert the string into a list of individual terms before being indexed. The analysis process allows Elasticsearch to search for individual words within each full text field. Text fields are not used for sorting and seldom used for aggregations (although the significant text aggregation is a notable exception).
索引全文值的字段,如电子邮件正文或产品说明。对这些字段进行分析,即在进行索引之前通过分析器将字符串转换为单个术语的列表。分析过程允许 Elasticsearch 在每个全文字段中搜索单独的单词。文本字段不用于排序,也很少用于聚合(尽管重要的文本聚合是一个明显的例外)。

text fields are best suited for unstructured but human-readable content. If you need to index unstructured machine-generated content, see Mapping unstructured content.
文本字段最适合非结构化但可读的内容。如果需要索引非结构化机器生成的内容,请参见映射非结构化内容。

总结:text类型适用于文本的非结构化内容,例如产品说明等。在数据创建时会根据分词器生成倒排索引用于全文检索。而且text不参与排序和聚合

text与keyword总结

keyword主要用于结构化内容的字段,并且总是会有相同值的字段。因为通常需要用于聚合、排序和术语级查询(如 term),所以避免参与全文检索
text字段类型用于全文内容,例如电子邮件正文或产品说明,并且es会通过分析器对字符串进行分词,可以在全文检索中搜索单独的单词。文本字段最适合非结构化但可读的内容并且不用于排序,也很少用于聚合

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐州蔡徐坤

又要到饭了兄弟们

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值