SpringBoot集成BBOSS-ElasticSearch实现ElasticSearch客户端

一、BBOSS-ElasticSearch

二、通过freeMarker创建索引库、创建索引

2.1maven

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.bbossgroups.plugins</groupId>
    <artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
    <version>6.5.6</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.bbossgroups.plugins</groupId>
    <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
    <version>6.5.6</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.23.1</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

2.2application.yml

spring:
  elasticsearch:
    bboss:
      elasticPassword: 123456
      elasticUser: elastic
      elasticsearch:
        dateFormat: yyyy.MM.dd
        discoverHost: false
        rest:
          hostNames: 127.0.0.1:9200
        scrollBlockedWaitTimeout: 0
        scrollThreadCount: 200
        scrollThreadQueue: 200
        showTemplate: false
        sliceScrollBlockedWaitTimeout: 0
        sliceScrollThreadCount: 100
        sliceScrollThreadQueue: 100
        timeZone: Asia/Shanghai
      http:
        connectionRequestTimeout: 5000
        customHttpRequestRetryHandler: org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler
        defaultMaxPerRoute: 200
        hostnameVerifier:
        keepAlive: 3600000
        keyPassword:
        keystore:
        maxHeaderCount: 200
        maxLineLength: -1
        maxTotal: 400
        retryTime: 1
        retryInterval: 1000
        soKeepAlive: false
        soReuseAddress: false
        staleConnectionCheckEnabled: false
        timeToLive: 3600000
        timeoutConnection: 5000
        timeoutSocket: 5000
        validateAfterInactivity: 2000
      dslfile:
        refreshInterval: -1

2.3接口方法

@RequestMapping(value="/createEsIndexMapping",method = RequestMethod.POST)
    public String createEsIndexMapping(@RequestBody CmIndexes cmIndexes) throws Exception {
        String str = "";
        //1、第一步使用freeMarker生成xml文件
        String cmIndex = esUtils.createXml(cmIndexes);
        //2、第二步创建ES索引
        String mapping = createIndiceMapping(cmIndexes);
        //判断是否存在
        if("1".equals(mapping)){
            str = "索引库已经存在!!!";
            return str;
        }
        return cmIndex;
    }

2.4freeMarker生成xml文件

 public String createXml(CmIndexes cmIndexes) throws IOException {
        Writer w  = null;
        String str = "";
        try {
            //1、获取xmlTemplate文件夹的当前路径
            URL url = Thread.currentThread().getContextClassLoader().getResource("templates");
            String path = url.getPath();
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding("utf-8");//解决写入到xml文件出现乱码问题
            configuration.setDirectoryForTemplateLoading(new File(path));
            //2、获取到freeMarker模板
            Template template = configuration.getTemplate("es.ftl","utf-8");
            Map<String, Object> responseMap = new HashMap<String, Object>();

            //3、获取索引库字段类型
            List<CmField> fieldList = cmIndexes.getFieldList();//获取到映射配置文件
            responseMap.put("esMappings",fieldList);//设置
            responseMap.put("mappingName",cmIndexes.getIndexesMappingName());//设置映射名称
            responseMap.put("indexName",cmIndexes.getIndexesName());//设置索引名称

            str = route+cmIndexes.getIndexesName()+".xml";
            File file = new File(str);
            FileOutputStream f = new FileOutputStream(file);
            w = new OutputStreamWriter(f,"utf-8");
            template.process(responseMap, w);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "文件创建失败";
        }finally{
            w.close();
        }
        return "创建成功";
    }

2.5freeMarker模板【需要根据实际手动配置模板】


