es 创建动态索引(一)

2019-05-30  本文已影响0人  良人与我

es 实体类

/**
 * @author river
 * @date 2019/2/27 14:47
 **/
@Data
@Document(indexName = "student-" + "#{ T(com.river.es.entity.Student).dateStr() }",

        type = "student",
        shards = 2,
        replicas = 1, refreshInterval = "-1")
public class Student {

    @Id
    private String id;

    private String name;

    private Integer age;

    private String desc;

    public static String dateStr()
    {
        return DateUtil.now().replace(" ","");
    }
}

索引的后缀是时间,精确到秒钟
测试方法,每隔一秒钟插入一条数据。

    @Test
    public void testsave() throws InterruptedException {
        for (int i = 0; i < 5 ; i++) {
            Thread.sleep(1000);
            Student student = new Student();
            student.setName("name:"+i);
            studentService.save(student);
        }
    }

通过kibana 去查询结果


GET /student*/_search?pretty
{
  "query": {
    "match_all": {}
  }
}

结果如下 发现的确创建了多个索引

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 27,
    "successful": 27,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "student-2019-05-3020:09:38",
        "_type": "student",
        "_id": "nPalCGsBUgUE7dzSTPac",
        "_score": 1,
        "_source": {
          "id": null,
          "name": "name:0",
          "age": null,
          "desc": null
        }
      },
      {
        "_index": "student-2019-05-3020:09:41",
        "_type": "student",
        "_id": "nvalCGsBUgUE7dzSVvYo",
        "_score": 1,
        "_source": {
          "id": null,
          "name": "name:2",
          "age": null,
          "desc": null
        }
      },
      {
        "_index": "student-2019-05-3020:09:42",
        "_type": "student",
        "_id": "n_alCGsBUgUE7dzSW_YE",
        "_score": 1,
        "_source": {
          "id": null,
          "name": "name:3",
          "age": null,
          "desc": null
        }
      },
      {
        "_index": "student-2019-05-3020:09:43",
        "_type": "student",
        "_id": "oPalCGsBUgUE7dzSX_bT",
        "_score": 1,
        "_source": {
          "id": null,
          "name": "name:4",
          "age": null,
          "desc": null
        }
      },
      {
        "_index": "student-2019-05-3020:09:40",
        "_type": "student",
        "_id": "nfalCGsBUgUE7dzSUfZj",
        "_score": 1,
        "_source": {
          "id": null,
          "name": "name:1",
          "age": null,
          "desc": null
        }
      }
    ]
  }
}

参考地址
https://www.jianshu.com/p/0b603db1278f
https://www.dingdangss.com/2017/04/05/%E5%88%A9%E7%94%A8springAOP-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%B3%A8%E8%A7%A3-SpEL%E5%AE%9E%E7%8E%B0%E6%93%8D%E4%BD%9C%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95/

上一篇 下一篇

猜你喜欢

热点阅读