注意:此文是解决springboot 2.x通过spring-boot-starter-data-elasticsearch整合elasticsearch 6.x实现高亮highlighter解决办法。
参考官方文档:
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/
public interface ElasticRepository extends ElasticsearchRepository{
/**
* @Query方法也无法实现高亮
* @param content
* @param pageable
* @return
*/
@Query("{\"bool\" : {\"must\" : {\"match\" : {\"content\" : \"?0\"}}}}," +
"\"highlight\": {\"fields\" :{ \"content\" : {}}}")
Page findByContent(String content, Pageable pageable);
}
@Transactional(propagation= Propagation.REQUIRED,isolation= Isolation.DEFAULT)
@Service("elasticService")
public class ElasticServiceImpl implements ElasticService {
@Autowired
@Qualifier("elasticRepository")
private ElasticRepository elasticRepository;
@Override
public Page findByContent(String content, Integer pageNum, Integer pageSize) {
Pageable pageable = PageRequest.of(pageNum - 1,pageSize);
return elasticRepository.findByContent(content,pageable);
}
}
该方案可正常检索数据,但是不能高亮。
@Transactional(propagation= Propagation.REQUIRED,isolation= Isolation.DEFAULT)
@Service("elasticService")
public class ElasticServiceImpl implements ElasticService {
/**
* 此方法主要说明,不能进行复杂的高亮查询
* @param key
* @param pageNum
* @param pageSize
* @return
*/
@Override
public Page highlightQuery(String key,Integer pageNum,Integer pageSize) {
System.out.println(key+","+pageNum+","+pageSize);
//高亮显示规则
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("");
highlightBuilder.postTags("");
highlightBuilder.field("fileName");
highlightBuilder.field("category");
highlightBuilder.field("content");
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.multiMatchQuery(key,"fileName","category","content"))
.withHighlightBuilder(highlightBuilder)
.withPageable(PageRequest.of(pageNum - 1,pageSize))
.build();
System.out.println("查询的语句:" + searchQuery.getQuery().toString());
return elasticRepository.search(searchQuery);
}
}
其中elasticRepository.search(searchQuery)方法是ElasticsearchRepository接口(本文中ElasticServiceImpl 的父接口)中的方法。
@NoRepositoryBean
public interface ElasticsearchRepository extends ElasticsearchCrudRepository {
S index(S entity);
Iterable search(QueryBuilder query);
Page search(QueryBuilder query, Pageable pageable);
Page search(SearchQuery searchQuery);
Page searchSimilar(T entity, String[] fields, Pageable pageable);
void refresh();
Class getEntityClass();
}
出现异常
java.lang.NullPointerException
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.doSearch(ElasticsearchTemplate.java:914)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForPage(ElasticsearchTemplate.java:314)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForPage(ElasticsearchTemplate.java:309)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForPage(ElasticsearchTemplate.java:139)
at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.search(AbstractElasticsearchRepository.java:214)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy63.search(Unknown Source)
at com.cntaiping.tpa.db.service.impl.ElasticServiceImpl.highlightQuery(ElasticServiceImpl.java:147)
at com.cntaiping.tpa.db.service.impl.ElasticServiceImpl$$FastClassBySpringCGLIB$$74187366.invoke()
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.cntaiping.tpa.db.service.impl.ElasticServiceImpl$$EnhancerBySpringCGLIB$$ddf33d37.highlightQuery()
at com.cntaiping.tpa.db.controller.SearchController.query(SearchController.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
由于本人能力有限,暂时没有办法解决该异常。
@Transactional(propagation= Propagation.REQUIRED,isolation= Isolation.DEFAULT)
@Service("elasticService")
public class ElasticServiceImpl implements ElasticService {
@Autowired
@Qualifier("elasticRepository")
private ElasticRepository elasticRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Override
public List highlight(String key,Integer pageNum,Integer pageSize) {
BoolQueryBuilder boolQueryBuilder= QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery("fileName",key))
.should(QueryBuilders.matchQuery("category",key))
.should(QueryBuilders.matchQuery("content",key));
NativeSearchQuery nativeSearchQuery=new NativeSearchQueryBuilder()
.withQuery(boolQueryBuilder)
.withHighlightFields(new HighlightBuilder.Field("fileName"),new HighlightBuilder.Field("category"),new HighlightBuilder.Field("content"))
.withHighlightBuilder(new HighlightBuilder().preTags("").postTags("")).build();
AggregatedPage page = elasticsearchTemplate.queryForPage(nativeSearchQuery, ESFileBean.class, new SearchResultMapper() {
@Override
public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) {
ArrayList list = new ArrayList();
SearchHits hits = response.getHits();
for (SearchHit searchHit : hits) {
if (hits.getHits().length <= 0) {
return null;
}
Map sourceAsMap = searchHit.getSourceAsMap();
Integer fileId= (Integer) sourceAsMap.get("fileId");
String fileName= (String) sourceAsMap.get("fileName");
String category= (String) sourceAsMap.get("category");
String content= (String) sourceAsMap.get("content");
FileBean fileBean = new FileBean();
HighlightField field =searchHit.getHighlightFields().get("fileName");
if(field==null){
fileBean.setFileName(fileName);
}else{
fileBean.setFileName(field.fragments()[0].toString());
}
field = searchHit.getHighlightFields().get("category");
if(field==null){
fileBean.setContent(category);
}else{
fileBean.setContent(field.fragments()[0].toString());
}
field = searchHit.getHighlightFields().get("content");
if(field==null){
fileBean.setContent(content);
}else{
String hcontent = field.fragments()[0].toString();
fileBean.setContent(hcontent);
}
fileBean.setFileId(Long.valueOf(fileId));
list.add(fileBean);
}
if (list.size() > 0) {
return new AggregatedPageImpl((List) list);
}
return null;
}
});
return page.getContent();
}
}
@RequestMapping(value = "/query" )
@ResponseBody
public Page query(String key, String index) throws Exception {
System.out.println("(query.do)搜索:"+key);
ModelAndView mv = new ModelAndView();
if (key == null || key.trim().length() == 0) {
System.out.println("输入内容为空!");
return null;
}
//注意:jquery.pagination.js页码从0开始
int pageNum = 1;
int pageSize=20;
if (index!=null&&Integer.parseInt(index)>1){
pageNum=Integer.parseInt(index);
}
...
System.out.println("4.--------------");
list=elasticService.highlight(key,pageNum,pageSize);
System.out.println(list);
list.forEach(x->System.out.println(x));
return page;
}