Elasticsearch索引mapping的写入、查看与修改

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: mapping的写入与查看首先创建一个索引:curl -XPOST "http://127.0.0.1:9200/productindex"{"acknowledged":true} 现在只创建了一个索引...

mapping的写入与查看

首先创建一个索引:

curl -XPOST "http://127.0.0.1:9200/productindex"
{"acknowledged":true}  

现在只创建了一个索引,并没有设置mapping,查看一下索引mapping的内容:

curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty" 
{
  "productindex" : {
    "mappings" : { }
  }
}

可以看到mapping为空,我们只创建了一个索引,并没有进行mapping配置,mapping自然为空。
下面给productindex这个索引加一个type,type name为product,并设置mapping:

curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d ' 
{
    "product": {
            "properties": {
                "title": {
                    "type": "string",
                    "store": "yes"
                },
                "description": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "price": {
                    "type": "double"
                },
                "onSale": {
                    "type": "boolean"
                },
                "type": {
                    "type": "integer"
                },
                "createDate": {
                    "type": "date"
                }
            }
        }
  }
'

{
  "acknowledged" : true
}

上面的操作中,我们给productindex加了一个type,并写入了product的mapping信息,再次查看:

curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty"
{
  "productindex" : {
    "mappings" : {
      "product" : {
        "properties" : {
          "createDate" : {
            "type" : "date",
            "format" : "strict_date_optional_time||epoch_millis"
          },
          "description" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "onSale" : {
            "type" : "boolean"
          },
          "price" : {
            "type" : "double"
          },
          "title" : {
            "type" : "string",
            "store" : true
          },
          "type" : {
            "type" : "integer"
          }
        }
      }
    }
  }
}

修改mapping

如果想给product新增一个字段,那么需要修改mapping,尝试一下:

curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '{
     "product": {
                "properties": {
                     "amount":{
                        "type":"integer"
                   }
                }
            }
    }'
{
  "acknowledged" : true
}

新增成功。
如果要修改一个字段的类型呢,比如onSale字段的类型为boolean,现在想要修改为string类型,尝试一下:

 curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '{
     "product": {
                "properties": {
                 "onSale":{
                    "type":"string" 
               }
            }
        }
}'

返回错误:

{
  "error" : {
    "root_cause" : [ {
      "type" : "illegal_argument_exception",
      "reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [string]"
    } ],
    "type" : "illegal_argument_exception",
    "reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [string]"
  },
  "status" : 400
}

为什么不能修改一个字段的type?原因是一个字段的类型修改以后,那么该字段的所有数据都需要重新索引。Elasticsearch底层使用的是lucene库,字段类型修改以后索引和搜索要涉及分词方式等操作,不允许修改类型在我看来是符合lucene机制的。
这里有一篇关于修改mapping字段的博客,叙述的比较清楚:Elasticsearch 的坑爹事——记录一次mapping field修改过程,可以参考.

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
目录
相关文章
|
11月前
|
自然语言处理 大数据 应用服务中间件
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
211 5
|
11月前
|
存储 分布式计算 大数据
大数据-169 Elasticsearch 索引使用 与 架构概念 增删改查
大数据-169 Elasticsearch 索引使用 与 架构概念 增删改查
163 3
|
存储 API 数据库
检索服务elasticsearch索引(Index)
【8月更文挑战第23天】
545 6
|
10月前
|
存储 缓存 监控
优化Elasticsearch 索引设计
优化Elasticsearch 索引设计
174 5
|
10月前
|
存储 JSON 关系型数据库
Elasticsearch 索引
【11月更文挑战第3天】
196 4
|
10月前
|
测试技术 API 开发工具
ElasticSearch7.6.x 模板及滚动索引创建及注意事项
ElasticSearch7.6.x 模板及滚动索引创建及注意事项
162 8
|
JSON 自然语言处理 数据库
ElasticSearch基础1——索引和文档。Kibana,RestClient操作索引和文档+黑马旅游ES库导入
概念、ik分词器、倒排索引、索引和文档的增删改查、RestClient对索引和文档的增删改查
ElasticSearch基础1——索引和文档。Kibana,RestClient操作索引和文档+黑马旅游ES库导入
|
存储 搜索推荐 数据建模
Elasticsearch 的数据建模与索引设计
【9月更文第3天】Elasticsearch 是一个基于 Lucene 的搜索引擎,广泛应用于全文检索、数据分析等领域。为了确保 Elasticsearch 的高效运行,合理的数据建模和索引设计至关重要。本文将探讨如何为不同的应用场景设计高效的索引结构,并分享一些数据建模的最佳实践。
449 2
|
JSON 自然语言处理 数据库
Elasticsearch从入门到项目部署 安装 分词器 索引库操作
这篇文章详细介绍了Elasticsearch的基本概念、倒排索引原理、安装部署、IK分词器的使用,以及如何在Elasticsearch中进行索引库的CRUD操作,旨在帮助读者从入门到项目部署全面掌握Elasticsearch的使用。
|
自然语言处理 Java 索引
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
128 0