ESService

来自ling
跳转至: 导航搜索
package com.deloitte.tax.service;


import com.alibaba.druid.support.json.JSONUtils;
import com.deloitte.tax.model.PageUploadModel;
import com.deloitte.tax.ansj.AnsjTest;
import com.deloitte.tax.model.UpdatePageModel;
import com.deloitte.tax.pojo.PermissionRule;
import com.deloitte.tax.utils.FileParse;
import com.deloitte.tax.vo.GroupInfo;
import com.deloitte.tax.vo.MatchedRecord;
import com.deloitte.tax.vo.SearchResult;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpHost;

import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.*;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;

import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.index.query.functionscore.WeightBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;

import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.*;
import java.rmi.MarshalledObject;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.elasticsearch.index.query.QueryBuilders.prefixQuery;

@Service
public class ESService {

    private static Integer id = 200;
    private final Logger logger = LoggerFactory.getLogger(ESService.class);
    //private final static Integer SIZE = 10;
    public static RestHighLevelClient client = null;

    public static String ES_HOST;
    public static int ES_PORT;
    public static String ES_PROTOCAL;
    public static String ES_INDEX;
    public static String ES_TYPE;

    @Value("${elasticsearch.host}")
    public void setEsHost(String esHost) {
        ES_HOST = esHost;
    }

    @Value("${elasticsearch.port}")
    public void setEsPort(int esPort) {
        ES_PORT = esPort;
    }

    @Value("${elasticsearch.protocal}")
    public void setEsProtocal(String esProtocal) {
        ES_PROTOCAL = esProtocal;
    }

    @Value("${elasticsearch.index}")
    public void setEsIndex(String esIndex) {
        ES_INDEX = esIndex;
    }

    @Value("${elasticsearch.type}")
    public void setEsType(String esType) {
        ES_TYPE = esType;
    }

