当前位置: 首页 > news >正文

北京网站建设百度排名今日疫情最新情况

北京网站建设百度排名,今日疫情最新情况,杭州响应式网站,app开发公司 杭州在使用Elasticsearch作为搜索引擎时,经常会遇到一些映射(Mapping)相关的问题。本文将深入分析字段映射冲突问题,并通过原生的Elasticsearch API请求来复现和解决这个问题。 问题描述 在实际项目中,我们遇到以下错误: Transport…

在使用Elasticsearch作为搜索引擎时,经常会遇到一些映射(Mapping)相关的问题。本文将深入分析字段映射冲突问题,并通过原生的Elasticsearch API请求来复现和解决这个问题。

问题描述

在实际项目中,我们遇到以下错误:

TransportError(400, 'illegal_argument_exception', 'mapper [match_score] cannot be changed from type [integer] to [double]')

类似的:

TransportError(400, 'illegal_argument_exception', 'mapper [score] cannot be changed from type [double] to [integer]')

这两个错误都指向同一个问题:尝试将一个已存在的字段类型从一种数据类型改为另一种数据类型,这在Elasticsearch中是不允许的。

不同文档类型中的同名字段问题

这是一个非常常见但容易被忽视的问题:即使在不同的文档类型(doc type)中定义同名字段,Elasticsearch也会要求它们具有相同的类型定义。很多开发者误以为不同文档类型(doc type)之间的字段是相互独立的,就像关系数据库中不同表的同名字段可以有不同的数据类型一样,但Elasticsearch并非如此。

例如,假设我们有两个文档类型:type1type2,它们都定义了一个名为score的字段,但在type1中它是integer类型,而在type2中它是double类型。当这两个类型的文档被索引到同一个Elasticsearch索引中时,就会发生冲突。

让我们通过一个简单示例来验证这个问题:

# 创建一个索引,定义type1类型,包含integer类型的score字段
curl -X PUT "http://localhost:9200/conflict_demo" -H "Content-Type: application/json" -d'
{"mappings": {"type1": {"properties": {"score": {"type": "integer"}}}}
}'# 在相同索引中添加type2类型,尝试使用double类型的score字段
curl -X PUT "http://localhost:9200/conflict_demo/_mapping/type2" -H "Content-Type: application/json" -d'
{"properties": {"score": {"type": "double"}}
}'

第二个命令将会失败,并显示以下错误:

{"error": {"root_cause": [{"type": "illegal_argument_exception","reason": "mapper [score] cannot be changed from type [integer] to [double]"}],"type": "illegal_argument_exception","reason": "mapper [score] cannot be changed from type [integer] to [double]"},"status": 400
}

这是因为在Elasticsearch中,一个索引的映射是扁平的。虽然文档可以存储在不同的类型下,但同名字段在内部被视为同一个字段。这是Elasticsearch的设计决策,目的是为了优化存储和搜索效率。

注意:从Elasticsearch 6.0开始,每个索引只允许有一个映射类型,而在Elasticsearch 7.0中,映射类型被完全移除。这一变化进一步强调了Elasticsearch的字段是全局的,而不是按文档类型隔离的设计思想。

环境准备

本文使用Elasticsearch 5.6版本进行验证,您可以通过以下命令检查您的ES版本:

curl -X GET "http://localhost:9200/"

如果一切正常,您将看到类似以下的输出:

{"name" : "CWlnNkA","cluster_name" : "elasticsearch","cluster_uuid" : "vPFGQy83SDaz5cPa_OiX1A","version" : {"number" : "5.6.15","build_hash" : "fe7575a","build_date" : "2019-02-13T16:21:45.880Z","build_snapshot" : false,"lucene_version" : "6.6.1"},"tagline" : "You Know, for Search"
}

问题复现

步骤1:创建带有Integer类型match_score字段的索引

首先,我们创建一个索引,并定义一个类型为Integer的match_score字段:

curl -X PUT "http://localhost:9200/test_index" -H "Content-Type: application/json" -d'
{"mappings": {"doc": {"properties": {"match_score": {"type": "integer"},"title": {"type": "text"},"content": {"type": "text"}}}}
}'

查看索引映射:

curl -X GET "http://localhost:9200/test_index/_mapping"

输出应该类似:

{"test_index": {"mappings": {"doc": {"properties": {"content": {"type": "text"},"match_score": {"type": "integer"},"title": {"type": "text"}}}}}
}

步骤2:添加一些测试数据

curl -X POST "http://localhost:9200/test_index/doc/1" -H "Content-Type: application/json" -d'
{"match_score": 10,"title": "Document 1","content": "This is the first document with integer match_score"
}'

步骤3:尝试修改字段类型(复现错误)

现在,我们尝试将match_score字段的类型从integer更改为double:

