elasticsearch搜索引擎查询java工具类

Elasticsearch查询java工具类

项目组使用Elasticsearch搜索引擎也有一段时间了,刚开始是2.4版本,现在又要在新的工程中使用,准备升级为5.4版本,以前的工具类也不好用了,没办法,只能简单的写一个构建查询语句的工具类。小弟自知功底不够,有问题的地方还请大佬多多批评指正。

注:由于所在公司特殊性,所以贴出的不是正在使用工程中的源码,而是我新建了一个工程去存放这些源码。

接下来贴上代码结构:

elasticsearch搜索引擎查询java工具类_第1张图片

最重要的是esbuilder中的文件,其中包括es四大查询模式构建工具类,一个查询模式定义文件,一个构建工厂类,一个最终拼装类。使用了简单工厂设计模式加反射。

下面是各个文件的具体代码:

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();
}

 

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;
    }
}

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;
    }
}

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;
    }
}

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;
    }
}

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;
    }
}

ESQueryBuilderFactory:

 

public class ESQueryBuilderFactory {

    public static IESQueryBuilders creatESQueryBuilder(ESQueryMode mode) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class clazz = Class.forName(mode.mode());
        return (IESQueryBuilders) clazz.newInstance();
    }
}

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(ESQuaryMode.MUST);
must.termQuery("termName", "value1","value1");
must.wildcardQuery("wildName","value1");

IESQueryBuilders should = ESQueryBuilderFactory.creatESQueryBuilder(ESQuaryMode.SHOULD);
should.wildcardQuery("wildName", "value1","value2");

BoolQueryBuilder boolQueryBuilder = esQueryProduce.getProduct(must).getProduct(should).getBoolQueryBuilder();

然后把最终生成的boolQueryBuilder放在SearchRequestBuilder中查询就可以了。

代码地址:https://github.com/wanglclx/EsSearchDemo

转载请注明出处:https://blog.csdn.net/wanglc7/article/details/80351551,谢谢。

 

你可能感兴趣的:(Elasticsearch,Elasticsearch,查询,QueryBuilder,工具类)