zoukankan      html  css  js  c++  java
  • es 索引别名

    别名是指给一个或者多个索引定义另外一个名称,使索引别名和索引之间可以建立某种逻辑关系。

    可以用别名表示别名和索引之间的包含关系。例如,我们建立了10月、11月、12月的用户入住酒店的日志索引,假设需要搜索这3个月的日志索引,如果分别去3个索引中进行搜索,这种编码方案比较低效。此时可以创建一个别名4_quarter,设置前面的3个索引的别名为4_quarter,然后在4_quarter中进行搜索即可。

    创建10_month_log,11_month_log ,12_month_log 3个索引:

    PUT /10_month_log 
    PUT /11_month_log 
    PUT /12_month_log 
    { 
        "mappings":{ 
            "properties":{ 
                "uid":{              //用户ID字段 
                    "type":"keyword" 
                }, 
                "hotel_id":{         //酒店ID字段 
                    "type":"keyword" 
                }, 
                "check_in_date":{     //入住日期字段 
                    "type":"keyword" 
                } 
            } 
        } 
    }

    写入文档:

    POST /10_month_log/_doc/001 
    {                                 //写入的文档数据    
      "uid":"001", 
      "hotel_id":"1", 
      "check_in_date":"2021-10-05" 
    }
    POST /11_month_log/_doc/002 
    {                                 //写入的文档数据    
      "uid":"002", 
      "hotel_id":"1", 
      "check_in_date":"2021-11-05" 
    }
    POST /12_month_log/_doc/003
    {                                 //写入的文档数据    
      "uid":"003", 
      "hotel_id":"1", 
      "check_in_date":"2021-12-05" 
    }

    设置上面3个索引的别名为4_quarter,请求的DSL如下:

    POST /_aliases 
    { 
      "actions": [ 
        { 
          "add": {   //为索引10_month_log建立别名4_quarter
            "index": "10_month_log", 
            "alias": "4_quarter" 
          } 
        }, 
        { 
          "add": {   //为索引11_month_log建立别名4_quarter
            "index": "11_month_log", 
            "alias": "4_quarter" 
          } 
        }, 
        { 
          "add": {   //为索引12_month_log建立别名4_quarter
            "index": "12_month_log", 
            "alias": "4_quarter" 
          } 
        } 
      ] 
    }

    此时,请求在索引4_quarter中搜索hotel_id为1的用户的入住记录,搜索的DSL如下:

    GET /4_quarter/_search 
    { 
      "query": { 
        "term": {        //搜索hotel_id为1的文档 
          "hotel_id": "1" 
        } 
      } 
    }

    在默认情况下,当一个别名只指向一个索引时,写入数据的请求可以指向这个别名,如果这个别名指向多个索引,则写入数据的请求是不可以指向这个别名的。

    ES不能确定向4_quarter写入数据时的转发对象。这种情况需要在别名设置时,将目标索引的is_write_index属性值设置为true来指定该索引可用于执行数据写入操作。例如设置12_month_log为数据写入转发对象,对应的DSL如下:

    POST /_aliases 
    { 
      "actions": [ 
        { 
          "add": {  //设置12_month_log为索引别名4_quarter的数据写入转发对象 
            "index": "12_month_log", 
            "alias": "4_quarter", 
             "is_write_index":true 
          } 
        } 
      ] 
    }

    文章参考:Elasticsearch搜索引擎构建入门与实战 --> 3.1.5 索引别名

  • 相关阅读:
    STUN协议简介
    AssetManager asset使用
    采购申请 POCIRM-001:ORA-01403: 无论数据未找到
    Windows7在自由的虚拟机(微软官方虚拟机)
    C面试题
    热血江湖按键精灵脚本游戏!
    System.setProperty()
    Linux下patch打补丁命令
    Eclipse和PyDev搭建python开发环境
    二维数组中的查找
  • 原文地址:https://www.cnblogs.com/ooo0/p/15632802.html
Copyright © 2011-2022 走看看