ElasticSearch映射分词

news2025/7/14 8:35:14

目录

弃用Type

why

映射

查询 mapping of index

创建 index with mapping

添加 field with mapping

数据迁移

1.新建 一个 index with correct mapping 

2.数据迁移 reindex data into that index

分词

POST _analyze

自定义词库

ik分词器

circuit_breaking_exception


弃用Type

ES 6.x 之前,Type 开始弃用

ES 7.x ,被弱化,仍支持

ES 8.x ,完全移除

弃用后,每个索引只包含一种文档类型

如果需要区分不同类型的文档,俩种方式:

  • 创建不同的索引
  • 在文档中添加自定义字段来实现。

why

Elasticsearch 的底层存储(Lucene)是基于索引的,而不是基于 Type 的。

在同一个索引中,不同 Type 的文档可能具有相同名称但不同类型的字段,这种字段类型冲突会导致数据不一致和查询错误。

GET /bank/_search
{
  "query": {
    "match": {
      "address": "mill lane"
    }
  },
  "_source": ["account_number","address"]
}

从查询语句可以看出,查询是基于index的,不会去指定type。如果有不同type的address,就会引起查询冲突。


映射

Mapping 定义 doc和field 如何被存储和被检索

Mapping(映射) 是 Elasticsearch 中用于定义文档结构和字段类型的机制。它类似于关系型数据库中的表结构(Schema),用于描述文档中包含哪些字段、字段的数据类型(如文本、数值、日期等),以及字段的其他属性(如是否分词、是否索引等)。

Mapping 是 Elasticsearch 的核心概念之一,它决定了数据如何被存储、索引和查询。

查询 mapping of index

 _mapping

