|
|
@@ -7,7 +7,6 @@ import com.uas.search.constant.model.CollectField;
|
|
|
import com.uas.search.constant.model.PageParams;
|
|
|
import com.uas.search.constant.model.PageParams.FilterField;
|
|
|
import com.uas.search.constant.model.SPage;
|
|
|
-import com.uas.search.exception.SearchException;
|
|
|
import com.uas.search.grouping.DistinctGroupCollector;
|
|
|
import com.uas.search.grouping.GoodsGroupCollector;
|
|
|
import com.uas.search.model.*;
|
|
|
@@ -50,7 +49,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
private static Logger logger = LoggerFactory.getLogger(SearchServiceImpl.class);
|
|
|
|
|
|
@Override
|
|
|
- public SPage<Long> getKindIds(String keyword, Integer page, Integer size) {
|
|
|
+ public SPage<Long> getKindIds(String keyword, Integer page, Integer size) throws IOException {
|
|
|
List<Long> ids = new ArrayList<>();
|
|
|
SPage<Document> documents = getKindDocuments(keyword, page, size);;
|
|
|
SPage<Long> sPage = new SPage<>(documents.getTotalPage(), documents.getTotalElement(), documents.getPage(),
|
|
|
@@ -63,7 +62,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SPage<Map<String, Object>> getKinds(String keyword, Integer page, Integer size) {
|
|
|
+ public SPage<Map<String, Object>> getKinds(String keyword, Integer page, Integer size) throws IOException {
|
|
|
List<Map<String, Object>> kinds = new ArrayList<>();
|
|
|
SPage<Document> documents = getKindDocuments(keyword, page, size);
|
|
|
SPage<Map<String, Object>> sPage = new SPage<>(documents.getTotalPage(),
|
|
|
@@ -79,9 +78,9 @@ public class SearchServiceImpl implements SearchService {
|
|
|
return sPage;
|
|
|
}
|
|
|
|
|
|
- private SPage<Document> getKindDocuments(String keyword, Integer page, Integer size){
|
|
|
+ private SPage<Document> getKindDocuments(String keyword, Integer page, Integer size) throws IOException {
|
|
|
if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
- throw new SearchException("搜索关键词无效:" + keyword);
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效:" + keyword);
|
|
|
}
|
|
|
BooleanQuery booleanQuery = SearchUtils.getBooleanQuery(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
logger.info(booleanQuery.toString());
|
|
|
@@ -117,7 +116,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SPage<Long> getBrandIds(String keyword, Integer page, Integer size) {
|
|
|
+ public SPage<Long> getBrandIds(String keyword, Integer page, Integer size) throws IOException {
|
|
|
List<Long> ids = new ArrayList<>();
|
|
|
SPage<Document> documents = getBrandDocuments(keyword, page, size);
|
|
|
SPage<Long> sPage = new SPage<>(documents.getTotalPage(), documents.getTotalElement(), documents.getPage(),
|
|
|
@@ -130,7 +129,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SPage<Map<String, Object>> getBrands(String keyword, Integer page, Integer size) {
|
|
|
+ public SPage<Map<String, Object>> getBrands(String keyword, Integer page, Integer size) throws IOException {
|
|
|
List<Map<String, Object>> brands = new ArrayList<>();
|
|
|
SPage<Document> documents = getBrandDocuments(keyword, page, size);
|
|
|
SPage<Map<String, Object>> sPage = new SPage<Map<String, Object>>(documents.getTotalPage(),
|
|
|
@@ -148,9 +147,9 @@ public class SearchServiceImpl implements SearchService {
|
|
|
return sPage;
|
|
|
}
|
|
|
|
|
|
- private SPage<Document> getBrandDocuments(String keyword, Integer page, Integer size){
|
|
|
+ private SPage<Document> getBrandDocuments(String keyword, Integer page, Integer size) throws IOException {
|
|
|
if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
- throw new SearchException("搜索关键词无效:" + keyword);
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效:" + keyword);
|
|
|
}
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
booleanQuery.add(SearchUtils.getBooleanQuery(SearchConstants.BRAND_NAMECN_FIELD, keyword),
|
|
|
@@ -199,10 +198,9 @@ public class SearchServiceImpl implements SearchService {
|
|
|
* @param kindIds
|
|
|
* @param brandIds
|
|
|
* @return
|
|
|
- * @throws SearchException
|
|
|
*/
|
|
|
private Map<String, Object> getComponentIds(String keyword, PageParams pageParams, List<Long> kindIds,
|
|
|
- List<Long> brandIds) throws SearchException {
|
|
|
+ List<Long> brandIds) {
|
|
|
// 因为器件、属性值的数据量远比类目、品牌大得多,而且器件搜索可能还需进行分页,
|
|
|
// 所以涉及器件、属性值的搜索,大都不能像类目和品牌一样直接利用SearchUtils.getDocuments方法
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.COMPONENT_TABLE_NAME);
|
|
|
@@ -434,7 +432,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
*/
|
|
|
private DistinctGroupCollector collectBySearchComponent(String keyword, Query filter, String groupField, DistinctGroupCollector.CollectField... collectFields){
|
|
|
if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
- throw new SearchException("搜索关键词无效:" + keyword);
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效:" + keyword);
|
|
|
}
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.COMPONENT_TABLE_NAME);
|
|
|
try {
|
|
|
@@ -500,7 +498,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
public List<Map<String, Object>> getSimilarComponents(String componentCode, Integer size) {
|
|
|
size = size == null || size < 1 ? SIMILAR_NUM : size;
|
|
|
if (SearchUtils.isKeywordInvalid(componentCode)) {
|
|
|
- throw new SearchException("输入无效:" + componentCode);
|
|
|
+ throw new IllegalArgumentException("输入无效:" + componentCode);
|
|
|
}
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.COMPONENT_TABLE_NAME);
|
|
|
|
|
|
@@ -534,10 +532,10 @@ public class SearchServiceImpl implements SearchService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Map<String, Object>> getSimilarBrands(String brandName, Integer size) {
|
|
|
+ public List<Map<String, Object>> getSimilarBrands(String brandName, Integer size) throws IOException {
|
|
|
size = size == null || size < 1 ? SIMILAR_NUM : size;
|
|
|
if (SearchUtils.isKeywordInvalid(brandName)) {
|
|
|
- throw new SearchException("输入无效:" + brandName);
|
|
|
+ throw new IllegalArgumentException("输入无效:" + brandName);
|
|
|
}
|
|
|
List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
// 品牌名称带有空格,并且中英文名并无一定顺序,因此对nameCn、nameEn均要搜索
|
|
|
@@ -566,19 +564,19 @@ public class SearchServiceImpl implements SearchService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Map<String, Object>> getSimilarKinds(String kindName, Integer size) {
|
|
|
+ public List<Map<String, Object>> getSimilarKinds(String kindName, Integer size) throws IOException {
|
|
|
size = size == null || size < 1 ? SIMILAR_NUM : size;
|
|
|
return getSimilarKinds(kindName, null, null, size);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Map<String, Object>> getSimilarLeafKinds(String kindName, Integer size) {
|
|
|
+ public List<Map<String, Object>> getSimilarLeafKinds(String kindName, Integer size) throws IOException {
|
|
|
size = size == null || size < 1 ? SIMILAR_NUM : size;
|
|
|
return getSimilarKinds(kindName, (short) 1, null, size);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Map<String, Object>> getSimilarKindsByLevel(String kindName, Short level, Integer size) {
|
|
|
+ public List<Map<String, Object>> getSimilarKindsByLevel(String kindName, Short level, Integer size) throws IOException {
|
|
|
size = size == null || size < 1 ? SIMILAR_NUM : size;
|
|
|
return getSimilarKinds(kindName, null, level, size);
|
|
|
}
|
|
|
@@ -595,10 +593,10 @@ public class SearchServiceImpl implements SearchService {
|
|
|
* @param size 指定的联想词数目
|
|
|
* @return
|
|
|
*/
|
|
|
- private List<Map<String, Object>> getSimilarKinds(String kindName, Short isLeaf, Short level, Integer size) {
|
|
|
+ private List<Map<String, Object>> getSimilarKinds(String kindName, Short isLeaf, Short level, Integer size) throws IOException {
|
|
|
size = size == null || size < 1 ? SIMILAR_NUM : size;
|
|
|
if (SearchUtils.isKeywordInvalid(kindName)) {
|
|
|
- throw new SearchException("输入无效:" + kindName);
|
|
|
+ throw new IllegalArgumentException("输入无效:" + kindName);
|
|
|
}
|
|
|
List<Map<String, Object>> kinds = new ArrayList<>();
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
@@ -633,7 +631,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
public List<Map<String, String>> getSimilarPropertyValues(Long kindId, Long propertyId, String keyword,
|
|
|
Long topNum) {
|
|
|
if (kindId == null || propertyId == null) {
|
|
|
- throw new SearchException("类目id和属性id不能为空");
|
|
|
+ throw new IllegalArgumentException("类目id和属性id不能为空");
|
|
|
}
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.COMPONENT_TABLE_NAME);
|
|
|
|
|
|
@@ -749,7 +747,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
*/
|
|
|
private List<String> getSimilarValues(String tableName, String field, String sortField, String keyword, Integer size, boolean usePrefixQuery) {
|
|
|
if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
- throw new SearchException("输入无效:" + keyword);
|
|
|
+ throw new IllegalArgumentException("输入无效:" + keyword);
|
|
|
}
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(tableName);
|
|
|
|
|
|
@@ -818,7 +816,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, Object> getGoodsIds(String keyword, PageParams pageParams) throws SearchException {
|
|
|
+ public Map<String, Object> getGoodsIds(String keyword, PageParams pageParams) {
|
|
|
List<String> keywordFields = new ArrayList<>();
|
|
|
// 先根据品牌搜索,品牌不存在再搜索型号等
|
|
|
keywordFields.add(SearchConstants.GOODS_BR_NAME_CN_UNTOKENIZED_FIELD);
|
|
|
@@ -840,10 +838,9 @@ public class SearchServiceImpl implements SearchService {
|
|
|
* @param pageParams
|
|
|
* @param recursivelyGet 是否递归获取(逐步降低精度,直到只匹配一个字符)
|
|
|
* @return
|
|
|
- * @throws SearchException
|
|
|
*/
|
|
|
private Map<String, Object> getGoodsIds(String keyword, List<String> keywordFields, Boolean tokenized,
|
|
|
- PageParams pageParams, Boolean recursivelyGet) throws SearchException {
|
|
|
+ PageParams pageParams, Boolean recursivelyGet) {
|
|
|
// 因为器件、属性值的数据量远比类目、品牌大得多,而且器件搜索可能还需进行分页,
|
|
|
// 所以涉及器件、属性值的搜索,大都不能像类目和品牌一样直接利用SearchUtils.getDocuments方法
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.GOODS_TABLE_NAME);
|
|
|
@@ -1025,7 +1022,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (sort != null) {
|
|
|
com.uas.search.constant.model.Sort.Field field = sort.getField();
|
|
|
if (field == null) {
|
|
|
- throw new SearchException("排序字段不可为空:" + sort);
|
|
|
+ throw new IllegalArgumentException("排序字段不可为空:" + sort);
|
|
|
}
|
|
|
boolean reverse = sort.isReverse();
|
|
|
if (field == RESERVE) {
|
|
|
@@ -1045,7 +1042,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
sortField(SearchConstants.GOODS_GO_RESERVE_FIELD, Type.DOUBLE, true, Double.MIN_VALUE)
|
|
|
));
|
|
|
} else {
|
|
|
- throw new SearchException("不支持该排序方式:" + field);
|
|
|
+ throw new IllegalArgumentException("不支持该排序方式:" + field);
|
|
|
}
|
|
|
} else {
|
|
|
// 默认(综合排序)
|
|
|
@@ -1100,7 +1097,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
private List<Map<String, Object>> collectBySearchGoods(String keyword, List<String> keywordFields,
|
|
|
Boolean tokenized, CollectField collectedField, Map<FilterField, Object> filters, Boolean recursivelyGet) {
|
|
|
if (collectedField == null && CollectionUtils.isEmpty(filters)) {
|
|
|
- throw new SearchException("参数不合法:collectedField=" + collectedField + ", filter=" + filters);
|
|
|
+ throw new IllegalArgumentException("参数不合法:collectedField=" + collectedField + ", filter=" + filters);
|
|
|
}
|
|
|
// 与批次搜索的搜索词保持一致,最终有结果的 keyword 是相同的
|
|
|
if(recursivelyGet != null && recursivelyGet){
|
|
|
@@ -1144,7 +1141,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
fieldsToLoad.add(SearchConstants.GOODS_CRNAME_FIELD);
|
|
|
break;
|
|
|
default:
|
|
|
- throw new SearchException("不支持该统计字段:" + collectedField);
|
|
|
+ throw new IllegalArgumentException("不支持该统计字段:" + collectedField);
|
|
|
}
|
|
|
GoodsGroupCollector collector = new GoodsGroupCollector(uniqueField, fieldsToLoad);
|
|
|
indexSearcher.search(booleanQuery, collector);
|
|
|
@@ -1258,31 +1255,31 @@ public class SearchServiceImpl implements SearchService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Kind getKind(Long id) {
|
|
|
+ public Kind getKind(Long id) throws IOException {
|
|
|
return DocumentToObjectUtils.toKind(
|
|
|
SearchUtils.getDocumentById(SearchConstants.KIND_TABLE_NAME, SearchConstants.KIND_ID_FIELD, id));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Brand getBrand(Long id) {
|
|
|
+ public Brand getBrand(Long id) throws IOException {
|
|
|
return DocumentToObjectUtils.toBrand(
|
|
|
SearchUtils.getDocumentById(SearchConstants.BRAND_TABLE_NAME, SearchConstants.BRAND_ID_FIELD, id));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Component getComponent(Long id) {
|
|
|
+ public Component getComponent(Long id) throws IOException {
|
|
|
return DocumentToObjectUtils.toComponent(SearchUtils.getDocumentById(SearchConstants.COMPONENT_TABLE_NAME,
|
|
|
SearchConstants.COMPONENT_ID_FIELD, id));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Goods getGoods(String id) {
|
|
|
+ public Goods getGoods(String id) throws IOException {
|
|
|
return DocumentToObjectUtils.toGoods(
|
|
|
SearchUtils.getDocumentById(SearchConstants.GOODS_TABLE_NAME, SearchConstants.GOODS_GO_ID_FIELD, id));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SPage<Object> getObjects(String tableName, String keyword, String field, Boolean tokenized, @NotEmpty("page") Integer page, @NotEmpty("size") Integer size) {
|
|
|
+ public SPage<Object> getObjects(String tableName, String keyword, String field, Boolean tokenized, @NotEmpty("page") Integer page, @NotEmpty("size") Integer size) throws IOException {
|
|
|
if (keyword == null) {
|
|
|
keyword = "";
|
|
|
}
|
|
|
@@ -1321,7 +1318,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
int totalHits = previousTopDocs.totalHits;
|
|
|
ScoreDoc[] previousScoreDocs = previousTopDocs.scoreDocs;
|
|
|
if ((sPage.getPage() - 1) * sPage.getSize() >= totalHits) {
|
|
|
- throw new SearchException("页码过大:元素总数量为" + totalHits);
|
|
|
+ throw new IllegalArgumentException("页码过大:元素总数量为" + totalHits);
|
|
|
}
|
|
|
topDocs = indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], query,
|
|
|
sPage.getSize());
|
|
|
@@ -1344,8 +1341,6 @@ public class SearchServiceImpl implements SearchService {
|
|
|
content.add(DocumentToObjectUtils.toObject(document, tableName));
|
|
|
}
|
|
|
sPage.setContent(content);
|
|
|
- } catch (IOException e) {
|
|
|
- throw new SearchException(e).setDetailedMessage(e);
|
|
|
} finally {
|
|
|
SearchUtils.releaseIndexSearcher(indexSearcher);
|
|
|
}
|