|
@ -14,6 +14,7 @@ import io.swagger.annotations.ApiOperation;
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
|
|
import org.apache.solr.client.solrj.util.ClientUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
@ -56,7 +57,8 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
|
|
|
textSearchService.saveOrUpdateHotWords(keyword, 1);
|
|
|
Map<String, String> sortMap = new HashMap<>(1);
|
|
|
sortMap.put(ResourceCells.EVENT_DATE, "desc");
|
|
|
QueryResponse queryResponse = solrUtil.highlight(ResourceCore.MasterTable, generateQ(keyword, filters), null, sortMap, page - 1, size, ResourceCells.SEARCH_FIELD, null);
|
|
|
String q = getQueryBySearchText(keyword,filters);
|
|
|
QueryResponse queryResponse = solrUtil.highlight(ResourceCore.MasterTable, q, null, sortMap, page - 1, size, ResourceCells.SEARCH_FIELD, null);
|
|
|
List<Map<String, Object>> dataList = new ArrayList<>(queryResponse.getHighlighting().size());
|
|
|
queryResponse.getResults().forEach(document -> {
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
@ -168,4 +170,165 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
|
|
|
}
|
|
|
return q.toString();
|
|
|
}
|
|
|
|
|
|
private String getQueryBySearchText(String keys,String filters){
|
|
|
String q = "";
|
|
|
//用于存储某个词
|
|
|
String temp = "";
|
|
|
//用于存储某个词的开头
|
|
|
String start="";
|
|
|
//未找过的字符串
|
|
|
String otherString = "";
|
|
|
//用于判断 and or ,上个词
|
|
|
String preString ="and";
|
|
|
if(StringUtils.isNotBlank(keys)) {
|
|
|
for (int i = 0; i < keys.length(); i++) {
|
|
|
String ch = String.valueOf(keys.charAt(i));
|
|
|
if (StringUtils.isBlank(start)) {
|
|
|
if (" ".equals(ch)) {
|
|
|
continue;
|
|
|
} else {
|
|
|
start = ch;
|
|
|
otherString = keys.substring(i+1,keys.length());
|
|
|
}
|
|
|
}
|
|
|
//以"开头 ,查找后面是否有 " ,有则截取
|
|
|
if ("\"".equals(start)){
|
|
|
int fhIndex = otherString.indexOf("\"");
|
|
|
if(fhIndex > -1){
|
|
|
temp = keys.substring(i+1,i+fhIndex+1);
|
|
|
if(!"or".equalsIgnoreCase(preString)){
|
|
|
q = builderAndQuery(temp,q);
|
|
|
}else{
|
|
|
q = builderOrQuery(temp,q);
|
|
|
}
|
|
|
i = fhIndex+i+1;
|
|
|
start = "";
|
|
|
preString = temp;
|
|
|
}else{
|
|
|
int kgIndex = otherString.indexOf(" ");
|
|
|
if(kgIndex > -1){
|
|
|
temp = keys.substring(i,i+kgIndex+1);
|
|
|
if(!"or".equalsIgnoreCase(preString)){
|
|
|
q = builderAndQuery(temp,q);
|
|
|
}else{
|
|
|
q = builderOrQuery(temp,q);
|
|
|
}
|
|
|
i = kgIndex+i+1;
|
|
|
start = "";
|
|
|
preString = temp;
|
|
|
}else{
|
|
|
temp = start+otherString;
|
|
|
if(!"or".equalsIgnoreCase(preString)){
|
|
|
q = builderAndQuery(temp,q);
|
|
|
}else{
|
|
|
q = builderOrQuery(temp,q);
|
|
|
}
|
|
|
i= keys.length();
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
int kgIndex = otherString.indexOf(" ");
|
|
|
if(kgIndex > -1){
|
|
|
temp = keys.substring(i,i+kgIndex+1);
|
|
|
if(!"and".equalsIgnoreCase(temp) && !"or".equalsIgnoreCase(temp)){
|
|
|
if(!"or".equalsIgnoreCase(preString)){
|
|
|
q = builderAndQuery(temp,q);
|
|
|
}else{
|
|
|
q = builderOrQuery(temp,q);
|
|
|
}
|
|
|
}
|
|
|
i=kgIndex+i+1;
|
|
|
start = "";
|
|
|
preString = temp;
|
|
|
}else{
|
|
|
temp = start+otherString;
|
|
|
if(!"and".equalsIgnoreCase(temp) && !"or".equalsIgnoreCase(temp)){
|
|
|
if(!"or".equalsIgnoreCase(preString)){
|
|
|
q = builderAndQuery(temp,q);
|
|
|
}else{
|
|
|
q = builderOrQuery(temp,q);
|
|
|
}
|
|
|
}
|
|
|
i= keys.length();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
System.out.println(q);
|
|
|
q = getQuery(filters, new StringBuilder(q));
|
|
|
return q;
|
|
|
}
|
|
|
|
|
|
private String builderAndQuery(String temp,String q){
|
|
|
if(StringUtils.isNotBlank(temp)){
|
|
|
if(temp.startsWith("-")){
|
|
|
String substring = temp.substring(1, temp.length());
|
|
|
substring = ClientUtils.escapeQueryChars(substring);
|
|
|
if(StringUtils.isNotBlank(substring)){
|
|
|
if(StringUtils.isBlank(q)){
|
|
|
q+="-" + ResourceCells.SEARCH_FIELD + ":" + substring ;
|
|
|
}else{
|
|
|
q+=" AND -" + ResourceCells.SEARCH_FIELD + ":" + substring;
|
|
|
}
|
|
|
}
|
|
|
}else if(temp.startsWith("+")){
|
|
|
String substring = temp.substring(1, temp.length());
|
|
|
substring = ClientUtils.escapeQueryChars(substring);
|
|
|
if(StringUtils.isNotBlank(substring)){
|
|
|
if(StringUtils.isBlank(q)){
|
|
|
q+= ResourceCells.SEARCH_FIELD + ":" + temp.substring(1,temp.length());
|
|
|
}else{
|
|
|
q+=" AND " + ResourceCells.SEARCH_FIELD + ":" + temp.substring(1,temp.length());
|
|
|
}
|
|
|
}
|
|
|
}else{
|
|
|
if(StringUtils.isNotBlank(temp)){
|
|
|
temp = ClientUtils.escapeQueryChars(temp);
|
|
|
if(StringUtils.isBlank(q)){
|
|
|
q+= ResourceCells.SEARCH_FIELD + ":" + temp;
|
|
|
}else{
|
|
|
q+=" AND " + ResourceCells.SEARCH_FIELD + ":" + temp;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return q;
|
|
|
}
|
|
|
|
|
|
private String builderOrQuery(String temp,String q){
|
|
|
if(StringUtils.isNotBlank(temp)){
|
|
|
if(temp.startsWith("-")){
|
|
|
String substring = temp.substring(1, temp.length());
|
|
|
substring = ClientUtils.escapeQueryChars(substring);
|
|
|
if(StringUtils.isNotBlank(substring)){
|
|
|
if(StringUtils.isBlank(q)){
|
|
|
q+="-" + ResourceCells.SEARCH_FIELD + ":" + substring;
|
|
|
}else{
|
|
|
q+=" OR -" + ResourceCells.SEARCH_FIELD + ":" + substring;
|
|
|
}
|
|
|
}
|
|
|
}else if(temp.startsWith("+")){
|
|
|
String substring = temp.substring(1, temp.length());
|
|
|
substring = ClientUtils.escapeQueryChars(substring);
|
|
|
if(StringUtils.isNotBlank(substring)){
|
|
|
if(StringUtils.isBlank(q)){
|
|
|
q+= ResourceCells.SEARCH_FIELD + ":" + substring;
|
|
|
}else{
|
|
|
q+=" OR " + ResourceCells.SEARCH_FIELD + ":" + substring;
|
|
|
}
|
|
|
}
|
|
|
}else{
|
|
|
if(StringUtils.isNotBlank(temp)) {
|
|
|
temp = ClientUtils.escapeQueryChars(temp);
|
|
|
if(StringUtils.isBlank(q)){
|
|
|
q+= ResourceCells.SEARCH_FIELD + ":" + temp;
|
|
|
}else{
|
|
|
q+=" OR " + ResourceCells.SEARCH_FIELD + ":" + temp;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return q;
|
|
|
}
|
|
|
}
|