All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.es.plus.samples.service.SamplesEsService Maven / Gradle / Ivy

There is a newer version: 0.3.7
Show newest version
package com.es.plus.samples.service;

import com.es.plus.adapter.params.EsHit;
import com.es.plus.adapter.params.EsHits;
import com.es.plus.adapter.params.EsResponse;
import com.es.plus.core.service.EsServiceImpl;
import com.es.plus.core.statics.Es;
import com.es.plus.core.wrapper.aggregation.EsAggWrapper;
import com.es.plus.core.wrapper.chain.EsChainLambdaQueryWrapper;
import com.es.plus.core.wrapper.chain.EsChainQueryWrapper;
import com.es.plus.core.wrapper.core.EsLambdaQueryWrapper;
import com.es.plus.core.wrapper.core.EsLambdaUpdateWrapper;
import com.es.plus.core.wrapper.core.EsWrapper;
import com.es.plus.es7.client.EsPlusAggregations;
import com.es.plus.samples.dto.SamplesEsDTO;
import com.es.plus.samples.dto.SamplesNestedDTO;
import com.es.plus.samples.dto.SamplesNestedInnerDTO;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.index.query.InnerHitBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Service
public class SamplesEsService extends EsServiceImpl {
    
    /**
     * 三级嵌套查询,如果不使用框架用原生查询将会非常复杂
     */
    public void nested() {
        //获取二级查询条件
        Consumer> innerConsumer = getSamplesNestedConsumer();
        // 声明语句嵌套关系是must
        InnerHitBuilder innerHitBuilder = new InnerHitBuilder("test");
        innerHitBuilder.setSize(10);
        innerHitBuilder.setFetchSourceContext(new FetchSourceContext(false));

 
        //一级查询条件
        EsChainLambdaQueryWrapper queryWrapper = esChainQueryWrapper().must().fetch(false)
                //二级
                .nestedQuery(SamplesEsDTO::getSamplesNesteds, SamplesNestedDTO.class,
                        (esQueryWrap) -> {
                            esQueryWrap.must().term(SamplesNestedDTO::getUsername, "3");
                            InnerHitBuilder innerHitBuilder1 = new InnerHitBuilder();
                            innerHitBuilder1.setSize(100);
                            //三级
                            esQueryWrap.must().nestedQuery(SamplesNestedDTO::getSamplesNestedInner, SamplesNestedInnerDTO.class,
                                    (innerQuery) -> {
                                        innerQuery.must().term(SamplesNestedInnerDTO::getUsername, 3)
                                        .term(SamplesNestedInnerDTO::getState,true);
                                    }, ScoreMode.None, innerHitBuilder1);
                        }, ScoreMode.None,innerHitBuilder);


        EsResponse esResponse = queryWrapper.search();
        EsHits innerHits = esResponse.getInnerHits();
        List esHitList = innerHits.getEsHitList();
        for (EsHit esHit : esHitList) {
            long innerHitsTotal = esHit.getInnerHitsTotal("test");
            EsHits esHitEsHits = esHit.getEsInnerHits("test");
            List test = esHit.getInnerList(SamplesNestedDTO.class, "test");
            for (EsHit hit : esHitEsHits.getEsHitList()) {
                List list = hit.getInnerList(SamplesNestedInnerDTO.class,"samplesNesteds.samplesNestedInner");
                System.out.println();
            }
            System.out.println();
        }
        // 查询
        List list = esResponse.getList();
        System.out.println(list);
    }

    /**
     *  获取二级嵌套查询对象
     */
    private Consumer> getSamplesNestedConsumer() {
        Consumer> innerConsumer = (esQueryWrap) -> {
            esQueryWrap.must().term(SamplesNestedDTO::getUsername, "3");
            InnerHitBuilder innerHitBuilder1 = new InnerHitBuilder();
            innerHitBuilder1.setSize(100);
            Consumer> innerInnerConsumer = skuQueryWrapper();
            esQueryWrap.must().nestedQuery(SamplesNestedDTO::getSamplesNestedInner, SamplesNestedInnerDTO.class,
                    innerInnerConsumer, ScoreMode.None, innerHitBuilder1);
        };
        return innerConsumer;
    }

    /**
     *  获取三级嵌套查询对象
     */
    private Consumer> skuQueryWrapper() {
        Consumer> innerInnerConsumer = (innerQuery) -> {
            innerQuery.must().term(SamplesNestedInnerDTO::getUsername, 3);
        };
        return innerInnerConsumer;
    }


    public void search() {
        // 声明语句嵌套关系是must
        EsResponse esResponse = esChainQueryWrapper().mustNot()
                .wildcard(SamplesEsDTO::getUsername, "*g那好好好好好好好好好好好好哈哈哈哈哈哈哈哈人兔兔兔兔兔兔兔兔吞吞吐吐他吞吞吐吐vvvvvvvvvvvvvvvvvvv请问额额额GG古古怪怪滚滚滚刚刚扭扭捏捏那你哈哈哈哈哈好哈哈哈哈哈哈哈哈应用*")
                .term(SamplesEsDTO::getEmail, "bbbbbb")
                // 多个must嵌套
//                .must(a ->
//                        // 声明内部语句关系的should
//                        a.must()
//                                .term(SamplesEsDTO::getNickName, "dasdsad")
//                                .term(SamplesEsDTO::getPhone, "1386859111"))
                .search();
//        EsResponse list2 = esChainQueryWrapper().list();
//        System.out.println(list2);
        List list = esResponse.getList();
        System.out.println(list);
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        EsChainQueryWrapper term = Es.chainQuery(Map.class).index("sys_user2ttt_alias").must()
                .match("username", "HZH").term("email", "abc");
        term.esAggWrapper().terms("keyword");
        EsResponse list1 = term.search();
//        Map username1 = list1.getEsAggsResponse().getTermsAsMap("keyword");
//        System.out.println(username1);
    }


