Springboot操作Elasticsearch

  常见的日志系统是基于logstach+elasticsearch+kibna框架搭建的,但是有时候kibana的查询无法满足我们的要求,因此有时需要代码去操作es,本文后续都以es代替elasticsearch。

一.es基本概念理解

  索引:含有相同属性的文档的集合.(可理解为数据库database) 

  类型:索引可以定义一个或多个类型,文档必须属于一个类型。(可以想象成数据库中的表table) 

  文档:文档是可以被索引的基本数据单位。(可以想象成数据库表中的一条数据) 

  分片:每一个索引有多个分片,每个分片都是一个Lucene索引。 

  备份:拷贝一份备份就完成了分片的备份。 

注意:每创建一个索引默认会创建5个分片和一个备份,当主分片出问题时,备份可以代替工作;备份的分片还可以执行搜索操作。

二.es单机搭建

    如果要使用springboot操作es,最好下载es版本时按照以下表格来下载。

 

 

    安装包下载好后解压,然后执行bin/elasticsearch即可,最后访问http://localhost:9200/判断是否安装成功。

    如果想更方便的操作es可以下载一个elasticsearch-head,安装好访问界面大致如下图,本文暂不介绍es-head。

 

 

三.springboot操作es

    1.使用curl -X PUT "localhost:9200/teststudent12"创建索引,参数如下:

{
    "order":0,
    "template":"ll*",
    "settings":{
        "index":{
            "number_of_shards":"4",
            "number_of_replicas":"1"
        }
    },
    "mappings":{
        "StudentTestVo":{
            "dynamic_templates":[
                {
                    "string_fields":{
                        "mapping":{
                            "analyzer":"only_words_analyzer",
                            "index":"analyzed",
                            "type":"keyword",
                            "fields":{
                                "raw":{
                                    "ignore_above":512,
                                    "index":"not_analyzed",
                                    "type":"string"
                                }
                            }
                        },
                        "match_mapping_type":"string",
                        "match":"*"
                    }
                }
            ],
            "properties":{
                "name":{
                    "type":"text"
                },
                "grade":{
                    "type":"text"
                },
                "age":{
                    "type":"short"
                },
                "date":{
                    "type":"date"
                }
            }
        }
    },
    "aliases":{

    }
}

2.创建索引成功后,编写代码操作es,代码可以加公众号 码农独白 下载。

 

posted @ 2019-11-27 23:11  人不疯狂枉一生  阅读(1168)  评论(0编辑  收藏  举报