【发布时间】:2020-10-06 14:43:20
【问题描述】:
我有两个来自同一索引的文档,最初看起来像这样(此处仅显示 _source 值)
{
"id" : "3",
"name": "Foo",
"property":{
"schemaId":"guid_of_the_RGB_schema_defined_extenally",
"value":{
"R":255,
"G":100,
"B":20
}
}
}
{
"id" : "2",
"name": "Bar",
"property":{
"schemaId":"guid_of_the_HSL_schema_defined_extenally",
"value":{
"H":255,
"S":100,
"L":20
}
}
}
模式(用于验证value)存储在 ES 外部,因为它与索引无关。
如果我不定义映射,value 字段将被视为Object 映射。一旦有新的子字段,它的子字段就会增长。
目前,ElasticSearch 支持 Flattened mapping https://www.elastic.co/guide/en/elasticsearch/reference/current/flattened.html 以防止索引中的这种爆炸。但是由于其限制,它对搜索内部字段的支持有限:As with queries, there is no special support for numerics — all values in the JSON object are treated as keywords. When sorting, this implies that values are compared lexicographically.
我需要能够查询索引以找到与给定文档匹配的文档(例如 [10,30] 范围内的 B)
到目前为止,我想出了一个像这样构建我的文档的解决方案
{
"id":4,
"name":"Boo",
"property":
{
"guid_of_the_normalized_RGB_schema_defined_extenally":
{
"R":0.1,
"G":0.2,
"B":0.5
}
}
虽然它没有解决我的映射爆炸问题,但它缓解了其他一些问题。
我的映射现在看起来与字段 property 类似
"property": {
"properties": {
"guid_of_the_RGB_schema_defined_extenally": {
"properties": {
"B": {
"type": "long"
},
"G": {
"type": "long"
},
"R": {
"type": "long"
}
}
},
"guid_of_the_normalized_RGB_schema_defined_extenally": {
"properties": {
"B": {
"type": "float"
},
"G": {
"type": "float"
},
"R": {
"type": "float"
}
},
"guid_of_the_HSL_schema_defined_extenally": {
"properties": {
"B": {
"type": "float"
},
"G": {
"type": "float"
},
"R": {
"type": "float"
}
}
}
}
}
这解决了字段名称相同但数据类型不同的情况。
有人可以建议我一个解决方案,可以解决索引爆炸的问题,而不会受到Flattened 在搜索中的限制吗?
【问题讨论】:
标签: elasticsearch elasticsearch-7