最近项目组需要用到Elasticsearch,封装了ESQueryBuilder方便条件组合,结合ES工具类使用效果更佳,下面是ES工具类传送门:
SpringBoot Elasticsearch工具类封装
本文包括ES四大查询模式构建工具类,一个查询模式定义类,一个构建工厂类,一个最终拼装类。使用了简单工厂设计模式加反射,特此记录便于日后查阅。
1、代码结构
2、maven依赖
org.elasticsearch.plugin
transport-netty4-client
6.7.2
3、IESQueryBuilders
public interface IESQueryBuilders {
IESQueryBuilders termQuery(String name, String... valve) throws Exception;
IESQueryBuilders wildcardQuery(String name, String... values) throws Exception;
IESQueryBuilders rangeQuery(String name, String from, String to) throws Exception;
BoolQueryBuilder getBoolQueryBuilder();
}
4、ESMust
public class ESMust implements IESQueryBuilders {
private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
@Override
public BoolQueryBuilder getBoolQueryBuilder() {
return boolQueryBuilder;
}
@Override
public ESMust termQuery(String name, String... values) throws Exception {
boolQueryBuilder.must(QueryBuilders.termsQuery(name, values));
return this;
}
@Override
public ESMust wildcardQuery(String name, String... values) throws Exception{
for (String value : values){
boolQueryBuilder.must(QueryBuilders.wildcardQuery(name, "*" + value + "*"));
}
return this;
}
@Override
public ESMust rangeQuery(String name, String from, String to) throws Exception {
boolQueryBuilder.must(QueryBuilders.rangeQuery(name).from(from, true).to(to, true));
return this;
}
}
5、ESMustNot
public class ESMustNot implements IESQueryBuilders {
private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
@Override
public BoolQueryBuilder getBoolQueryBuilder() {
return boolQueryBuilder;
}
@Override
public ESMustNot termQuery(String name, String... values) throws Exception {
boolQueryBuilder.mustNot(QueryBuilders.termQuery(name, values));
return this;
}
@Override
public ESMustNot wildcardQuery(String name, String... values) throws Exception {
for (String value : values){
boolQueryBuilder.mustNot(QueryBuilders.wildcardQuery(name, "*" + value + "*"));
}
return this;
}
@Deprecated
public ESMustNot rangeQuery(String name, String from, String to) throws Exception {
return null;
}
}
6、ESShould
public class ESShould implements IESQueryBuilders {
private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
@Override
public BoolQueryBuilder getBoolQueryBuilder() {
return boolQueryBuilder;
}
@Override
public ESShould termQuery(String name, String... values) throws Exception {
boolQueryBuilder.should(QueryBuilders.termsQuery(name, values));
return this;
}
@Override
public ESShould wildcardQuery(String name, String... values) throws Exception {
for (String value : values){
boolQueryBuilder.should(QueryBuilders.wildcardQuery(name, "*" + value + "*"));
}
return this;
}
@Deprecated
public ESShould rangeQuery(String name, String from, String to) throws Exception {
return null;
}
}
7、ESFilter
public class ESFilter implements IESQueryBuilders {
private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
@Override
public BoolQueryBuilder getBoolQueryBuilder() {
return boolQueryBuilder;
}
@Override
public ESFilter termQuery(String name, String... values) throws Exception {
boolQueryBuilder.filter(QueryBuilders.termsQuery(name, values));
return this;
}
@Override
public ESFilter wildcardQuery(String name, String... values) throws Exception {
for (String value : values){
boolQueryBuilder.filter(QueryBuilders.wildcardQuery(name, "*" + value + "*"));
}
return this;
}
@Deprecated
public ESFilter rangeQuery(String name, String from, String to) throws Exception {
return null;
}
}
8、ESQueryMode
public enum ESQueryMode implements Serializable{
MUST("com.example.esbuilder.ESMust"),
MUST_NOT("com.example.esbuilder.ESMustNot"),
SHOULD("com.example.esbuilder.ESShould"),
FILTER("com.example.esbuilder.ESFilter");
private String mode;
ESQueryMode(String mode) {
this.mode = mode;
}
public String mode() {
return mode;
}
}
9、ESQueryBuilderFactory
public class ESQueryBuilderFactory {
public static IESQueryBuilders creatESQueryBuilder(ESQueryMode mode) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class> clazz = Class.forName(mode.mode());
return (IESQueryBuilders) clazz.newInstance();
}
}
10、IESQueryProduce
/**
* @Classname: com.openailab.oascloud.datacenter.common.build.IESQueryProduce
* @Description: ES组装器
* @Author: zxzhang
* @Date: 2019/7/9
*/
public interface IESQueryProduce {
/**
* 获取构造器
*
* @param
* @return org.elasticsearch.index.query.BoolQueryBuilder
* @author zxzhang
* @date 2019/7/9
*/
BoolQueryBuilder getBoolQueryBuilder();
/**
* 获取组装器
*
* @param esQueryBuilders
* @return com.openailab.oascloud.datacenter.common.build.IESQueryProduce
* @author zxzhang
* @date 2019/7/9
*/
IESQueryProduce getProduct(IESQueryBuilders esQueryBuilders);
}
11、ESQueryProduce
public class ESQueryProduce implements IESQueryProduce {
private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
@Override
public BoolQueryBuilder getBoolQueryBuilder() {
return boolQueryBuilder;
}
@Override
public IESQueryProduce getProduct(IESQueryBuilders esQueryBuilders) {
boolQueryBuilder.must(esQueryBuilders.getBoolQueryBuilder());
return this;
}
}
然后就能自由的拼接复杂的es查询语句了。
比如:
IESQueryBuilders must = ESQueryBuilderFactory.creatESQueryBuilder(ESQueryMode.MUST);
must.termQuery("termName", "value1","value1");
must.wildcardQuery("wildName","value1");
IESQueryBuilders should = ESQueryBuilderFactory.creatESQueryBuilder(ESQueryMode.SHOULD);
should.wildcardQuery("wildName", "value1","value2");
BoolQueryBuilder boolQueryBuilder = esQueryProduce.getProduct(must).getProduct(should).getBoolQueryBuilder();
然后把最终生成的boolQueryBuilder放在SearchRequestBuilder中查询就可以了。