curl -X PUT "http://localhost:9200/test_index/_mapping/doc" -H "Content-Type: application/json" -d'
{"properties": {"match_score": {"type": "double"}}
}'

这将导致以下错误:

{"error": {"root_cause": [{"type": "illegal_argument_exception","reason": "mapper [match_score] cannot be changed from type [integer] to [double]"}],"type": "illegal_argument_exception","reason": "mapper [match_score] cannot be changed from type [integer] to [double]"},"status": 400
}

这正是我们在实际项目中遇到的错误。

步骤4:在相同索引中添加另一个文档类型(复现多类型冲突)

为了更清楚地展示不同文档类型中同名字段的冲突,我们尝试在同一个索引中添加另一个文档类型:

curl -X PUT "http://localhost:9200/test_index/_mapping/another_doc" -H "Content-Type: application/json" -d'
{"properties": {"match_score": {"type": "double"},"description": {"type": "text"}}
}'

这个命令也会失败,显示与之前相同的错误,因为match_score字段已经在索引中定义为integer类型,不能在另一个文档类型中将其定义为double类型。

问题根本原因

Elasticsearch不允许对现有字段的类型进行更改,因为这会导致已经索引的数据无法正确解析。这是Elasticsearch的基本设计原则之一。

具体来说,当多个文档类型共享同一个索引时,有三种情况会导致字段映射冲突:

  1. 同名字段使用了不同的数据类型(如我们示例中的integer vs double)
  2. 同名字段使用了不兼容的分析器或索引选项
  3. 一个是父字段,一个是子字段的冲突

重要说明:不同文档类型中的同名字段必须具有完全相同的映射定义。这一限制在实际开发中尤其需要注意,因为它经常导致意想不到的映射冲突,特别是在大型项目中,不同团队可能负责不同的文档类型。

解决方案

方案1:使用别名字段(推荐)

最简单且最灵活的解决方案是为冲突的字段使用不同的名称:

# 首先创建一个新索引,包含两个不同名称的字段
curl -X PUT "http://localhost:9200/test_index_new" -H "Content-Type: application/json" -d'
{"mappings": {"doc": {"properties": {"integer_match_score": {"type": "integer"},"double_match_score": {"type": "double"},"title": {"type": "text"},"content": {"type": "text"}}}}
}'

这种方法的优点是,每个字段都可以使用最适合其数据的类型。

对于多文档类型场景,我们可以为每个类型创建特定的字段名:

curl -X PUT "http://localhost:9200/multi_type_index" -H "Content-Type: application/json" -d'
{"mappings": {"type1": {"properties": {"type1_score": {"type": "integer"}}},"type2": {"properties": {"type2_score": {"type": "double"}}}}
}'

方案2:使用通用类型(如keyword或text)

如果必须使用相同的字段名,可以选择一个通用的更宽泛的类型:

curl -X PUT "http://localhost:9200/test_index_common" -H "Content-Type: application/json" -d'
{"mappings": {"doc": {"properties": {"match_score": {"type": "keyword"},"title": {"type": "text"},"content": {"type": "text"}}}}
}'

但这可能会影响搜索和聚合操作的性能。

方案3:重建索引

如果您必须更改字段类型,唯一的方法是创建一个新索引,然后重新索引数据:

# 步骤1:创建新索引
curl -X PUT "http://localhost:9200/test_index_v2" -H "Content-Type: application/json" -d'
{"mappings": {"doc": {"properties": {"match_score": {"type": "double"},"title": {"type": "text"},"content": {"type": "text"}}}}
}'# 步骤2:使用reindex API重新索引数据
curl -X POST "http://localhost:9200/_reindex" -H "Content-Type: application/json" -d'
{"source": {"index": "test_index"},"dest": {"index": "test_index_v2"},"script": {"source": "ctx._source.match_score = (double)ctx._source.match_score"}
}'# 步骤3:删除旧索引
curl -X DELETE "http://localhost:9200/test_index"# 步骤4:创建别名(可选,便于无缝切换)
curl -X POST "http://localhost:9200/_aliases" -H "Content-Type: application/json" -d'
{"actions": [{"add": {"index": "test_index_v2","alias": "test_index_alias"}}]
}'

方案4:使用不同的索引

对于完全不相关的数据,最好使用不同的索引:

# 创建第一个索引,包含integer类型的match_score
curl -X PUT "http://localhost:9200/index_type1" -H "Content-Type: application/json" -d'
{"mappings": {"doc": {"properties": {"match_score": {"type": "integer"}}}}
}'# 创建第二个索引,包含double类型的match_score
curl -X PUT "http://localhost:9200/index_type2" -H "Content-Type: application/json" -d'
{"mappings": {"doc": {"properties": {"match_score": {"type": "double"}}}}
}'

