提交 fc102ed9 编写于 作者: S sanluan

elsaticsearch搜索复杂词汇高亮无效修改

上级 c7016d7a
......@@ -11,6 +11,7 @@ import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
......@@ -520,7 +521,8 @@ public abstract class BaseDao<E> {
} else {
formatter = new SimpleHTMLFormatter();
}
QueryScorer queryScorer = new RemoteMatchQueryScorer(fullTextQuery.getLuceneQuery(), defaultFieldName);
Analyzer analyzer = getFullTextSession().getSearchFactory().getAnalyzer("cms");
QueryScorer queryScorer = new RemoteMatchQueryScorer(analyzer, fullTextQuery.getLuceneQuery(), defaultFieldName);
Highlighter highlighter = new Highlighter(formatter, queryScorer);
for (E e : resultList) {
for (String fieldName : highLighterFieldNames) {
......@@ -529,8 +531,7 @@ public abstract class BaseDao<E> {
String hightLightFieldValue = null;
if (fieldValue instanceof String && CommonUtils.notEmpty(String.valueOf(fieldValue))) {
String safeValue = HtmlUtils.htmlEscape(String.valueOf(fieldValue), Constants.DEFAULT_CHARSET_NAME);
hightLightFieldValue = highlighter.getBestFragment(
getFullTextSession().getSearchFactory().getAnalyzer("cms"), fieldName, safeValue);
hightLightFieldValue = highlighter.getBestFragment(analyzer, fieldName, safeValue);
if (CommonUtils.notEmpty(hightLightFieldValue)) {
ReflectionUtils.invokeMethod(
BeanUtils.getPropertyDescriptor(getEntityClass(), fieldName).getWriteMethod(), e,
......
package com.publiccms.common.handler;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.WeightedSpanTermExtractor;
public class RemoteMatchQueryScorer extends QueryScorer {
private Analyzer analyzer;
public RemoteMatchQueryScorer(Query query, String field) {
public RemoteMatchQueryScorer(Analyzer analyzer, Query query, String field) {
super(query, field);
this.analyzer = analyzer;
}
public RemoteMatchQueryScorer(Query query, String field, String defaultField) {
public RemoteMatchQueryScorer(Analyzer analyzer, Query query, String field, String defaultField) {
super(query, null, field, defaultField);
this.analyzer = analyzer;
}
@Override
protected WeightedSpanTermExtractor newTermExtractor(String defaultField) {
return defaultField == null ? new RemoteMatchQueryWeightedSpanTermExtractor()
: new RemoteMatchQueryWeightedSpanTermExtractor(defaultField);
return defaultField == null ? new RemoteMatchQueryWeightedSpanTermExtractor(analyzer)
: new RemoteMatchQueryWeightedSpanTermExtractor(analyzer, defaultField);
}
}
package com.publiccms.common.handler;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.WeightedSpanTerm;
import org.apache.lucene.search.highlight.WeightedSpanTermExtractor;
import org.hibernate.search.query.dsl.impl.RemoteMatchQuery;
public class RemoteMatchQueryWeightedSpanTermExtractor extends WeightedSpanTermExtractor {
public RemoteMatchQueryWeightedSpanTermExtractor(String defaultField) {
private Analyzer analyzer;
public RemoteMatchQueryWeightedSpanTermExtractor(Analyzer analyzer, String defaultField) {
super(defaultField);
this.analyzer = analyzer;
}
public RemoteMatchQueryWeightedSpanTermExtractor() {
public RemoteMatchQueryWeightedSpanTermExtractor(Analyzer analyzer) {
this.analyzer = analyzer;
}
@Override
......@@ -21,8 +29,19 @@ public class RemoteMatchQueryWeightedSpanTermExtractor extends WeightedSpanTermE
if (query instanceof RemoteMatchQuery) {
RemoteMatchQuery queryTerm = (RemoteMatchQuery) query;
if (fieldNameComparator(queryTerm.getField())) {
WeightedSpanTerm weightedSpanTerm = new WeightedSpanTerm(boost, queryTerm.getSearchTerms());
terms.put(queryTerm.getSearchTerms(), weightedSpanTerm);
try (StringReader stringReader = new StringReader(queryTerm.getSearchTerms());
TokenStream tokenStream = analyzer.tokenStream(queryTerm.getField(), stringReader)) {
CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
tokenStream.reset();
while (tokenStream.incrementToken()) {
String term = charTermAttribute.toString();
WeightedSpanTerm weightedSpanTerm = new WeightedSpanTerm(boost, term);
terms.put(term, weightedSpanTerm);
}
tokenStream.end();
} catch (IOException e) {
}
}
} else {
super.extract(query, boost, terms);
......
......@@ -68,7 +68,7 @@ public class UeditorAdminController {
protected static final String FIELD_NAME = "file";
protected static final String SCRAW_TYPE = ".jpg";
protected static final String[] IMAGE_ALLOW_FILES = new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".svg"};
protected static final String[] IMAGE_ALLOW_FILES = new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".svg" };
protected static final String[] VIDEO_ALLOW_FILES = new String[] { ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg",
".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid" };
......@@ -124,8 +124,7 @@ public class UeditorAdminController {
* @return view name
*/
@RequestMapping(params = "action=" + ACTION_UPLOAD)
@ResponseBody
public Map<String, Object> upload(@RequestAttribute SysSite site, @SessionAttribute SysUser admin, MultipartFile file,
public String upload(@RequestAttribute SysSite site, @SessionAttribute SysUser admin, MultipartFile file,
HttpServletRequest request, ModelMap model) {
if (null != file && !file.isEmpty()) {
String originalName = file.getOriginalFilename();
......@@ -145,12 +144,17 @@ public class UeditorAdminController {
map.put("url", fileName);
map.put("type", suffix);
map.put("original", originalName);
return map;
model.addAttribute("result", map);
} catch (IllegalStateException | IOException e) {
model.addAttribute("result", getResultMap(false));
}
} else {
model.addAttribute("result", getResultMap(false));
}
} else {
model.addAttribute("result", getResultMap(false));
}
return getResultMap(false);
return "common/mapResult";
}
/**
......
{
<#list result as k,v>
"${k}":"${v}"<#sep>,
</#list>
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册