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

注解式elasticsearch+SpringBoot(附分布式配置)前言:以前使用的是RestHighLevelClient客户端,使用起来一大堆的类相互嵌套,特别是agg操作,代码十分惨烈。架构:使用方式与mybatis类似,采用xml的形式,将dsl与代码分离。示例用了swagger2和lombok。需知:必须学会DSL语法(看半小时差不多就会了吧)。依赖:<dependency><group…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

前言:以前使用的是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>

Jetbrains全家桶1年46,售后保障稳定

配置:

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>

分布式配置:

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

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

有疑问家sunziwen3366备注csdn

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/215466.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • JavaScript面向对象思想

    JavaScript面向对象思想javascript中的面向对象:ECMA标准定义JS中的对象:无序属性的集合,其属性可以包含基本值、对象或者函数。可以简单理解为JS的对象是一组无序的值,其中的属性或方法都有一个名字,根据这个名字可以访问相映射的值(值可以是基本值/对象/方法)面向对象三个基本特征是:封装、继承、多态封装:将对象运行所需的资源封装在程序对象中,基本上是方法和数据。对象是“公布其接口”。其他附加到这些接口上的对象不需要关心对象实现的方法即可使用这个对象。这个概念就是“不要告诉我你是怎么做的,只要做就可以了。”对象可

    2022年10月31日
  • spring、springMvc、springBoot和springCloud的联系与区别

    spring、springMvc、springBoot和springCloud的联系与区别spring和springMvc:1.spring是一个一站式的轻量级的java开发框架,核心是控制反转(IOC)和面向切面(AOP),针对于开发的WEB层(springMvc)、业务层(Ioc)、持久层(jdbcTemplate)等都提供了多种配置解决方案;2.springMvc是spring基础之上的一个MVC框架,主要处理web开发的路径映射和视图渲染,属于spring框架中WE…

  • mybatiscodehelperpro在线激活码(JetBrains全家桶)

    (mybatiscodehelperpro在线激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.htmlFZP9ED60OK-eyJsaWNlbnNlSWQi…

  • 世界各个地区WIFI 2.4G及5G信道划分表(附无线通信频率分配表)

    目前主流的无线WIFI网络设备802.11a/b/g/n/ac:传统802.111997年发布两个原始数据率:1Mbps和2Mbps跳频展频(FHSS)或直接序列展布频谱(DSSS)三个不重叠的信道中,工业、科学、医学(ISM)频段频率为2.4GHz最初定义的载波侦听多点接入/避免冲撞(CSMA-CA)802.11a1999年发布提供多种调制类型的数据传输率:6、9、12、18、24…

  • win10无法识别的usb设备前一个设备不正常_蓝牙变成未知usb设备

    win10无法识别的usb设备前一个设备不正常_蓝牙变成未知usb设备[修复]未知的USB设备(设备描述符请求失败)在Windows10中转至[修复]未知的USB设备(设备描述符请求失败)在Windows10中

  • Thinkphp5.0+Vue2.0前后端分离框架Vuethink

    Thinkphp5.0+Vue2.0前后端分离框架Vuethink

    2021年10月11日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号