    public void agg() {

        EsResponse dd = esChainQueryWrapper().must().match(SamplesEsDTO::getUsername, "dd").search();
        EsAggWrapper username = esChainQueryWrapper().esAggWrapper().terms("username");

        // 声明语句嵌套关系是must
        EsChainLambdaQueryWrapper esChainQueryWrapper = esChainQueryWrapper().must()
                .ge(SamplesEsDTO::getId, 1);
        esChainQueryWrapper.esLambdaAggWrapper()
                // terms聚合并且指定数量10000
                .filter(SamplesEsDTO::getUsername, ()-> {
                      EsWrapper esWrapper = esChainQueryWrapper();
                      return esWrapper;
                })
                .terms(SamplesEsDTO::getUsername, a -> a.size(1000))
                // 在terms聚合的基础上统计lock数量
                .subAggregation(t -> t.sum(SamplesEsDTO::getId));
        EsResponse esResponse = esChainQueryWrapper
                // 查询
                .search();
        List list = esResponse.getList();

        EsPlusAggregations esAggsResponse = (EsPlusAggregations) esResponse.getEsAggsResponse();
//        Aggregations aggregations = esAggsResponse.getAggregations();
//        Map asMap = aggregations.getAsMap();
//        Map map = JsonUtils.beanToMap(asMap);
//        System.out.println(map);
//        Terms terms = esAggsResponse.getTerms(SamplesEsDTO::getUsername);
//        Map termsAsMap = esAggsResponse.getTermsAsMap(SamplesEsDTO::getUsername);
    }

    public void profile1() {
        // 声明语句嵌套关系是must
        EsResponse esResponse = esChainQueryWrapper().must()
                .terms(SamplesEsDTO::getUsername, "admin", "hzh", "shi")
                // 多个must嵌套
                .must(a ->
                        // 声明内部语句关系的should
                        a.must()
                                .term(SamplesEsDTO::getNickName, "dasdsad")
                                .term(SamplesEsDTO::getPhone, "1386859111")).profile();
        System.out.println(esResponse);
    }

    public void test() {
        esChainQueryWrapper().must().match(SamplesEsDTO::getSamplesNesteds, "hzh")
                .term(true, SamplesEsDTO::getEmail, null);

        System.out.println();
    }

    public void scroll() {
        String scrollId = null;
        int page = 3;
        int size = 2;

        for (int i = 0; i < page; i++) {
            EsResponse hzh = esChainQueryWrapper().must()
                    .sortByAsc("id").scroll(size, scrollId);
            scrollId = hzh.getScrollId();
            System.out.println(hzh);
        }
    }

    public void count() {
        long hzh = this.count(null);

        System.out.println(hzh);
    }

    public void update() {
        Map map = new HashMap<>();
        map.put("username", "fsdfsfds");
        map.put("id", "d73d1b4e46244b0db766987759d6e");
        Es.chainUpdate(Map.class).index("sys_user2ttt").save(map);
    }

    public void newSelect() {
        EsResponse hzh = Es.chainLambdaQuery(SamplesEsDTO.class).term(SamplesEsDTO::getUsername, "hzh").search();
        System.out.println(hzh);
    }

    public void searhAfter() {
//        PageInfo pageInfo = new PageInfo<>();
//        pageInfo.setSize(3);

//        EsResponse samplesEsDTOEsResponse = Es.chainLambdaQuery(SamplesEsDTO.class)
//                .orderBy("asc", SamplesEsDTO::getId).searchAfter(null);
//
//
//        pageInfo.setSearchAfterValues(samplesEsDTOEsResponse.getTailSortValues());
//        EsResponse samplesEsDTOEsResponse1 = Es.chainLambdaQuery(SamplesEsDTO.class)
//                .orderBy("DESC", SamplesEsDTO::getId).searchAfter(null);
//
//        System.out.println(samplesEsDTOEsResponse);
//
//        System.out.println(samplesEsDTOEsResponse1);

    }


    public void updateByQuery() {
        EsLambdaUpdateWrapper updateWrapper = new EsLambdaUpdateWrapper<>();
        updateWrapper.match(SamplesEsDTO::getUsername, "ggghhh").set(SamplesEsDTO::getEmail, "bbbbbb");
        this.updateByQuery(updateWrapper);
        
        
        EsLambdaQueryWrapper queryWrapper = new EsLambdaQueryWrapper<>();
        queryWrapper.match(SamplesEsDTO::getUsername, "ggghhh");
        EsResponse list = this.search(queryWrapper);
        System.out.println(list);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy