首页 > 其他分享 >easyes 使用

easyes 使用

时间:2023-02-23 14:56:35浏览次数:19  
标签:cn easyes private 使用 import annotation es

easyes 是一个国产的开源软件,提供类似于mybatis-plus查询数据的方式来查询es数据,极大提高了实现es增删改查功能的开发效率

使用方式如下

1.工程导入依赖

        <dependency>
            <groupId>cn.easy-es</groupId>
            <artifactId>easy-es-boot-starter</artifactId>
<!--            <version>1.0.2</version>-->
            <version>1.1.1</version>
        </dependency>

2.新增数据模型(和索引对应)

package com.example.eeuse.model;

import cn.easyes.annotation.HighLight;
import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexId;
import cn.easyes.annotation.IndexName;
import cn.easyes.annotation.rely.Analyzer;
import cn.easyes.annotation.rely.FieldStrategy;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.annotation.rely.IdType;
import lombok.Data;

/**
 * ES数据模型
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
@Data
@IndexName("document2")
public class Document {
    /**
     * es中的唯一id,如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize,如此id便支持任意数据类型)
     */
    @IndexId(type = IdType.CUSTOMIZE)
    private Long id;
    /**
     * 文档标题,不指定类型默认被创建为keyword类型,可进行精确查询
     */
    private String title;
    /**
     * 文档内容,指定了类型及存储/查询分词器
     */
    @HighLight(mappingField="highlightContent")
    @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
    private String content;
    /**
     * 作者 加@TableField注解,并指明strategy = FieldStrategy.NOT_EMPTY 表示更新的时候的策略为 创建者不为空字符串时才更新
     */
    @IndexField(strategy = FieldStrategy.NOT_EMPTY)
    private String creator;
    /**
     * 创建时间
     */
    @IndexField(fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
    private String gmtCreate;
    /**
     * es中实际不存在的字段,但模型中加了,为了不和es映射,可以在此类型字段上加上 注解@TableField,并指明exist=false
     */
    @IndexField(exist = false)
    private String notExistsField;
    /**
     * 地理位置经纬度坐标 例如: "40.13933715136454,116.63441990026217"
     */
    @IndexField(fieldType = FieldType.GEO_POINT)
    private String location;
    /**
     * 图形(例如圆心,矩形)
     */
    @IndexField(fieldType = FieldType.GEO_SHAPE)
    private String geoLocation;
    /**
     * 自定义字段名称
     */
    @IndexField(value = "wu-la")
    private String customField;

    /**
     * 高亮返回值被映射的字段
     */
    private String highlightContent;
}

3.查询接口dao

package com.example.eeuse.mapper;

import cn.easyes.core.conditions.interfaces.BaseEsMapper;
import com.example.eeuse.model.Document;

/**
 * Mapper
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
public interface DocumentMapper extends BaseEsMapper<Document> {
}

4.springboot启动类加注解扫描

package com.example.eeuse;

import cn.easyes.starter.register.EsMapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 启动类
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
@SpringBootApplication
@EsMapperScan("com.example.eeuse.mapper")
public class EeUseApplication {

    public static void main(String[] args) {
        SpringApplication.run(EeUseApplication.class, args);
    }

}

5.基础配置yml

easy-es:
#  address: 10.20.64.228:9200
  address: ip:9200
#  username: elastic #es用户名,若无则删去此行配置
#  password: WG7WVmuNMtM4GwNYkyWH #es密码,若无则删去此行配置
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
server:
  port: 8080

 

6.插入与查询测试

package com.example.eeuse.controller;

import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.example.eeuse.mapper.DocumentMapper;
import com.example.eeuse.model.Document;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 测试使用Easy-ES
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestUseEeController {
    private final DocumentMapper documentMapper;

    @GetMapping("/insert")
    public Integer insert() {
        // 初始化-> 新增数据
        Document document = new Document();
        document.setId(System.currentTimeMillis());
        document.setTitle("老汉");
        document.setContent("武汉市长江大桥");
        document.setCreator("7157");
        document.setLocation("40.13933715136454,116.63441990026217");
        return documentMapper.insert(document);
    }

    @GetMapping("/search")
    public List<Document> search() {
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.like(Document::getContent, "长江");//40288fe7859f95720185a04964890015
        return documentMapper.selectList(wrapper);
    }
}

调用insert接口测试

 

 调用search接口测试

 

标签:cn,easyes,private,使用,import,annotation,es
From: https://www.cnblogs.com/jiaqirumeng/p/17147931.html

相关文章

  • JPA在SpringBoot中简单使用
    前言在SpringBoot项目中可以与JPA进行搭配,这样会省很多的开发时间,以下为JPA的简单使用一、导入依赖<!--springbootjpa依赖--><dependency><groupId>org.spring......
  • MarkDown的使用
    MarkDown的使用规范标题1标题2标题3标题4标题5字体**Hello,Word!**Hello,Word!Hello,Word!Hello,Word!引用学java,的以一个博客.分割线图片![截图......
  • BTrace使用
    来源《深入理解java虚拟机》书中第四章提到一个VisualVM插件,叫做BTrace,竟然可以在不改变原有代码也不停进程的基础上,在进程嵌入一段代码,获取进程中的一些方法参数和返回......
  • CH32V203C8T6使用SPI2出现的问题
    最近调试一个项目CAN转SPI(SPI主机),另外一个SPI从机接收使用到SPI2,一直测试不通,特此记录首先使用沁恒官方给的历程(点击即可下载该历程)可以正常跑通,使用自己的程序 一样......
  • 直播软件源码,在vue中使用html2canvas在前端生成图片
    直播软件源码,在vue中使用html2canvas在前端生成图片1、安装 npminstallhtml2canvas​2、用法 importhtml2canvasfrom'html2canvas'; html2canvas(document.......
  • 浅析MySQL中concat以及group_concat的使用
    说明:本文中使用的例子均在下面的数据库表tt2下执行: 一、concat()函数1、功能:将多个字符串连接成一个字符串。2、语法:concat(str1,str2,...)返回结果为连接参数产生......
  • c++线程的使用
    c++11之后,c++语言提供了并发编程的语言支持。c++11增加了线程以及线程相关的类。c++11提供的线程类叫做std::thread,创建线程只需提供线程函数或者函数对象,并且可以指定参......
  • Chatgpt的简单使用
    一、注册方式1、进入官网,常规操作,邮箱登录网址:https://platform.openai.com/2、找到合适的接码平台,目前全网都在用俄罗斯某接码网址:https://sms-activate.org/常规操作......
  • Mysql插入数据从指定选项中随机选择、插入时间从指定范围随机生成、Navicat使用存储过
    场景Navicat通过存储过程批量插入mysql数据:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/87855148上面使用过Navicat借助存储过程批量插入数据。但是插......
  • 集合类再探:不可变类的好处,Collector接口详解,使用内部迭代
    集合类再探注:本文使用的pom依赖见文末。......