Elastic 开发篇 javaAPI
Posted hoge
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elastic 开发篇 javaAPI相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章