【问题标题】:Elasticsearch: better to have more values or more fields?Elasticsearch:拥有更多值或更多字段更好?
【发布时间】:2018-10-10 06:17:44
【问题描述】:

假设有一个索引,其中包含描述车辆的文档。

您的索引需要处理两种不同类型的车辆:摩托车和汽车。

从性能的角度来看,以下哪个映射更好? (出于我的目的需要嵌套)

    "vehicle": {
        "type": "nested",
        "properties": {
            "car": {
                "properties": {
                    "model": {
                        "type": "string"
                    },
                    "cost": {
                        "type": "integer"
                    }
                }
            },
            "motorcycle": {
                "properties": {
                    "model": {
                        "type": "string"
                    },
                    "cost": {
                        "type": "integer"
                    }
                }
            }
        }
    }

或者这个:

"vehicle": {
    "type": "nested",
    "properties": {

        "model": {
            "type": "string"
        },
        "cost": {
            "type": "integer"
        },
        "vehicle_type": {
            "type": "string"     ### "car", "motorcycle"
        }

    }
}

第二个更易读,更薄。

但我的缺点是,当我进行查询时,如果我只想关注“汽车”,我需要将此条件作为查询的一部分。

如果我使用第一个映射,我只需要直接访问存储的字段,而不会增加查询的开销。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    第一个映射,其中汽车和摩托车被隔离在不同的领域,更有可能更快。原因是您已经知道要应用的过滤器少了一个,并且由于查询的选择性增加(例如,vehicle.car.model 的给定值的文档比 vehicle.model 少)

    另一种选择是创建两个不同的索引carmotorcycle,可能使用相同的index template

    在 Elasticsearch 中,查询由每个分片的单线程处理。这意味着,如果将索引一分为二,query both in a single request,它将并行执行。

    因此,当只需要查询一辆汽车或摩托车时,它会更快,因为索引更小。当涉及查询汽车和摩托车时,使用更多线程也可以更快。

    编辑:你应该知道后一个选项的一个缺点,内部 lucene 字典将被复制,如果汽车和摩托车中的值完全相同,它会使索引项列表加倍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-18
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      相关资源
      最近更新 更多