首页 > 编程学习 > Elasticsearch 6.1.2 (二)分页查询、排序、关键词查询,集合beetl实现前台展示
20181 月21

Elasticsearch 6.1.2 (二)分页查询、排序、关键词查询,集合beetl实现前台展示

后台代码,自定义tag:

package cn.wizzer.app.web.modules.tags;

import cn.wizzer.app.web.commons.ex.elasticsearch.EsService;
import cn.wizzer.app.web.commons.utils.YcDateUtil;
import cn.wizzer.app.ycold.modules.services.YcoldInquiryService;
import cn.wizzer.framework.page.Pagination;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.beetl.core.GeneralVarTagBinding;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wizzer on 2018/1/20.
 */
@IocBean
public class YcoldInquiryListTag extends GeneralVarTagBinding {
    private final static Log log = Logs.get();
    @Inject
    private EsService esService;
    @Inject
    private YcoldInquiryService ycoldInquiryService;
    @Inject
    private PropertiesProxy cfg;

    @Override
    public void render() {
        String startDate = Strings.sNull(this.getAttributeValue("startDate"));
        String endDate = Strings.sNull(this.getAttributeValue("endDate"));
        String keyword = Strings.sNull(this.getAttributeValue("keyword"));
        int pageNumber = NumberUtils.toInt(Strings.sNull(this.getAttributeValue("pageNumber")), 1);
        int pageSize = NumberUtils.toInt(Strings.sNull(this.getAttributeValue("pageSize")), 10);
        boolean highlight = BooleanUtils.toBoolean(Strings.sNull(this.getAttributeValue("highlight")));
        boolean explain = BooleanUtils.toBoolean(Strings.sNull(this.getAttributeValue("explain")));
        String sortName = Strings.sNull(this.getAttributeValue("sortName"));
        String sortOrder = Strings.sNull(this.getAttributeValue("sortOrder"));
        Pagination page = new Pagination();
        page.setPageNo(pageNumber);
        page.setPageSize(pageSize);
        try {
            BoolQueryBuilder query = QueryBuilders.boolQuery();
            //根据名称查询
            if (Strings.isNotBlank(keyword)) {
                query.must(QueryBuilders.wildcardQuery("CASNM", "*" + keyword + "*"));
            }
            //截止时间大于等于现在
            query.must(QueryBuilders.rangeQuery("IQDAT").gte(Times.format("yyyyMMddHHmmss", new Date())));
            //公共日期起
            if (Strings.isNotBlank(startDate)) {
                query.must(QueryBuilders.rangeQuery("ANNODAT").gte(startDate.replaceAll("-","")));
            }
            //公共日期至
            if (Strings.isNotBlank(endDate)) {
                query.must(QueryBuilders.rangeQuery("ANNODAT").lte(endDate.replaceAll("-","")));
            }
            //几个状态条件
            query.must(QueryBuilders.matchQuery("BUYER_STS", "N"));//采购商状态
            query.must(QueryBuilders.matchQuery("STS", "A"));//状态
            query.must(QueryBuilders.matchQuery("ANNOMK", "Y"));//公告註記
            SearchRequestBuilder srb = esService.getClient().prepareSearch(cfg.get("es.index.name"))
                    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                    .setTypes("inquiry")
                    .setQuery(query)
                    //分页
                    .setFrom((pageNumber - 1) * pageSize).setSize(pageSize)
                    //是否按匹配度排序
                    .setExplain(explain);

            if (highlight) {
                HighlightBuilder highlightBuilder = new HighlightBuilder().field("*").requireFieldMatch(false);
                highlightBuilder.preTags("<span style=\"color:red\">");
                highlightBuilder.postTags("</span>");
                srb.highlighter(highlightBuilder);
            }
            if (Strings.isNotBlank(sortName)) {
                String[] sortNames = StringUtils.split(sortName, ",");
                if ("asc".equalsIgnoreCase(sortOrder)) {
                    for (String s : sortNames) {
                        srb.addSort(s, SortOrder.ASC);
                    }
                } else {
                    for (String s : sortNames) {
                        srb.addSort(s, SortOrder.DESC);
                    }
                }
            }
            log.debug("srb:::\r\n" + srb.toString());

            SearchResponse response = srb.execute().actionGet();
            SearchHits hits = response.getHits();
            page.setTotalCount((int) hits.getTotalHits());
            List<Map<String, Object>> list = new ArrayList<>();
            hits.forEach(searchHit -> {
                Map<String, Object> source = searchHit.getSourceAsMap();
                Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
                //name高亮
                HighlightField nameField = highlightFields.get("CASNM");
                if (nameField != null) {
                    Text[] fragments = nameField.fragments();
                    String tmp = "";
                    for (Text text : fragments) {
                        tmp += text;
                    }
                    source.put("CASNM", tmp);
                }
                source.put("IQDAT", YcDateUtil.get_yyyyMMdd_HHmm(Strings.sNull(source.get("IQDAT"))));
                source.put("ANNODAT", YcDateUtil.get_yyyyMMdd(Strings.sNull(source.get("ANNODAT"))));
                list.add(source);
            });
            page.setList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.binds(page);
        this.doBodyRender();
    }
}

前台beetl页面:

  <#ycold_inquiry_list pageNumber="${pageNumber}" pageSize="${pageSize}" startDate="${startDate}" endDate="${endDate}" keyword="${keyword}" sortName="ANNODAT" sortOrder="desc" highlight="false" var="p">

    <table class="list_table_blue even_table">
        <thead>
        <tr>
            <th>公告日期</th>
            <th>案件名称</th>
            <th>交货地点</th>
            <th>报价截止日期</th>
            <th width="110">查看详细</th>
        </tr>
        </thead>
        <tbody>
        <%for(o in p.list){%>
        <tr>
            <td>${o.ANNODAT!}</td>
            <td class="l_text"><a href="${base!}/purchase/info/${o.XUID!}" target="_blank" class="td_a hide1">${o.CASNM!}</a>
            </td>
            <td class="l_text">${o.DLSITE!}</td>
            <td>${o.IQDAT!}</td>
            <td><a href="${base!}/purchase/info/${o.XUID!}" target="_blank" class="more_a png"></a></td>
        </tr>
        <%}%>
        </tbody>
    </table>
    <%if(p.totalCount>1){%>
    <div class="page round_s_a"></div>
    <script type="text/javascript">
        $(function () {
            $(".page").createPage({
                pageCount: ${p.totalPage},
                totalCount: ${p.totalCount},
                current: ${p.pageNo},
                backFn: function (p) {
                    window.location.href = "?page=" + p + "&size=${p.pageSize}";
                }
            });
        });
    </script>
    <%}%>

本文地址:https://wizzer.cn/archives/3387 , 转载请保留.

本文目前尚无任何评论.

发表评论