使用多索引查询:

curl -X GET "http://localhost:9200/index_type1,index_type2/_search" -H "Content-Type: application/json" -d'
{"query": {"match_all": {}}
}'

验证解决方案

让我们验证方案1(使用别名字段):

# 添加数据到新索引
curl -X POST "http://localhost:9200/test_index_new/doc/1" -H "Content-Type: application/json" -d'
{"integer_match_score": 10,"title": "Document with integer score","content": "This document uses an integer score"
}'curl -X POST "http://localhost:9200/test_index_new/doc/2" -H "Content-Type: application/json" -d'
{"double_match_score": 9.5,"title": "Document with double score","content": "This document uses a double score"
}'# 查询两种类型
curl -X GET "http://localhost:9200/test_index_new/_search" -H "Content-Type: application/json" -d'
{"query": {"bool": {"should": [{ "range": { "integer_match_score": { "gte": 5 } } },{ "range": { "double_match_score": { "gte": 5.0 } } }]}}
}'

最佳实践

  1. 预先规划映射:在开始索引数据之前,仔细规划字段类型和名称。

  2. 字段命名约定:为字段名添加类型前缀或文档类型前缀,例如int_scoredbl_scoretype1_scoretype2_score

  3. 文档模型设计:认真设计文档模型,避免不必要的类型嵌套和复杂关系,减少冲突可能性。

  4. 使用动态映射模板:为不同类型的字段定义模板:

curl -X PUT "http://localhost:9200/template_index" -H "Content-Type: application/json" -d'
{"mappings": {"doc": {"dynamic_templates": [{"integers": {"match_pattern": "regex","match": "^int_.*","mapping": {"type": "integer"}}},{"doubles": {"match_pattern": "regex","match": "^dbl_.*","mapping": {"type": "double"}}}]}}
}'
  1. 索引版本控制:使用时间戳或版本号,方便迁移:
my_index_v1, my_index_v2, my_index_202305
  1. 使用索引别名:为应用程序使用的索引创建别名,便于无缝切换:
curl -X POST "http://localhost:9200/_aliases" -H "Content-Type: application/json" -d'
{"actions": [{"add": {"index": "my_index_v2","alias": "my_index"}}]
}'
  1. 定期检查映射冲突:定期检查Elasticsearch日志中的映射错误,及早发现问题。

总结

Elasticsearch的字段映射冲突是一个常见的问题,特别是在多文档类型场景下,同名字段必须使用相同的数据类型。这一限制源于Elasticsearch的内部设计,旨在优化存储和查询效率。解决方案包括使用不同的字段名、选择通用数据类型、重建索引或使用多个索引。通过遵循最佳实践,可以避免这些问题并构建更加稳健的Elasticsearch应用程序。

通过本文的示例,您可以直接使用curl命令复现和测试这些解决方案,帮助您更好地理解和解决Elasticsearch映射冲突问题。

http://www.cadmedia.cn/news/3992.html

相关文章:

  • 英语教育网站建设线上it培训机构
  • 佛山格尔做网站的公司宣传推广计划怎么写
  • 专业信息网站建设方案推广普通话的文字内容
  • 工程行业招聘网站整站优化系统
  • 做网站建设需要会哪些重庆网站seo多少钱
  • 深圳住房与建设网站制作公司官网多少钱
  • 网站做业务赚钱百度指数是怎么计算的
  • 城乡与建设厅网站首页友情链接免费发布平台
  • 呼和浩特做网站深圳关键词优化
  • 网站建设那里流量主广告点击自助平台
  • 怎样做自己公司的网站外链发布平台
  • 网站服务器如何做热备价凡科建站收费价目表
  • 免费b站推广网站2021软件开发培训机构排名
  • 嘉定网站设计制作优化排名2022年新闻热点摘抄
  • 青岛气象站建站时间网站推广如何做
  • 西安哪家公司网站做的好成人零基础学电脑培训班
  • 复刻手表网站电商营销策划方案范文
  • 一个网站怎么绑定很多个域名网站seo置顶 乐云践新专家
  • 湘潭学校网站建设 磐石网络合肥seo排名扣费
  • 出口商出口外贸流程网站seo策划方案
  • 建设一个网站需要多长时间seo优化公司
  • 电子商务html网站模板软文广告素材
  • nodejs做网站能保护源代码吗google高级搜索
  • ps做网站大小靠谱的seo收费
  • 办公室平面设计图网站优化的主要内容
  • seovip培训北京朝阳区优化
  • 网页设计站友情链接样式
  • 深圳市住房和建设局网站变更海外自媒体推广
  • 重庆网站建设网页广告调词平台多少钱
  • 阳江网站涟源网站seo