ES 中的那些坑

数组

1. 数组中的 full-text 字段将被 【analyzed】

2. 数组中【所有元素】的数据类型必须一致

3. 数组的数据类型,以其 【第一个元素为准

映射

1. 数据类型会自动进行转化,比如 123 可以被转为 string ,但是 “test string” 没法转换为 long 类型

2. 使用 logstash 自动导入数据,filed 类型不一致将导致导入失败

3. 动态模板设置中,要把更精细的控制写在后面,否则精细的控制可能不会生效

正确示例
{
    "order": 0,
    "template": "woc_test",
    "settings": {
        "index.number_of_shards": "3"
    },
    "mappings": {
        "old_report_date": {
            "dynamic_templates": [
                {
                    "not_analyzed": {
                        "mapping": {
                            "index": "not_analyzed",
                            "type": "string"
                        },
                        "match_mapping_type": "string"     //该模板匹配范围最广,必须第一个写,如果放在了最后,那么后面几个精确控制的模板都不会生效
                    }
                },
                {
                    "远程应用服务": {
                        "mapping": {
                            "index": "not_analyzed",
                            "type": "string"
                        },
                        "match": "远程应用服务"
                    }
                },
                {
                    "启用线路繁忙保护": {
                        "mapping": {
                            "index": "not_analyzed",
                            "type": "string"
                        },
                        "match": "启用线路繁忙保护"
                    }
                },
                {
                    "排除IP地址": {
                        "mapping": {
                            "index": "not_analyzed",
                            "type": "string"
                        },
                        "match": "排除IP地址"
                    }
                },
                {
                    "其它": {
                        "mapping": {
                            "index": "analyzed",
                            "type": "string"
                        },
                        "path_match": "其它.*"
                    }
                }
            ]
        }
    },
    "aliases": {}
}

索引

1. 【同一个 _type】中 【相同 filed】 类型前后不一致将导致针对该 field 的搜索,搜索不到完整内容,甚至搜索不到任何内容

2. 【同一个 _index】中【不同 _type】 存在相同字段名,类型却不同。那么针对整个 index 对该字段的 search 操作可能导致结果不正确。

时间类型

1. 自动识别的 date 类型,在存储时候被转为了【Numeric】类型,在搜索时候却不会反转,依然以 【Unix 时间戳】的形式返回

空字段

1. 空字段在 ES 中是不被保存的

  • "empty_string": ""
  • "null_value": null
  • "empty_array": []
  • "array_with_null_value": [ null ]

排序

多值字段

多值字段的值本身没有固定的排序。

对于日期和数字类型(如 day_last:[100, 98, 80])可以使用 min、max、avg 或 sum 等方式来排序

"sort": {
    "dates": {
        "order": "asc",
        "mode": "min"
    }
}

 2. 字符串字段被 analyzed 后相当于是多值字段排序,对于类型为 string 的多值字段,排序一般不准确。

单值字段

1. 字符串字段被 not_analyzed 后相当于是单个字符串字段,对于类型为 string 的单值字段,排序会按默认字符顺序来进行。(A<B<C)

原文地址:https://www.cnblogs.com/licongyu/p/5466841.html