• Elastic 开发篇 javaAPI(4)


    1、解决精确匹配问题,如果不加配置,搜索农大,会搜出“农”“大”这两个字的匹配,我们要的是“农大”,那么好了,加上一个条件搞定;

    MatchQuery.Type.PHRASE

    完整代码:

    package com.thinkgem.jeesite.modules.baffle.service;
    
    import com.thinkgem.jeesite.common.service.CrudService;
    import com.thinkgem.jeesite.common.utils.EsUtils;
    import com.thinkgem.jeesite.modules.ada.dao.ParamRecordExDao;
    import com.thinkgem.jeesite.modules.ada.entity.ParamRecordEx;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.text.Text;
    import org.elasticsearch.index.query.MultiMatchQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.index.search.MatchQuery;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
    import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
    import org.springframework.stereotype.Service;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    @Service
    public class ElasticDbService extends CrudService<ParamRecordExDao, ParamRecordEx> {
    
    
        public Map<String,Object> searchEsData(String query,int pageNum,int pageSize){
            Map<String, Object> map = new HashMap<String, Object>();
            long start=System.currentTimeMillis();
            try {
                TransportClient client= EsUtils.getTransportClient();
                MultiMatchQueryBuilder multiMatchQueryBuilder= QueryBuilders
                        .multiMatchQuery(query,"param_array","service_type")
                        .type(MatchQuery.Type.PHRASE)
                        ;
                HighlightBuilder highlightBuilder=new HighlightBuilder()
                        .preTags("<span style='color:red'>")
                        .postTags("</span>")
                        .field("param_array")
                        .field("service_type");
                SearchResponse searchResponse=client.prepareSearch("approveadapter")
                        // 匹配度高的排在前面
                        .setExplain(true)
                        .setTypes("input")
                        .setQuery(multiMatchQueryBuilder)
                        .highlighter(highlightBuilder)
                        .setFrom((pageNum-1)*pageSize)
                        .setSize(pageSize)
                        .execute()
                        .actionGet();
    
                SearchHits hits=searchResponse.getHits();
                ArrayList<Map<String,Object>> newsList=new ArrayList<Map<String, Object>>();
                for(SearchHit hit:hits){
                    Map<String,Object> news=hit.getSourceAsMap();
                    HighlightField highlightField=hit.getHighlightFields().get("param_array");
                    if(highlightField!=null){
                        Text[] fragments=highlightField.fragments();
                        String hText="";
                        for(Text text:fragments){
                            hText+=text;
                        }
                        news.put("param_array",hText);
                    }
    
                    HighlightField hServiceType=hit.getHighlightFields().get("service_type");
                    if(hServiceType!=null){
                        Text[] fragments1=hServiceType.fragments();
                        String hServiceText="";
                        for(Text text:fragments1){
                            hServiceText+=text;
                        }
                        news.put("service_type",hServiceText);
                    }
    
                  
                    newsList.add(news);
                }
                long end=System.currentTimeMillis();
    
                map.put("newsList",newsList);
                map.put("totalHits",String.valueOf(hits.getTotalHits()));
                map.put("totalTime",String.valueOf(end-start));
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return map;
        }
    }
    

      

    
    
    www.beicaiduo.com
  • 相关阅读:
    提高开发效率
    layer的alert图
    javascript改写 <select>checked的选中,并不会出发change事件,太好了!
    $.ajax 在请求没有完成,是可以往下继续执行js代码的
    解决 hibernate cannot define positional parameter after any named parameters have been defined
    el表达式便利map集合
    c/c++ 多线程 std::call_once的应用
    c/c++ 多线程 std::call_once
    c/c++ 多线程 unique_lock的使用
    c/c++ 多线程 层级锁
  • 原文地址:https://www.cnblogs.com/hoge66/p/10026425.html
Copyright © 2020-2023  润新知