|
|
@@ -10,6 +10,7 @@ import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
import org.apache.lucene.analysis.Analyzer;
|
|
|
import org.apache.lucene.analysis.TokenStream;
|
|
|
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
|
|
@@ -27,6 +28,7 @@ import org.springframework.util.StringUtils;
|
|
|
import org.wltea.analyzer.lucene.IKAnalyzer;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.uas.search.console.core.exception.SystemError;
|
|
|
import com.uas.search.console.core.util.FastjsonUtils;
|
|
|
import com.uas.search.console.support.IndexSearcherManager;
|
|
|
import com.uas.search.console.util.SearchConstants;
|
|
|
@@ -54,15 +56,22 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
private static IndexSearcherManager searcherManager = new IndexSearcherManager();
|
|
|
|
|
|
+ private static Logger logger = Logger.getLogger(SearchServiceImpl.class);
|
|
|
+
|
|
|
@Override
|
|
|
public List<Long> getKindIds(String keyword) {
|
|
|
+ String message = "";
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
- throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ message = "搜索关键词无效" + keyword;
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
List<Long> ids = new ArrayList<Long>();
|
|
|
@@ -86,13 +95,18 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
@Override
|
|
|
public List<Map<String, Object>> getKinds(String keyword) {
|
|
|
+ String message = "";
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
- throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ message = "搜索关键词无效" + keyword;
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
List<Map<String, Object>> kinds = new ArrayList<Map<String, Object>>();
|
|
|
@@ -117,13 +131,18 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
@Override
|
|
|
public List<Long> getBrandIds(String keyword) {
|
|
|
+ String message = "";
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
- throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ message = "搜索关键词无效" + keyword;
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
List<Long> ids = new ArrayList<Long>();
|
|
|
@@ -151,13 +170,18 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
@Override
|
|
|
public List<Map<String, Object>> getBrands(String keyword) {
|
|
|
+ String message = "";
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
- throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ message = "搜索关键词无效" + keyword;
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
@@ -187,10 +211,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
@Override
|
|
|
public Map<String, Object> getComponentIds(String keyword, PageParams page) {
|
|
|
+ String message = "";
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
if (page == null) {
|
|
|
@@ -275,13 +302,18 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
@Override
|
|
|
public Set<Long> getKindIdsBySearchComponent(String keyword, String brandId) {
|
|
|
+ String message = "";
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
- throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ message = "搜索关键词无效" + keyword;
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
Set<Long> kindIds = new HashSet<Long>();
|
|
|
@@ -327,10 +359,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
return kinds;
|
|
|
}
|
|
|
|
|
|
+ String message = "";
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
@@ -359,13 +394,18 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
@Override
|
|
|
public Set<Long> getBrandIdsBySearchComponent(String keyword, String kindId) {
|
|
|
+ String message = "";
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
- throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ message = "搜索关键词无效" + keyword;
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
Set<Long> brandIds = new HashSet<Long>();
|
|
|
@@ -410,11 +450,14 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (CollectionUtils.isEmpty(brandIds)) {
|
|
|
return brands;
|
|
|
}
|
|
|
- searcherManager.maybeReopen();
|
|
|
|
|
|
+ String message = "";
|
|
|
+ searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
@@ -481,22 +524,27 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
@Override
|
|
|
public List<Map<String, Object>> getSimilarComponents(String componentCode) {
|
|
|
+ String message = "";
|
|
|
if (isKeywordInvalid(componentCode)) {
|
|
|
- throw new IllegalArgumentException("输入无效");
|
|
|
+ message = "输入无效:" + componentCode;
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
searcherManager.maybeReopen();
|
|
|
- IndexSearcher indexSearcher = searcherManager.get();
|
|
|
- if (indexSearcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (searcher == null) {
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
List<Map<String, Object>> result = new ArrayList<>();
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = getBooleanQuery(SearchConstants.COMPONENT_CODE_FIELD, componentCode);
|
|
|
- TopDocs hits = indexSearcher.search(booleanQuery, SIMILAR_NUM);
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, SIMILAR_NUM);
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
- Document document = indexSearcher.doc(scoreDoc.doc);
|
|
|
+ Document document = searcher.doc(scoreDoc.doc);
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
map.put("id", Long.parseLong(document.get(SearchConstants.COMPONENT_ID_FIELD)));
|
|
|
map.put("code", document.get(SearchConstants.COMPONENT_CODE_FIELD));
|
|
|
@@ -505,20 +553,25 @@ public class SearchServiceImpl implements SearchService {
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
- searcherManager.release(indexSearcher);
|
|
|
+ searcherManager.release(searcher);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Map<String, Object>> getSimilarBrands(String brandName) {
|
|
|
+ String message = "";
|
|
|
if (isKeywordInvalid(brandName)) {
|
|
|
- throw new IllegalArgumentException("输入无效");
|
|
|
+ message = "输入无效:" + brandName;
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
@@ -617,30 +670,35 @@ public class SearchServiceImpl implements SearchService {
|
|
|
* @return
|
|
|
*/
|
|
|
private List<String> getSimilarValues(String field, String keyword) {
|
|
|
+ String message = "";
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
- throw new IllegalArgumentException("输入无效");
|
|
|
+ message = "输入无效:" + keyword;
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
searcherManager.maybeReopen();
|
|
|
- IndexSearcher indexSearcher = searcherManager.get();
|
|
|
- if (indexSearcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (searcher == null) {
|
|
|
+ message = "获取索引文件失败";
|
|
|
+ logger.error(message);
|
|
|
+ throw new SystemError(message);
|
|
|
}
|
|
|
|
|
|
List<String> result = new ArrayList<>();
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = getBooleanQuery(field, keyword);
|
|
|
- TopDocs hits = indexSearcher.search(booleanQuery, SIMILAR_NUM);
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, SIMILAR_NUM);
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
Set<String> fieldsToLoad = new HashSet<>();
|
|
|
fieldsToLoad.add(field);
|
|
|
- Document document = indexSearcher.doc(scoreDoc.doc, fieldsToLoad);
|
|
|
+ Document document = searcher.doc(scoreDoc.doc, fieldsToLoad);
|
|
|
result.add(document.get(field));
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
- searcherManager.release(indexSearcher);
|
|
|
+ searcherManager.release(searcher);
|
|
|
}
|
|
|
return result;
|
|
|
}
|