SqlEs快速开始
可以参考demo:https://github.com/czcuestc/sqles/tree/master/sqles-demo
1,使用SqlEs很简单,首先创建实体类,通过注解配置索引,示例如下:
@Document
@Setting(maxResultWindow = 1000)
public class TestEntity implements Serializable {
private static final long serialVersionUID = -190806307024075427L;
/**
* id字段,作为实体对象的唯一标识,可以根据该字段快速查找记录。
* id字段的值必须设置,若id不设置,则插入,更新,删除操作会失败。
*/
@Id
@LongField
private Long id;
/**
* 添加了Highlight注解,默认会将搜索高亮结果内容放入highlight字段,highlight字段变量名默认为highlight+当前字段名,camel命名
*
*/
@Highlight
@TextField(analyzer = Analyzer.IK_SMART)
private String textField;
/**
* highlight字段
*/
private String highlightTextField;
@KeyWordField
private String keywordField;
@BoolField
private Boolean boolField;
@ByteField
private Byte byteField;
@ShortField
private Short shortField;
@ShortField
private Short nullValue;
@IntegerField
private Integer integerField;
/***
* Routing注解可设置路由字段
*/
@Routing
@LongField
private Long longField;
@HalfFloatField
private Float halfFloatField;
@ScaledFloatField(scalingFactor = 100)
private BigDecimal scaledFloatField;
@FloatField
private Float floatField;
@DoubleField
private Double doubleField;
@DateField
private String stringDateField;
@DateField
private Date dateField;
@DateField
private Long longDateField;
@DateField
private Time timeField;
@DateField
private Timestamp timestampField;
@DateField
private LocalTime localTimeField;
@DateField
private LocalDateTime localDateTimeField;
@DateField
private Instant instantField;
/**
* 记录score会默认放入该字段,也可以用@Score注解指定。
*/
private float score;
}
2,配置连接字符串,和数据库配置一样,唯一区别es连接字符为jdbc:es//xxxx,多个es实例以逗号分隔。
spring: datasource: sqles: url: jdbc:es://localhost:9200/mall?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: 123 driver-class-name: com.czcuestc.sqles.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: initial-size: 15 #连接池初始化大小 min-idle: 20 #最小空闲连接数 max-active: 30 #最大连接数
3,配置mapper扫描注解
@SqlEsMapperScan(basePackages = "com.sqles.demo")
SqlEsMapperScan扫描实体类配置和索引管理mapper。
MapperScan扫描es mapper配置和接口(es的mapper文件和数据库配置方式一样,你可以把es看成一个数据库,es提供了jdbc驱动,屏蔽了底层差异),然后可以通过mybatis访问es了。
/** * Copyright [2023-2024] [czcuestc] * All rights reserved */ @Configuration @MapperScan(basePackages = "com.sqles.demo.mapper") @SqlEsMapperScan(basePackages = "com.sqles.demo") public class DataSourceConfig { @ConfigurationProperties(prefix = "spring.datasource.sqles") @Bean(name = "sqles") public DataSource dataSource() { DataSource dataSource= DruidDataSourceBuilder.create().build(); return dataSource; } }