    @PostConstruct
    public static void init() {
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(ES_HOST, ES_PORT, ES_PROTOCAL)
                ));
    }


    public void cleanupNew() {
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Autowired
    private TaxSharePermissionsService taxSharePermissionsService;

    /**
     * @param fileName
     * @param inputStream
     * @throws Exception
     */
    public void createFileNew(String fileName, InputStream inputStream) throws Exception {
        logger.info("begin file:" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //读取文件内容
      /* int size = inputStream.available();
       byte[] buffer = new byte[size];
       inputStream.read(buffer);
       String str = new String(buffer,"UTF-8");*/
        //读取文件的纯文本内容
        String content = FileParse.getFileContentFromStream(inputStream);
        //提取文本的关键词
        AnsjTest ansjTest = new AnsjTest();
        String keywords = ansjTest.keyWordComputer(fileName, content);
        logger.info(keywords);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("uuidSP", "");
        jsonMap.put("title", fileName);
        jsonMap.put("keywords", keywords);
        jsonMap.put("data", content);
        jsonMap.put("type", 1);
        jsonMap.put("createdTime", sdf.format(new Date()));
        jsonMap.put("lastUpdateTime", sdf.format(new Date()));

        IndexRequest indexRequest = new IndexRequest(ES_INDEX, ES_TYPE, String.valueOf(id++))
                .source(jsonMap);
        //indexRequest.setPipeline("attachment");
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

        long version = indexResponse.getVersion();
        if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
            logger.info("created");
        } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
            logger.info("updated");
        }
        logger.info("end");
    }

    /**
     * @param model
     * @throws IOException
     */
    public void createPage(PageUploadModel model) throws IOException {
        logger.info("进入createPage()方法");
        logger.info("start");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createdTime = sdf.format(date);
        Map<String, Object> map = new HashMap<>();

        map.put("title", model.getTitle());
        map.put("data", model.getData());
        map.put("category", "P");
        map.put("createdTime", createdTime);
        map.put("lastUpdateTime", createdTime);
        map.put("link", model.getLink());

        //判断keywords字段是否为空
        if ("".equals(model.getKeywords())) {
            AnsjTest ansjTest = new AnsjTest();
            String keywords = ansjTest.keyWordComputer(model.getTitle(), model.getData());
            map.put("keywords", keywords);
        } else {
            map.put("keywords", model.getKeywords());
        }

        IndexRequest indexRequest = new IndexRequest(ES_INDEX, ES_TYPE)
                .source(map);
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
            logger.info("created");

        }
        logger.info("end");

    }

    private List<PrefixQueryBuilder> populateFilter() {
        List<PermissionRule> rules = taxSharePermissionsService.getPermissionsRules();
        List<PrefixQueryBuilder> prefixQueryBuilderList = new ArrayList<>();
        for (PermissionRule rule : rules) {
            if (rule.isVisiable() || rule.getUrl() == null || "page".equals(rule.getType())) {
                continue; //only process invisible
            }
            String urlPrefix = rule.getSpListPrefix();
            if (urlPrefix.trim().length() ==0){
                continue;
            }
            PrefixQueryBuilder prefixQueryBuilder= QueryBuilders.prefixQuery("link", urlPrefix);
            prefixQueryBuilderList.add(prefixQueryBuilder);
        }
        return prefixQueryBuilderList;
    }

    /**
     * @param keywords
     * @return
     * @throws IOException
     */
    //搜索的api
    public SearchResult searchByKey(String keywords, int currentPage,int searchType, Map<String, Object> map,int size) throws IOException {

        SearchResult searchResult = new SearchResult();
        //创建searchReq,指定索引为file_index
        SearchRequest searchRequest = new SearchRequest(ES_INDEX);
        //指定索引的类型
        searchRequest.types(ES_TYPE);
        //创建搜索参数
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //高亮搜索
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<b>");
        highlightBuilder.postTags("</b>");
        highlightBuilder.field("title").numOfFragments(0);
        highlightBuilder.field("data").numOfFragments(1);
        //设置没有匹配关键词返回的内容长度
        highlightBuilder.noMatchSize(200);
        //设置匹配关键词返回的内容长度
        highlightBuilder.fragmentSize(200);

        searchSourceBuilder.highlighter(highlightBuilder);

        //创建bool查询构建器
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();;

        //过滤权限为no的数据
//        BoolQueryBuilder noQueryBuilder = QueryBuilders.boolQuery();

        List<PrefixQueryBuilder> prefixList = populateFilter();
        if(prefixList != null) {
            for (PrefixQueryBuilder prefix : prefixList) {
                boolQueryBuilder.mustNot(prefix);
            }
        }
        boolQueryBuilder.mustNot(QueryBuilders.termQuery("category", "T"));
        //搜索条件
        //查询条件,or
        if (searchType == 1){
            MatchQueryBuilder firstMatchQueryBuilder = QueryBuilders.matchQuery("title", keywords).boost(5).minimumShouldMatch("-15%");
            MatchQueryBuilder secondMatchQueryBuilder = QueryBuilders.matchQuery("keywords", keywords).boost(5).minimumShouldMatch("-15%");
            MatchQueryBuilder thirdMatchQueryBuilder = QueryBuilders.matchQuery("data", keywords).minimumShouldMatch("-15%");

            //添加查询条件
            boolQueryBuilder.should(firstMatchQueryBuilder);
            boolQueryBuilder.should(secondMatchQueryBuilder);
            boolQueryBuilder.should(thirdMatchQueryBuilder);
        }else if(searchType == 2){
            MatchPhraseQueryBuilder firstPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("title", keywords).boost(5);
            MatchPhraseQueryBuilder secondPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("keywords", keywords).boost(5);
            MatchPhraseQueryBuilder thirdPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("data", keywords);
            //添加查询条件
            boolQueryBuilder.should(firstPhraseQueryBuilder);
            boolQueryBuilder.should(secondPhraseQueryBuilder);
            boolQueryBuilder.should(thirdPhraseQueryBuilder);
        }


        //创建postFilter的bool查询构建器
        BoolQueryBuilder postFilterBuilder = QueryBuilders.boolQuery();

        RangeQueryBuilder timeQueryBuilder = null;

        //创建聚合查询
        AggregationBuilder contentTypeTermsBuilder = AggregationBuilders.terms("docContentType").field("docContentType");
        AggregationBuilder industryTermsBuilder = AggregationBuilders.terms("docIndustry").field("docIndustry");
        //AggregationBuilder jurisdictionTermsBuilder = AggregationBuilders.terms("docJurisdiction").field("docJurisdiction");
        AggregationBuilder languageTermsBuilder = AggregationBuilders.terms("docLanguage").field("docLanguage");
        AggregationBuilder regionTermsBuilder = AggregationBuilders.terms("docRegion").field("docRegion");
        AggregationBuilder serviceLineTermsBuilder = AggregationBuilders.terms("docServiceLine").field("docServiceLine");
        //打分与发布时间相关的函数
        float number = 0.000000000386f;

        FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[2];

        //过滤条件1 当匹配的数据包含channel时,设置weight为2
        float weight = 3.5f;
        ScoreFunctionBuilder<WeightBuilder> scoreFunctionBuilder = new WeightBuilder();
        scoreFunctionBuilder.setWeight(weight);
        TermQueryBuilder channelFilter = QueryBuilders.termQuery("category", "P");
        FunctionScoreQueryBuilder.FilterFunctionBuilder channelFilterFunction = new FunctionScoreQueryBuilder.FilterFunctionBuilder(channelFilter, scoreFunctionBuilder);
        filterFunctionBuilders[0] = channelFilterFunction;

        //与时间关联
        FunctionScoreQueryBuilder.FilterFunctionBuilder timeFunctionBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(ScoreFunctionBuilders.fieldValueFactorFunction("lastUpdateTime")
                .modifier(FieldValueFactorFunction.Modifier.NONE).factor(number));
        filterFunctionBuilders[1] = timeFunctionBuilder;

        //多条件查询
        FunctionScoreQueryBuilder scoreQuery = QueryBuilders.functionScoreQuery(boolQueryBuilder, filterFunctionBuilders)
                .scoreMode(FunctionScoreQuery.ScoreMode.MULTIPLY)
                .boostMode(CombineFunction.SUM);

        searchSourceBuilder.query(scoreQuery).from(size * (currentPage - 1)).size(size);


        searchSourceBuilder.aggregation(contentTypeTermsBuilder);
        searchSourceBuilder.aggregation(industryTermsBuilder);
        searchSourceBuilder.aggregation(languageTermsBuilder);
        searchSourceBuilder.aggregation(regionTermsBuilder);
        searchSourceBuilder.aggregation(serviceLineTermsBuilder);
        //判断map是否为空
        if (!map.isEmpty()) {
            //获取当前时间
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = sdf.format(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if ("Past 24 Hours".equals(map.get("ModifiedDate"))) {
                //添加过滤条件
                logger.info("过去24小时");
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                String passTime = sdf.format(calendar.getTime());
                timeQueryBuilder = QueryBuilders.rangeQuery("lastUpdateTime")
                        .gte(passTime).lte(currentTime);
                postFilterBuilder.must(timeQueryBuilder);
            } else if ("Past Month".equals(map.get("ModifiedDate"))) {
                calendar.add(Calendar.MONTH, -1);
                String passTime = sdf.format(calendar.getTime());
                timeQueryBuilder = QueryBuilders.rangeQuery("lastUpdateTime")
                        .gte(passTime).lte(currentTime);
                postFilterBuilder.must(timeQueryBuilder);
            } else if ("Past Six Month".equals(map.get("ModifiedDate"))) {
                calendar.add(Calendar.MONTH, -6);
                String passTime = sdf.format(calendar.getTime());
                timeQueryBuilder = QueryBuilders.rangeQuery("lastUpdateTime")
                        .gte(passTime).lte(currentTime);
                postFilterBuilder.must(timeQueryBuilder);
            } else if ("Past Year".equals(map.get("ModifiedDate"))) {
                calendar.add(Calendar.YEAR, -1);
                String passTime = sdf.format(calendar.getTime());
                timeQueryBuilder = QueryBuilders.rangeQuery("lastUpdateTime")
                        .gte(passTime).lte(currentTime);
                postFilterBuilder.must(timeQueryBuilder);
            } else if ("Earlier".equals(map.get("ModifiedDate"))) {
                calendar.add(Calendar.YEAR, -1);
                String passTime = sdf.format(calendar.getTime());
                timeQueryBuilder = QueryBuilders.rangeQuery("lastUpdateTime")
                        .lte(passTime);
                postFilterBuilder.must(timeQueryBuilder);
            }
            //根据选中的分组创建查询条件
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if ("docContentType".equals(entry.getKey())) {
                    QueryBuilder postFilter = QueryBuilders.matchQuery("docContentType", entry.getValue());
                    postFilterBuilder.must(postFilter);
                } else if ("docIndustry".equals(entry.getKey())) {
                    QueryBuilder postFilter = QueryBuilders.matchQuery("docIndustry", entry.getValue());
                    postFilterBuilder.must(postFilter);
                }
//                else if ("docJurisdiction".equals(entry.getKey())) {
//                    QueryBuilder postFilter = QueryBuilders.matchQuery("docJurisdiction", entry.getValue());
//                    postFilterBuilder.must(postFilter);
//                }
                else if ("docLanguage".equals(entry.getKey())) {
                    if ("English".equals(entry.getValue()) || "Chinese".equals(entry.getValue())) {
                        QueryBuilder postFilter = QueryBuilders.matchQuery("docLanguage", entry.getValue());
                        postFilterBuilder.must(postFilter);
                    } else {
                        QueryBuilder postFilter1 = QueryBuilders.matchQuery("docLanguage", "English");
                        QueryBuilder postFilter2 = QueryBuilders.matchQuery("docLanguage", "Chinese");
                        postFilterBuilder.mustNot(postFilter1);
                        postFilterBuilder.mustNot(postFilter2);
                    }
                } else if ("docRegion".equals(entry.getKey())) {
                    QueryBuilder postFilter = QueryBuilders.matchQuery("docRegion", entry.getValue());
                    postFilterBuilder.must(postFilter);
                } else if ("docServiceLine".equals(entry.getKey())) {
                    if ("GBT".equals(entry.getValue())) {
                        QueryBuilder postFilter = QueryBuilders.matchQuery("docServiceLine", "Lead Tax Services");
                        postFilterBuilder.must(postFilter);
                    } else if ("ITMT".equals(entry.getValue())) {
                        QueryBuilder postFilter1 = QueryBuilders.matchQuery("docServiceLine", "International Tax");
                        QueryBuilder postFilter2 = QueryBuilders.matchQuery("docServiceLine", "Mergers & Acquisitions");
                        postFilterBuilder.should(postFilter1);
                        postFilterBuilder.should(postFilter2);
                    } else if ("TP".equals(entry.getValue())) {
                        QueryBuilder postFilter = QueryBuilders.matchQuery("docServiceLine", "Transfer Pricing");
                        postFilterBuilder.must(postFilter);
                    } else if ("GES".equals(entry.getValue())) {
                        QueryBuilder postFilter = QueryBuilders.matchQuery("docServiceLine", "Global Employer Services");
                        postFilterBuilder.must(postFilter);
                    } else if ("IT".equals(entry.getValue())) {
                        QueryBuilder postFilter = QueryBuilders.matchQuery("docServiceLine", "Indirect Tax");
                        postFilterBuilder.must(postFilter);
                    } else if ("LS".equals(entry.getValue())) {
                        QueryBuilder postFilter = QueryBuilders.matchQuery("docServiceLine", "Qin Li Law Firm");
                        postFilterBuilder.must(postFilter);
                    }
                }
            }
        }
        searchSourceBuilder.postFilter(postFilterBuilder);
        //按score降序排序
        searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
        // searchSourceBuilder.sort("postDate",SortOrder.DESC);
        //将搜索参数添加到searchRequest
        searchRequest.source(searchSourceBuilder);

        //同步执行
        SearchResponse searchResponse = client.search(searchRequest);
        RestStatus status = searchResponse.status();
        logger.info(status+"");

        SearchHits hits = searchResponse.getHits();
        SearchHit[] searchHits = hits.getHits();
        int count = (int) hits.getTotalHits();
        searchResult.setTotalCount(count);

        logger.info("searchHits.length" + searchHits.length + ", count:" +count);
        //接收匹配的数据结果

        MatchedRecord matchedRecord = null;
        for (SearchHit hit : searchHits) {

            matchedRecord = new MatchedRecord();

            Map<String, HighlightField> highlightFieldMap = hit.getHighlightFields();
            HighlightField highlightFileName = highlightFieldMap.get("title");
            HighlightField highlightData = highlightFieldMap.get("data");
            if (highlightData != null) {
                Text[] dataList = highlightData.fragments();
                for (Text data : dataList) {
                    matchedRecord.setData(data.string());
                }
            }

            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            String title = (String) sourceAsMap.get("title");
            String author = (String) sourceAsMap.get("docAuthor");
            String lastUpdateTime = (String) sourceAsMap.get("lastUpdateTime");
            String link = (String) sourceAsMap.get("link");
            String uuidSP = (String) sourceAsMap.get("uuidSP");
            String category = (String) sourceAsMap.get("category");
            String webUrl = (String) sourceAsMap.get("webUrl");
            String listUrl = (String) sourceAsMap.get("listUrl");
            String listId = (String) sourceAsMap.get("listId");
            String listItemId = (String) sourceAsMap.get("listItemId");

            float maxScore = hit.getScore();
//            logger.info(author);
            if (highlightFileName != null) {
                Text[] fileNameList = highlightFileName.fragments();
                for (Text str : fileNameList) {
                    matchedRecord.setTitle(str.string());
                }
            } else {
                matchedRecord.setTitle(title);
            }
            matchedRecord.setAuthor(author);
            matchedRecord.setLastUpdateTime(lastUpdateTime);
            matchedRecord.setLink(link);
            matchedRecord.setUuidSP(uuidSP);
            matchedRecord.setCategory(category);
            matchedRecord.setWebUrl(webUrl);
            matchedRecord.setListUrl(listUrl);
            matchedRecord.setListId(listId);
            matchedRecord.setListItemId(listItemId);

//            String suffixStr = matchedRecord.getLink()!=null && matchedRecord.getLink().indexOf("/")!=-1?matchedRecord.getLink()
//                    .substring(matchedRecord.getLink().lastIndexOf("/"),matchedRecord.getLink().length()):null;
//            if(suffixStr!=null && suffixStr.indexOf(".000")!=-1){
//                continue;
//            }

            searchResult.getArticleInfo().add(matchedRecord);
//            logger.info(maxScore);
        }

        //searchResult.setTotalCount(searchResult.getArticleInfo()!=null?searchResult.getArticleInfo().size():0);


        Aggregations terms = searchResponse.getAggregations();
        GroupInfo groupInfo = null;
        Map<String, Long> categorys = null;

        //接收导航栏数据
        for (Aggregation term : terms) {
            int i = 0;
            groupInfo = new GroupInfo();
            categorys = new HashMap<String, Long>();
            ParsedStringTerms stringTerms = (ParsedStringTerms) term;
            if ("docLanguage".equals(term.getName())) {
                long otherTotal = 0;
                for (Terms.Bucket bucket : stringTerms.getBuckets()) {
                    if ("English".equals(bucket.getKeyAsString()) || "Chinese".equals(bucket.getKeyAsString())) {
                        categorys.put(bucket.getKeyAsString(), bucket.getDocCount());
                    } else {
                        otherTotal += bucket.getDocCount();
                    }
                }
                categorys.put("Other", otherTotal);
            } else if ("docContentType".equals(term.getName())) {
                for (Terms.Bucket bucket : stringTerms.getBuckets()) {
                    categorys.put(bucket.getKeyAsString(), bucket.getDocCount());
                }
            } else {
                for (Terms.Bucket bucket : stringTerms.getBuckets()) {
                    categorys.put(bucket.getKeyAsString(), bucket.getDocCount());
                    i++;
                    if (i == 5) {
                        break;
                    }
                }
            }
            groupInfo.setGroupName(term.getName());
            groupInfo.getCategoryInfos().putAll(categorys);
            searchResult.getMenuInfo().add(groupInfo);
        }
        return searchResult;
    }

    /**
     * @return
     */
    public JSONArray getAllPageMessage() throws IOException {
        JSONArray jsonPage = new JSONArray();
        //创建searchrequest对象,并指定索引
        SearchRequest searchRequest = new SearchRequest(ES_INDEX);
        //设置索引的类型
        searchRequest.types(ES_TYPE);
        //创建搜索条件对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询条件
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "P");
        //创建bool对象
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        boolQueryBuilder.must(termQueryBuilder);

        searchSourceBuilder.query(boolQueryBuilder);
        //设置按照上传时间降序排列
        searchSourceBuilder.sort("lastUpdateTime", SortOrder.DESC);
        //设置源过滤
        String[] includeFields = new String[]{"title", "keywords", "createdTime", "lastUpdateTime", "link"};
        String[] excludeFields = new String[]{"uuidSp", "data"};
        searchSourceBuilder.fetchSource(includeFields, excludeFields).from(0).size(240);

