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