• 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
  • 相关阅读:
    Mysql事务的隔离级别,ACID以及三要素的取舍(强一致性弱一致性)、ABA问题
    符合python风格的对象
    python重载运算符之左add右add(radd)
    pycham快捷键
    设置linux时间同步
    描述克隆linux的步骤,以及后续配置
    linux命令
    ransientException: find no Route:SELECT * FROM `ego`.`tb_content_category` LIMIT 0,1000‘
    linux修改vim /etc/profile 配置文件 修改不了
    web.xml
  • 原文地址:https://www.cnblogs.com/hoge66/p/10026425.html
Copyright © 2020-2023  润新知