//        searchSourceBuilder.from(0);
//        searchSourceBuilder.size(10);
        searchRequest.source(searchSourceBuilder);

        //同步执行
        SearchResponse searchResponse = client.search(searchRequest);
        RestStatus status = searchResponse.status();

        logger.info(status+"");

        SearchHits hits = searchResponse.getHits();
        SearchHit[] hitsArray = hits.getHits();
        logger.info(hitsArray.length+"");

        JSONObject jsonObject = null;
        for (SearchHit hit : hitsArray) {
            jsonObject = new JSONObject();
            Map<String, Object> data = hit.getSourceAsMap();
            String title = (String) data.get("title");
            String keywords = (String) data.get("keywords");
            String createdTime = (String) data.get("createdTime");
            String lastUpdateTime = (String) data.get("lastUpdateTime");
            String link = (String) data.get("link");

            PageUploadModel model = new PageUploadModel();
            model.setTitle(title);
            model.setKeywords(keywords);
            model.setLink(link);

            //createPage(model);

            jsonObject.put("id", hit.getId());
            jsonObject.put("title", title);
            jsonObject.put("keywords", keywords);
            jsonObject.put("createdTime", createdTime);
            jsonObject.put("lastUpdateTime", lastUpdateTime);
            jsonObject.put("link", link);
            jsonPage.add(jsonObject);
        }
        logger.info(jsonPage+"");
        return jsonPage;
    }

    /**
     * @param id
     * @return
     */
    public JSONObject getDataById(String id) throws IOException {
        logger.info("获取当前页面的信息");

        GetRequest getRequest = new GetRequest(ES_INDEX, ES_TYPE, id);

        //同步执行
        GetResponse getResponse = client.get(getRequest);
        JSONObject pageJson = new JSONObject();
        if (getResponse.isExists()) {
            Map<String, Object> page = getResponse.getSourceAsMap();
            pageJson.put("id", id);
            pageJson.put("title", page.get("title"));
            pageJson.put("keywords", page.get("keywords"));
            pageJson.put("data", page.get("data"));
            pageJson.put("createdTime", page.get("createdTime"));
            pageJson.put("link", page.get("link"));
        } else {
            logger.info("更新的数据不存在");
        }
        logger.info(JSONUtils.toJSONString(pageJson));
        return pageJson;
    }

    /**
     * @param model
     * @throws IOException
     */
    public void updatePage(UpdatePageModel model) throws IOException {
        logger.info("更新页面!");

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastUpdateTime = sdf.format(date.getTime());
        UpdateRequest updateRequest = new UpdateRequest(ES_INDEX, ES_TYPE, model.getId());

        Map<String, Object> map = new HashMap<>();
        map.put("uuidSP", "");
        map.put("title", model.getTitle());
        map.put("keywords", model.getKeywords());
        map.put("data", model.getData());
        map.put("channel", 2);
        map.put("createdTime", model.getCreatedTime());
        map.put("lastUpdateTime", lastUpdateTime);
        map.put("link", model.getLink());

        UpdateResponse updateResponse = client.update(updateRequest.doc(map));
        if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
            logger.info("updated");
        }
    }

    public void deleteById(String id) throws IOException {
        logger.info("删除页面!");
        DeleteRequest deleteRequest = new DeleteRequest(ES_INDEX, ES_TYPE, id);
        DeleteResponse deleteResponse = client.delete(deleteRequest);
        if (deleteResponse.getResult() == DocWriteResponse.Result.DELETED) {
            logger.info("delete");
        }
    }


}