zoukankan      html  css  js  c++  java
  • Elasticsearch索引模板和别名

    创建模板(模板名和索引名一样都不能有大写)

    PUT http://222.108.x.x:9200/_template/templateds

    {
        "template": "dsideal*",
        "order": 0,
        "settings": {
            "number_of_shards": 5
        },
        "aliases": {
            "dsideal-query": {}      #起个别名
        },
        "mappings": {
            "doc": {
                "properties": {
                    "person_name": {
                        "type": "keyword"
                    },
                    "gender_id": {
                        "type": "long"
                    },
                    "bureau_id": {
                        "type": "long"
                    }
                }
            }
        }
    }

    写一些数据

    POST http://222.108.x.x:9200/dsideal10/doc/1

    {
        "person_name": "张三",
        "gender_id": 1,
        "bureau_id": 2
    }

    POST http://222.108.x.x:9200/dsideal11/doc/2

    {
        "person_name": "李四",
        "gender_id": 2,
        "bureau_id": 2
    }

     会按照模板自动生成两个索引“dsideal10”和“dsideal11”

    可以利用在创建模板时起的别名进行查询

    POST http://222.108.x.x:9200/dsideal-query/_search

    {
        "size": 10,
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {
                            "bureau_id": "2"
                        }
                    }
                ]
            }
        }
    }

    创建多个索引别名【备忘】

    POST http://222.108.x.x:9200/_aliases

    {
        "actions" : [
            { "add" : { "index" : "dsideal1","alias" : "alias1" } },
            { "add" : { "index" : "dsideal2","alias" : "alias1" } }
        ]
    }

    在创建一个索引时可为这个索引根据条件创建多个别名

    POST http://222.108.x.x:9200/test100

    {
        "aliases" : {
            "2014" : {
                "filter" : {
                    "term" : {"year": 2014 }
                }
            },
            "2015" : {
                "filter" : {
                    "term" : {"year": 2015 }
                }
            },
            "2016" : {
                "filter" : {
                    "term" : {"year": 2016 }
                }
            }
        },
        "mappings": {
            "doc": {
                "properties": {
                    "person_name": {
                        "type": "keyword"
                    },
                    "year": {
                        "type": "long"
                    },
                    "bureau_id": {
                        "type": "long"
                    }
                }
            }
        }
    }

    说明:当插入year=2015的数据时可用2015这个别名去查询

    测试插入一些数据

    {"person_name":"张三","year":2014,"bureau_id":2},
    {"person_name":"李四","year":2015,"bureau_id":3},
    {"person_name":"王五","year":2015,"bureau_id":3},
    {"person_name":"赵六","year":2016,"bureau_id":4}

     post http://222.108.x.x:9200/2015/_search

    {
        "size": 10,
        "query": {
            "bool": {
                "must": [
                    {
                        "match_all": {
                            
                        }
                    }
                ]
            }
        }
    }

    就只返回year=2015的数据

  • 相关阅读:
    log4net preserveLogFileNameExtension 和 watch
    BootStrap自带的图标
    git fetch批处理,遍历一个文件夹下的所有子目录,执行git fetch --all
    Recommended Settings for Tracing and Message Logging
    蜗牛—JSONJ学习ava转变
    Do you master on array in C ?
    全面推行使用智能指针的权利
    2014/08/23——OJ出现waiting...
    在Windows通过使用MinGW静态编译Assimp
    Ubuntu12.04password正确 入口的桌面(测试的恢复正常)
  • 原文地址:https://www.cnblogs.com/kgdxpr/p/9627527.html
Copyright © 2011-2022 走看看