查询转义符的问题

This commit is contained in:
daihh
2022-11-15 09:39:53 +08:00
parent 2f8a74a0c4
commit b2523163a8

View File

@@ -12,6 +12,7 @@ import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
@@ -338,11 +339,15 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
if(StringUtils.isNotBlank(params.getKeywords())) {
//boolQuery.must(QueryBuilders.queryStringQuery(params.getKeywords()).field("name", 8f));
String words=QueryParser.escape(params.getKeywords());
// System.out.println(params.getKeywords());
// System.out.println(words);
BoolQueryBuilder keywordQuery = QueryBuilders.boolQuery();
keywordQuery.should(QueryBuilders.wildcardQuery("name", "*"+params.getKeywords()+"*").boost(9f));
keywordQuery.should(QueryBuilders.queryStringQuery(params.getKeywords()).field("teacher", 8f));
keywordQuery.should(QueryBuilders.wildcardQuery("name", "*"+words+"*").boost(9f));
//keywordQuery.should(QueryBuilders.queryStringQuery(words).field("name", 9f));//用此方法无法查询出有转义符的处理
keywordQuery.should(QueryBuilders.queryStringQuery(words).field("teacher", 8f));
keywordQuery.minimumShouldMatch(1);
boolQuery.must(keywordQuery);
//boolQuery.must(QueryBuilders.wildcardQuery("name", "*"+params.getKeywords()+"*").boost(9f));
@@ -462,7 +467,7 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
//log.info("共查询到 "+hits.getTotalHits());
log.info("共查询到 "+hits.getTotalHits());
for (SearchHit hit : hits) {
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
@@ -477,23 +482,24 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
try {
CourseFullText cft =mapper.readValue(sourceAsString, CourseFullText.class);
cft.setEsId(hit.getId());
if(name!= null){
Text[] fragments = name.fragments();
StringBuffer fmtStr = new StringBuffer("");
for (Text fragment : fragments) {
fmtStr.append(fragment);
}
cft.setName(fmtStr.toString());
}
if(summary!= null){
Text[] fragments = summary.fragments();
StringBuffer fmtStr = new StringBuffer("");
for (Text fragment : fragments) {
fmtStr.append(fragment);
}
cft.setSummary(fmtStr.toString());
}
//以下不再加高亮显,高亮显在页面中去实现
// if(name!= null){
// Text[] fragments = name.fragments();
// StringBuffer fmtStr = new StringBuffer("");
// for (Text fragment : fragments) {
// fmtStr.append(fragment);
// }
// cft.setName(fmtStr.toString());
// }
//
// if(summary!= null){
// Text[] fragments = summary.fragments();
// StringBuffer fmtStr = new StringBuffer("");
// for (Text fragment : fragments) {
// fmtStr.append(fragment);
// }
// cft.setSummary(fmtStr.toString());
// }
//去掉老师的高亮显
// if(teacher!= null){
// Text[] fragments = teacher.fragments();
@@ -504,14 +510,14 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
// cft.setTeacher(fmtStr.toString());
// }
if(keywords!= null){
Text[] fragments = keywords.fragments();
StringBuffer fmtStr = new StringBuffer("");
for (Text fragment : fragments) {
fmtStr.append(fragment);
}
cft.setKeywords(fmtStr.toString());
}
// if(keywords!= null){
// Text[] fragments = keywords.fragments();
// StringBuffer fmtStr = new StringBuffer("");
// for (Text fragment : fragments) {
// fmtStr.append(fragment);
// }
// cft.setKeywords(fmtStr.toString());
// }
list.add(cft);
}catch(Exception ee) {