Elasticsearch索引按月划分以及获取所有索引数据

项目中数据库根据月份水平划分,由于没有用数据库中间件,没办法一下查询所有订单信息,所有用Elasticsearch做订单检索。

Elasticsearch索引和数据库分片同步,也是根据月份来建立索引。

思路:

  1. 建立索引时用“order_yyyyMM”命名;
  2. 为这些订单索引建立别名“order”;
  3. 写数据时把数据写到对应的月份索引,读时根据别名读取,就可以查询到所有订单数据;

对索引分片也可以根据业务灵活的删除老数据。

具体实现需要用到模板,

template可以修改索引的默认配置。我们以下面这个template为例说明一下。

  1. 建立一个template名称为order_template
  2. "template": "order*",表示对于所有以order*开头的索引,默认配置使用template中的配置。
  3. "settings","mappings","aliases",可以修改这些类型的默认配置
curl -XPUT "http://localhost:9200/_template/order_template?pretty" -d '{
  "template": "order*",
  "settings": {
    "number_of_shards": 10
  },
  "mappings": {
    "data": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "name": {
          "type": "string",
          "index": "not_analyzed"
        },
        "id": {
          "type": "long"
        }
      }
    }
  },
  "aliases": {"order":{}}
}'

  设置模板后,每次创建order索引,就会自动关联别名,读数据时就可以通过别名一下把所有数据都获取到。

 

原文地址:https://www.cnblogs.com/zxxfz/p/10876757.html