注解式elasticsearch+SpringBoot(附分布式配置)

前言:以前使用的是Rest High Level Client客户端,使用起来一大堆的类相互嵌套,特别是agg操作,代码十分惨烈。

架构:使用方式与mybatis类似,采用xml的形式,将dsl与代码分离。示例用了swagger2和lombok。

需知:必须学会DSL语法(看半小时差不多就会了吧)。


依赖:

<dependency>
            <groupId>com.bbossgroups.plugins</groupId>
            <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
            <version>5.9.5</version>
        </dependency>

配置:

server:
  port: 3000
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://xxxxxxxxxxxxx/xxxxx?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: **********
  #      type: com.alibaba.druid.pool.DruidDataSource
  elasticsearch:
    bboss:
      elasticUser: elastic
      elasticPassword: changeme
      elasticsearch:
        rest:
          hostNames: xxx.xxx.xxx.xxx:9200
          ##hostNames: 192.168.8.25:9200,192.168.8.26:9200,192.168.8.27:9200  ##集群地址配置
        dateFormat: yyyy.MM.dd
        timeZone: Asia/Shanghai
        ttl: 2d
        showTemplate: true
        discoverHost: false
      dslfile:
        refreshInterval: -1
      http:
        timeoutConnection: 5000
        timeoutSocket: 5000
        connectionRequestTimeout: 5000
        retryTime: 1
        maxLineLength: -1
        maxHeaderCount: 200
        maxTotal: 400
        defaultMaxPerRoute: 200
        soReuseAddress: false
        soKeepAlive: false
        timeToLive: 3600000
        keepAlive: 3600000
        keystore:
        keyPassword:
        hostnameVerifier:

使用示例:

实体类:

import com.frameworkset.orm.annotation.ESId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * TODO
 *
 * @author sunziwen
 * @version 1.0
 * @date 2019/12/12 14:53
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Person{
    @ESId
    private Integer personId;
    private String name;
    private Integer age;
    private String introduction;
}

测试:

package com.example.layer.controller;

import com.example.layer.entity.Person;
import io.swagger.annotations.ApiOperation;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.ESDatas;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * TODO
 *
 * @author sunziwen
 * @version 1.0
 * @date 2019/12/12 13:32
 **/
@RestController
public class TestController {
    private final BBossESStarter bBossESStarter;

    public TestController(BBossESStarter bBossESStarter) {
        this.bBossESStarter = bBossESStarter;
    }

    @PostMapping("test_create")
    @ApiOperation("创建索引")
    public Object create() {
        ClientInterface restClient = bBossESStarter.getConfigRestClient("elasticsearch/person.xml");
        return restClient.createIndiceMapping("person", "createPersonIndice");
    }

    @PostMapping("test_add")
    @ApiOperation("添加文档")
    public Object add() {
        ClientInterface restClient = bBossESStarter.getRestClient();
        Person person = Person.builder()
                              .personId(-1)
                              .name("张三丰")
                              .age(100)
                              .introduction("武当创始人")
                              .build();
        return restClient.addDocument("person", "person", person, "refresh");
    }

    @PostMapping("test_adds")
    @ApiOperation("批量添加文档")
    public Object adds() {
        ClientInterface restClient = bBossESStarter.getRestClient();
        ArrayList<Person> people = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            Person person = Person.builder()
                                  .personId(i)
                                  .name("张三丰" + i)
                                  .age(100 + i * 2)
                                  .introduction("武当创始人" + i * 3)
                                  .build();
            people.add(person);
        }
        return restClient.addDocuments("person", "person", people, "refresh");
    }

    @PostMapping("test_getById")
    @ApiOperation("Id获取文档")
    public Object getById(@RequestParam Integer id) {
        ClientInterface restClient = bBossESStarter.getRestClient();
        return restClient.getDocument("person", "person", id + "", Person.class);
    }

    @PostMapping("test_search")
    @ApiOperation("检索")
    public Object search() {
        ClientInterface restClient = bBossESStarter.getConfigRestClient("elasticsearch/person.xml");
        HashMap<String, Object> params = new HashMap<>(2);
        params.put("min", 100);
        params.put("max", 300);
        params.put("size", 100);
        ESDatas<Person> searchRange = restClient.searchList("person/_search", "searchRange", params, Person.class);
        return searchRange;
    }
}

XML:

<properties>
    <property name="createPersonIndice">
        <![CDATA[{
            "settings": {
                "number_of_shards": 1,
                "index.refresh_interval": "5s"
            },
            "mappings": {
                "person": {
                    "properties": {
                        "id":{
                            "type":"long"
                        },
                        "name": {
                            "type": "keyword"
                        },
                        "age":{
                            "type":"long"
                        },
                        "introduction": {
                            "type": "text"
                        }
                    }
                }
            }
        }]]>
    </property>
    <property name="searchRange">
        <![CDATA[{
            "query": {
                "bool": {
                    "filter": [
                        {
                            "range": {
                                "age": {
                                    "gte": #[min],
                                    "lt": #[max]
                                }
                            }
                        }
                    ]
                }
            },
            "size":#[size]
        }]]>

    </property>
</properties>

分布式配置:

这里为了方便懒得建分布式项目了。在启动时从分布式配置中心拿到相应的配置后...(这里我直接写死了)

有疑问家sunziwen3366备注csdn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文子阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值