GET /bank/_mapping
{
  "bank" : {
    "mappings" : {
      "properties" : {
        "account_number" : {
          "type" : "long"
        },
        "address" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "age" : {
          "type" : "long"
        },
        "balance" : {
          "type" : "long"
        },
        "city" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "email" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "employer" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "firstname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "gender" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "lastname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "state" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
  • text 可以添加子field ---keyword,类型是 keyword。keyword存储精确值

创建 index with mapping

Put /{indexName}

Put /my_index
{
  "mappings": {
    "properties": {
      "account_number": {
        "type": "long"
      },
      "address": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "city": {
        "type": "keyword"
      }
    }
  }
}

添加 field with mapping

  •  PUT /{indexName}/_mapping + mapping.properties请求体
PUT /my_index/_mapping
{
  "properties": {
    "state": {
      "type": "keyword",
      "index": false
    }
  }
}
  •  "index": false  该字段无法被索引,不会参与检索   默认true

数据迁移

 ES不支持修改已存在的mapping。若想更新已存在的mapping,就要进行数据迁移。

1.新建 一个 index with correct mapping 

PUT /my_bank
{
  "mappings": {
    "properties": {
      "account_number": {
        "type": "long"
      },
      "address": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "age": {
        "type": "integer"
      },
      "balance": {
        "type": "long"
      },
      "city": {
        "type": "keyword"
      },
      "email": {
        "type": "keyword"
      },
      "employer": {
        "type": "keyword"
      },
      "firstname": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "gender": {
        "type": "keyword"
      },
      "lastname": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "state": {
        "type": "keyword"
      }
    }
  }
}

2.数据迁移 reindex data into that index

POST _reindex
{
  "source": {
    "index": "bank",
    "type": "account"
  },
  "dest": {
    "index": "my_bank"
  }
}
  • ES 8.0  弃用type参数 


分词

        将文本拆分为单个词项(tokens)

POST _analyze

标准分词器

POST _analyze
{
  "analyzer": "standard",
  "text": ["it's test data","hello world"]
}

 Response

{
  "tokens" : [
    {
      "token" : "it's",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "test",
      "start_offset" : 5,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "data",
      "start_offset" : 10,
      "end_offset" : 14,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "hello",
      "start_offset" : 15,
      "end_offset" : 20,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "world",
      "start_offset" : 21,
      "end_offset" : 26,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

自定义词库

nginx/html目录下 创建es/term.text,添加词条

配置ik远程词库,/elasticsearch/config/analysis-ik/IKAnalyzer.cfg.xml

 测试

POST _analyze
{
  "analyzer": "ik_smart",
  "text": "尚硅谷项目谷粒商城"
}

 [尚硅谷,谷粒商城]为term.text词库中的词条

 Response

{
  "tokens" : [
    {
      "token" : "尚硅谷",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "项目",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "谷粒商城",
      "start_offset" : 5,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}

ik分词器

        中文分词

github地址

https://github.com/infinilabs/analysis-ik

    下载地址

    bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/7.4.2

    进入docker容器ES 下载 ik 插件

    卸载插件

    elasticsearch-plugin remove analysis-ik

    测试

    POST _analyze
    {
      "analyzer": "ik_smart",
      "text": "我要成为java高手"
    }

    Response 

    {
      "tokens" : [
        {
          "token" : "我",
          "start_offset" : 0,
          "end_offset" : 1,
          "type" : "CN_CHAR",
          "position" : 0
        },
        {
          "token" : "要",
          "start_offset" : 1,
          "end_offset" : 2,
          "type" : "CN_CHAR",
          "position" : 1
        },
        {
          "token" : "成为",
          "start_offset" : 2,
          "end_offset" : 4,
          "type" : "CN_WORD",
          "position" : 2
        },
        {
          "token" : "java",
          "start_offset" : 4,
          "end_offset" : 8,
          "type" : "ENGLISH",
          "position" : 3
        },
        {
          "token" : "高手",
          "start_offset" : 8,
          "end_offset" : 10,
          "type" : "CN_WORD",
          "position" : 4
        }
      ]
    }

    circuit_breaking_exception

    熔断器机制被触发

    {
        "error": {
            "root_cause": [
                {
                    "type": "circuit_breaking_exception",
                    "reason": "[parent] Data too large, data for [<http_request>] would be [124604192/118.8mb], which is larger than the limit of [123273216/117.5mb], real usage: [124604192/118.8mb], new bytes reserved: [0/0b], usages [request=0/0b, fielddata=1788/1.7kb, in_flight_requests=0/0b, accounting=225547/220.2kb]",
                    "bytes_wanted": 124604192,
                    "bytes_limit": 123273216,
                    "durability": "PERMANENT"
                }
            ],
            "type": "circuit_breaking_exception",
            "reason": "[parent] Data too large, data for [<http_request>] would be [124604192/118.8mb], which is larger than the limit of [123273216/117.5mb], real usage: [124604192/118.8mb], new bytes reserved: [0/0b], usages [request=0/0b, fielddata=1788/1.7kb, in_flight_requests=0/0b, accounting=225547/220.2kb]",
            "bytes_wanted": 124604192,
            "bytes_limit": 123273216,
            "durability": "PERMANENT"
        },
        "status": 429
    }

    查看ES日志

    docker logs elasticsearch

    检查 Elasticsearch 的内存使用情况

    GET /_cat/nodes?v&h=name,heap.percent,ram.percent
    • 如果 heap.percent 或 ram.percent 接近 100%,说明内存不足。

     增加 Elasticsearch 堆内存

    删除并重新创建容器 调整 -Xms 和 -Xmx 参数 256m

    docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
    > -e "discovery.type=single-node" \
    > -e ES_JAVA_OPTS="-Xms64m -Xmx256m" \
    > -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
    > -v  /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
    > -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
    > -d elasticsearch:7.4.2

    本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2301613.html

    如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

    相关文章

    Es的text和keyword类型以及如何修改类型

    昨天同事触发定时任务发现es相关服务报了一个序列化问题&#xff0c; 今天早上捕获异常将异常堆栈全部打出来看&#xff0c;才发现是聚合的字段不是keyword类型的问题。 到kibbna命令行执行也是一样的错误 使用 /_mapping查看索引的字段类型&#xff0c;才发现userUniqueid是te…

    1-18 GIT设置公钥

    1-1 GIT如何设置公钥 1.0 注册账号 这个应该都是会的&#xff0c;就不做介绍了 2.0 设置公钥 PWD的作用是查看文件的路径 ssh-keygen -t ed25519 -C "Gitee SSH Key" 读取公钥文件&#xff1a; cat ~/.ssh/id_ed25519.pub 3.0 测试 查看绑定的用户名和邮箱&#xff1…

    Pytorch深度学习教程_3_初识pytorch

    欢迎来到《PyTorch深度学习教程》系列的第三篇&#xff01;在前面的两篇中&#xff0c;我们已经介绍了Python及numpy的基本使用。今天&#xff0c;我们将深入探索PyTorch的核心功能&#xff0c;帮助你更好地理解和使用这个强大的深度学习框架。 欢迎订阅专栏&#xff1a; 深度…

    基于Flask的艺恩影片票房分析系统的设计与实现

    【Flask】基于Flask的艺恩影片票房分析系统的设计与实现&#xff08;完整系统源码开发笔记详细部署教程&#xff09;✅ 目录 一、项目简介二、项目界面展示三、项目视频展示 一、项目简介 该系统利用Python编程语言进行后端开发&#xff0c;结合Echarts进行数据的可视化展示&a…

    ollama-chat-ui-vue,一个可以用vue对接ollama的开源项目,可接入deepSeek

    ollama-chat-ui-vue 使用vue3 vite elementUi 搭建的前端chat,通过ollama可与模型对话,目前支持独立思考,切换模型(联网查询后续支持) github地址&#xff1a;ollama-chat-ui-vue 制作不易github点点star&#xff0c;谢谢 前置工作 安装ollama,ollama官网地址 安装完olla…

    TCP开发

    TCP客户端编程开发 任何的网络编程套接字开发的两种工作模式&#xff1a;TCP网络、UDP网络。 TCP和UDP的介绍 TCP&#xff1a;连接式网络通信&#xff0c;长连接通信或流式通信。TCP的通信一般稳定、可靠&#xff0c;但传输速度往往没有UDP快。其中有这样一个概念----心跳时…

    Java 基于SpringBoot+Vue 的旅游网站信息化管理系统设计与实现

    Java 基于SpringBootVue 的旅游网站信息化管理系统设计与实现 博主介绍&#xff1a;✌程序员徐师兄、8年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战*✌ &#x1f345;文末获取源码联系&#x1f34…

    Ubuntu:20.04更新cmake到更高版本

    从输出信息可以看出&#xff0c;您当前的系统中已经安装了 cmake&#xff0c;但版本是 3.16.3&#xff0c;而您的项目需要 CMake 3.18 或更高版本。默认情况下&#xff0c;Ubuntu 20.04 的官方软件仓库中提供的 CMake 版本较低&#xff08;如 3.16.3&#xff09;&#xff0c;因…

    php 系统命令执行及绕过

    文章目录 php的基础概念php的基础语法1. PHP 基本语法结构2. PHP 变量3.输出数据4.数组5.超全局变量6.文件操作 php的命令执行可以执行命令的函数命令执行绕过利用代码中命令&#xff08;如ls&#xff09;执行命令替换过滤过滤特定字符串神技&#xff1a;利用base64编码解码的绕…

    论文笔记-WSDM2024-LLMRec

    论文笔记-WSDM2024-LLMRec: Large Language Models with Graph Augmentation for Recommendation LLMRec: 基于图增强的大模型推荐摘要1.引言2.前言2.1使用图嵌入推荐2.2使用辅助信息推荐2.3使用数据增强推荐 3.方法3.1LLM作为隐式反馈增强器3.2基于LLM的辅助信息增强3.2.1用户…

    计算四个锚点TOA定位中GDOP的详细步骤和MATLAB例程

    该MATLAB代码演示了在三维空间中,使用四个锚点的TOA(到达时间)定位技术计算几何精度衰减因子(GDOP)的过程。如需帮助,或有导航、定位滤波相关的代码定制需求,请联系作者 文章目录 DOP计算原理MATLAB例程运行结果示例关键点说明扩展方向另有文章: 多锚点Wi-Fi定位和基站…

    Lookup Join显著增强,Paimon1.0版本如何做的?

    Hi&#xff0c;大家好&#xff0c;我们又满血复活了。 2025年开年更新频率不快&#xff0c;一方面是大模型如火如荼&#xff0c;也一直在补相关知识&#xff1b;另外一方面&#xff0c;新的一年里身体健康被我摆到了第一位&#xff0c;不会像前几年那么卷了。 后续我们的更新会…

    Vue前端开发-Vant组件之Button组件

    Vant 有丰富的UI组件&#xff0c;而基础组件是全部组件的核心&#xff0c;基础组件中将常用的元素做了二次的开发&#xff0c;封装成Vant格式组件&#xff0c;如按钮、图片和布局等&#xff0c;这些封装后的Vant组件&#xff0c;提供了更多面向实际应用的属性和事件&#xff0c…

    DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方DeepSeek接入)

    前言 在当今数字化时代&#xff0c;AI编程助手已成为提升开发效率的利器。DeepSeek作为一款强大的AI模型&#xff0c;凭借其出色的性能和开源免费的优势&#xff0c;成为许多开发者的首选。今天&#xff0c;就让我们一起探索如何将DeepSeek接入PyCharm&#xff0c;实现高效、智…

    【Linux网络编程】应用层协议HTTP(请求方法,状态码,重定向,cookie,session)

    &#x1f381;个人主页&#xff1a;我们的五年 &#x1f50d;系列专栏&#xff1a;Linux网络编程 &#x1f337;追光的人&#xff0c;终会万丈光芒 &#x1f389;欢迎大家点赞&#x1f44d;评论&#x1f4dd;收藏⭐文章 ​ Linux网络编程笔记&#xff1a; https://blog.cs…

    健康养生:从生活细节开启活力之旅

    在忙碌的现代生活里&#xff0c;健康养生不再是一个抽象概念&#xff0c;而是关乎生活质量的关键。其实&#xff0c;只要掌握日常养生要点&#xff0c;就能轻松开启活力满满的健康生活。 饮食是健康的基石。每日饮食需遵循 “彩虹原则”&#xff0c;摄入多种颜色食物。早餐时&…

    DeepSeek + Mermaid编辑器——常规绘图

    下面这张图出自&#xff1a;由清华大学出品的 《DeepSeek&#xff1a;从入门到精通》。 作为纯文本生成模型&#xff0c;DeepSeek虽不具备多媒体内容生成接口&#xff0c;但其开放式架构允许通过API接口与图像合成引擎、数据可视化工具等第三方系统进行协同工作&#xff0c;最终…

    【拥抱AI】GPT Researcher的诞生

    一、GPT Researcher 研究过程总结 GPT Researcher 是一个开源的自主智能体&#xff0c;旨在通过利用人工智能技术实现高效、全面且客观的在线研究。它通过一系列创新的设计和优化&#xff0c;解决了传统研究工具&#xff08;如 AutoGPT&#xff09;中存在的问题&#xff0c;如…

    Redis7——基础篇(三)

    前言&#xff1a;此篇文章系本人学习过程中记录下来的笔记&#xff0c;里面难免会有不少欠缺的地方&#xff0c;诚心期待大家多多给予指教。 基础篇&#xff1a; Redis&#xff08;一&#xff09;Redis&#xff08;二&#xff09; 接上期内容&#xff1a;上期完成了Redis的基本…

    MySQL登录问题总结

    不管何种数据库&#xff0c;使用的第一步都是先登录。 MySQL命令行登录语句&#xff1a;mysql -u username -P port -p -D database_name 登录MySQL的报错一般从报错信息都能得到反馈&#xff0c;常见报错原因分析如下&#xff0c;实例中的以test用户为例&#xff0c;登录环境为…