码迷,mamicode.com
首页 > 编程语言 > 详细

Elastic 开发篇 javaAPI(4)

时间:2018-11-27 17:12:45      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:tag   slist   think   frame   index   package   put   explain   ram   

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;
    }
}

  

 

Elastic 开发篇 javaAPI(4)

标签:tag   slist   think   frame   index   package   put   explain   ram   

原文地址:https://www.cnblogs.com/hoge66/p/10026425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!