<properties>
    
    <property name="${mappingName}">
        
            "${esMapping.fieldName}":{
            "type":"${esMapping.indexesFieldType}"<#if esMapping.isDate == 1>,
            "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
            }<#if esMapping_has_next>,
        
        }
        }
        }]]>
    property>

    <property name="testHighlightSearch">
        
                        #if($${esMapping.fieldName} && !$${esMapping.fieldName}.equals(""))
                            <#if esMapping.isDate == 1>
                            {
                                ## 时间范围检索,返回对应时间范围内的记录,接受long型的值
                                "range": {
                                    "${esMapping.fieldName}":{
                                        "gte": #[startTime],##统计开始时间
                                        "lt": #[endTime],  ##统计截止时间
                                    }
                                }
                            },
                            
                            <#if esMapping.isDate == 2>
                            {
                                "match" : {
                                    "${esMapping.fieldName}":{
                                        "query":#[${esMapping.fieldName}],
                                        "operator": "and"
                                    }
                                }
                            },
                            
                        #end
                    
                    {
                    "match_all": {
                        }
                    }
                ]
            }
        },
        ## 分页起点
            "from":#[from],
        ## 最多返回size条记录
            "size":#[size]
        }]]>
    property>
properties>

2.6创建ES索引

public String createIndiceMapping(CmIndexes cmIndexes){
        String str = "";
        //1、创建es客户端对象
        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("./esmapper/"+cmIndexes.getIndexesName()+".xml");
        //2、查询该索引是否存在
        boolean exist = clientUtil.existIndice(cmIndexes.getIndexesName());
        //3、如果存在,则删除索引表
        if (exist){
            str = "1";
            return str;
        }
        //4、不存在,则创建索引
        str = clientUtil.createIndiceMapping(cmIndexes.getIndexesName(), cmIndexes.getIndexesMappingName());
        return str;
    }

三、删除索引以及freeMarker文件

@RequestMapping(value="/deleteEsIndex",method = RequestMethod.POST)
    public String deleteEsIndex(@RequestParam String indexName){
        ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
        try {
            //1、删除索引
            //删除索引表
            String indice = clientUtil.dropIndice(indexName);
            //删除上一步创建的ftl文件  route是在pom.xml里面设置的路径
            String fileUrl = route+indexName+".xml";
            File file = new File(fileUrl);
            boolean delete = file.delete();
            if(delete == false){
                return "删除失败";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "删除成功";
    }

3.1单个删除

ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
clientUtil.deleteDocument("索引名称","文档类型","id");

3.2新增更新

clientUtil.addDocument("索引名称",“内容”);
clientUtil.addDocumentWithId("索引名称","文档类型","内容","内容id");

四、ES查询

4.1首先前端传JSON参数

SpringBoot集成BBOSS-ElasticSearch实现ElasticSearch客户端_第1张图片

4.2后端接口进行查询

@RequestMapping(value="/selectEsIndex",method = RequestMethod.POST)
    public List<Object> selectEsIndex(String indexName,String params) throws Exception {
        Map map = (Map)JSONObject.parse(params);

        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/"+indexName+".xml");//初始化一个加载sql配置文件的es客户端接口
        //执行查询,demo为索引表,_search为检索操作action
        ESDatas<Object> esDatas =  //ESDatas包含当前检索的记录集合,最多1000条记录,由dsl中的size属性指定
                clientUtil.searchList(indexName+"/_search",//demo为索引表,_search为检索操作action
                        "testHighlightSearch",//esmapper/demo.xml中定义的dsl语句
                        map,//变量参数
                        Object.class);//返回的文档封装对象类型
        System.out.println(esDatas.getDatas());
        return esDatas.getDatas();
    }

此次难点:1、传值类型是否正确;2、freeMarker模板查询配置是否正确;

ElasticSearch入门学习笔记(一)概念篇
ElasticSearch入门学习笔记(二)软件安装篇
ElasticSearch入门学习笔记(三)SpringBoot整合篇
阿里云Docker安装ES\ES_Head\安装部署logstash导mysql数据入ElasticSearch

在这里插入图片描述

你可能感兴趣的:(ELasticSearch,elasticsearch,spring,boot,java)