1.1 部署es和kibana
1.2 springboot整合es及配置
1.2.1 引入相关依赖
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
<exclusions>
<exclusion>
<groupid>org.elasticsearch.client</groupid>
<artifactid>elasticsearch-rest-high-level-client</artifactid>
</exclusion>
<exclusion>
<groupid>org.elasticsearch.client</groupid>
<artifactid>elasticsearch-rest-client</artifactid>
</exclusion>
<exclusion>
<groupid>org.elasticsearch</groupid>
<artifactid>elasticsearch</artifactid>
</exclusion>
</exclusions>
</dependency>
<!--es相关-->
<dependency>
<groupid>org.elasticsearch.client</groupid>
<artifactid>elasticsearch-rest-high-level-client</artifactid>
<version>7.14.0</version>
</dependency>
<dependency>
<groupid>org.elasticsearch.client</groupid>
<artifactid>elasticsearch-rest-client</artifactid>
<version>7.14.0</version>
</dependency>
<dependency>
<groupid>org.elasticsearch</groupid>
<artifactid>elasticsearch</artifactid>
<version>7.14.0</version>
</dependency>
<dependency>
<groupid>org.dromara.easy-es</groupid>
<artifactid>easy-es-boot-starter</artifactid>
<version>2.0.0-beta4</version>
</dependency>
1.2.2 yml相关配置
easy-es:
enable: true
address: 192.168.164.128:9200
schema: http
banner: false
keep-alive-millis: 30000
connect-timeout: 5000
socket-timeout: 600000
connection-request-timeout: 5000
max-conn-total: 100
max-conn-per-route: 100
global-config:
process-index-mode: manual
print-dsl: true
distributed: false
reindextimeouthours: 72
async-process-index-blocking: true
db-config:
map-underscore-to-camel-case: true
id-type: customize
1.3 索引crud
1.3.1 索引托管自动挡
1.3.1.1 配置实体模板
@data
@indexname(shardsnum = 3,replicasnum = 2)
public class document {
@indexid(type = idtype.customize)
private long id;
private string title;
@highlight(mappingfield="highlightcontent")
@indexfield(fieldtype = fieldtype.text, analyzer = analyzer.ik_smart, searchanalyzer = analyzer.ik_max_word)
private string content;
@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;
@indexfield(exist = false)
private string notexistsfield;
@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;
}
1.3.1.2 配置启动模式
easy-es:
sockettimeout: 600000
global-config:
process_index_mode: smoothly
async-process-index-blocking: true
distributed: false
reindextimeouthours: 72
1.3.2 索引手动挡
1.3.2.1 配置启动模式
easy-es:
global-config:
process_index_mode: manual
1.3.2.2 配置实体模板
@data
@indexname(shardsnum = 3, replicasnum = 2, keepglobalprefix = true)
public class document {
@indexid(type = idtype.customize)
private string id;
private string title;
@highlight(mappingfield = "highlightcontent")
@indexfield(fieldtype = fieldtype.text, analyzer = analyzer.ik_smart, searchanalyzer = analyzer.ik_max_word)
private string content;
}
1.3.2.3 创建索引
@test
public void testcreateindexbyentity() {
documentmapper.createindex();
}
@test
public void testcreateindexbyentity() {
string indexname = localdate.now().format(datetimeformatter.ofpattern("yyyy-mm-dd"));
documentmapper.createindex(indexname);
}
@test
public void testcreateindex() {
lambdaesindexwrapper<document> wrapper = new lambdaesindexwrapper<>();
wrapper.indexname(document.class.getsimplename().tolowercase());
wrapper.mapping(document::gettitle, fieldtype.keyword, 2.0f)
.mapping(document::getlocation, fieldtype.geo_point)
.mapping(document::getgeolocation, fieldtype.geo_shape)
.mapping(document::getcontent, fieldtype.text, analyzer.ik_smart, analyzer.ik_max_word);
wrapper.mapping("wu-la", fieldtype.text, analyzer.ik_max_word, analyzer.ik_max_word);
wrapper.settings(3, 2);
string aliasname = "daily";
wrapper.createalias(aliasname);
wrapper.join("joinfield", "document", "comment");
boolean isok = documentmapper.createindex(wrapper);
assertions.asserttrue(isok);
}
1.3.2.4 查询索引
@test
public void testexistsindex() {
string indexname = document.class.getsimplename().tolowercase();
boolean existsindex = documentmapper.existsindex(indexname);
assertions.asserttrue(existsindex);
}
@test
public void testgetindex() {
getindexresponse indexresponse = documentmapper.getindex();
indexresponse.getmappings().foreach((k, v) -> system.out.println(v.getsourceasmap()));
}
1.3.2.5 更新索引(不推荐)
@test
public void testupdateindex() {
lambdaesindexwrapper<document> wrapper = new lambdaesindexwrapper<>();
string indexname = document.class.getsimplename().tolowercase();
wrapper.indexname(indexname);
wrapper.mapping(document::getcreator, fieldtype.keyword);
wrapper.mapping(document::getgmtcreate, fieldtype.date);
boolean isok = documentmapper.updateindex(wrapper);
assertions.asserttrue(isok);
}
1.3.2.6 删除索引
@test
public void testdeleteindex() {
string indexname = document.class.getsimplename().tolowercase();
boolean isok = documentmapper.deleteindex(indexname);
assertions.asserttrue(isok);
}
1.4 数据crud
1.4.1 数据同步
1.4.2 数据crud
1.4.2.1 新增数据
integer insert(t entity);
integer insert(t entity, string... indexnames);
integer insertbatch(collection<t> entitylist)
integer insertbatch(collection<t> entitylist, string... indexnames);
- 如果您在insert时传入的entity有id并且该id对应数据已存在,则此次insert实际效果为更新该id对应的数据,并且更新不计入insert接口最后返回的成功总条数.
- 当insert接口如上所述,触发了数据更新逻辑,本次更新字段和全局配置的策略(如not_null/not_empty)等均不生效,若您期望策略生效,可以调用update接口而非insert接口.
- 插入后如需id值可直接从entity中取,用法和mp中一致,批量插入亦可直接从原对象中获取插入成功后的数据id,以上接口返回integer为成功条数.
1.4.2.2 删除数据
integer deletebyid(serializable id);
integer deletebyid(serializable id, string... indexnames);
integer delete(lambdaesquerywrapper<t> wrapper);
integer deletebatchids(collection<? extends serializable> idlist);
integer deletebatchids(collection<? extends serializable> idlist, string... indexnames);
1.4.2.3 更新数据(不推荐)
integer updatebyid(t entity);
integer updatebyid(t entity, string... indexnames);
integer updatebatchbyids(collection<t> entitylist);
integer updatebatchbyids(collection<t> entitylist, string... indexnames);
integer update(t entity, lambdaesupdatewrapper<t> updatewrapper);
1.4.2.4 查询数据(推荐分页查询)
long selectcount(lambdaesquerywrapper<t> wrapper);
long selectcount(wrapper<t> wrapper, boolean distinct);
t selectbyid(serializable id);
t selectbyid(serializable id, string... indexnames);
list<t> selectbatchids(collection<? extends serializable> idlist);
list<t> selectbatchids(collection<? extends serializable> idlist, string... indexnames);
t selectone(lambdaesquerywrapper<t> wrapper);
list<t> selectlist(lambdaesquerywrapper<t> wrapper);
1.4.3 嵌套查询
@test
public void testnestedand() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.in(document::getstarnum, 1, 2)
.and(w -> w.eq(document::gettitle, "老汉").or().eq(document::gettitle, "推*"));
list<document> documents = documentmapper.selectlist(wrapper);
}
@test
public void testand(){
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.eq(document::gettitle, "老汉")
.match(document::getcontent, "推*");
list<document> documents = documentmapper.selectlist(wrapper);
}
@test
public void testnestedor() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.eq(document::getstarnum, 1)
.or(i -> i.eq(document::gettitle, "老汉").eq(document::getcreator, "糟老头子"));
list<document> documents = documentmapper.selectlist(wrapper);
}
@test
public void testor() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.eq(document::gettitle, "老汉")
.or()
.eq(document::gettitle, "痴汉");
list<document> documents = documentmapper.selectlist(wrapper);
}
@test
public void testnestedfilter() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.in(document::getstarnum, 1, 2)
.filter(w -> w.eq(document::gettitle, "老汉").or().eq(document::gettitle, "推*"));
list<document> documents = documentmapper.selectlist(wrapper);
}
@test
public void testfilter() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.filter().eq(document::gettitle, "老汉");
list<document> documents = documentmapper.selectlist(wrapper);
}
@test
public void testnestednot() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.eq(document::gettitle, "老汉")
.not(i->i.eq(size,18).eq(age,18));
list<document> documents = documentmapper.selectlist(wrapper);
}
@test
public void testnot() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.eq(document::gettitle, "老汉")
.not()
.eq(size,18);
list<document> documents = documentmapper.selectlist(wrapper);
}
1.4.4 链式调用
lambdaesindexchainwrapper<t> lambdachainindex(baseesmapper<t> baseesmapper);
lambdaesquerychainwrapper<t> lambdachainquery(baseesmapper<t> baseesmapper);
lambdaesupdatechainwrapper<t> lambdachainupdate(baseesmapper<t> baseesmapper);
@test
public void testone() {
document document = eswrappers.lambdachainquery(documentmapper).eq(document::gettitle, "隔壁老汉").one();
}
@test
public void testselectone() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.eq(document::gettitle, "隔壁老王")
.limit(1);
document document = documentmapper.selectone(wrapper);
}
1.5 拓展功能
1.5.1 混合查询
@test
public void testmix0(){
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
querybuilder querybuilder = querybuilders.matchquery("content", "推*").minimumshouldmatch("80%");
wrapper.eq(document::gettitle,"老汉").mix(querybuilder);
list<document> documents = documentmapper.selectlist(wrapper);
system.out.println(documents);
}
@test
public void testmix1() {
searchsourcebuilder searchsourcebuilder = new searchsourcebuilder();
searchsourcebuilder.query(querybuilders.matchquery("content", "推*").minimumshouldmatch("80%"));
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.setsearchsourcebuilder(searchsourcebuilder);
list<document> documents = documentmapper.selectlist(wrapper);
system.out.println(documents);
}
1.5.2 分页查询
espageinfo<t> pagequery(lambdaesquerywrapper<t> wrapper, integer pagenum, integer pagesize);
@test
public void testpagequery() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.match(document::gettitle, "老汉");
espageinfo<document> documentpageinfo = documentmapper.pagequery(wrapper,1,10);
system.out.println(documentpageinfo);
}
1.5.3 排序
wrapper.orderbydesc(排序字段,支持多字段)
wrapper.orderbyasc(排序字段,支持多字段)
wrapper.sortbyscore(boolean condition,sortorder sortorder)
1.5.4 分词 / 模糊匹配
@test
public void testmatch(){
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.match(document::getcontent,"技术");
list<document> documents = documentmapper.selectlist(wrapper);
system.out.println(documents.size());
}
@test
public void testmatchphrase() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.matchphrase(document::getcontent, "技术");
list<document> documents = documentmapper.selectlist(wrapper);
system.out.println(documents);
}
@test
public void testmatchallquery() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.matchallquery();
list<document> documents = documentmapper.selectlist(wrapper);
system.out.println(documents);
}
@test
public void testmultimatchquery() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.multimatchquery("老王", document::gettitle, document::getcontent, document::getcreator, document::getcustomfield);
list<document> documents = documentmapper.selectlist(wrapper);
system.out.println(documents.size());
system.out.println(documents);
}
1.5.5 条件过滤
@test
public void testfilter() {
lambdaesquerywrapper<document> wrapper = new lambdaesquerywrapper<>();
wrapper.filter().eq(document::gettitle, "老汉");
list<document> documents = documentmapper.selectlist(wrapper);
}
发表评论