|
|
@@ -0,0 +1,472 @@
|
|
|
+package com.uas.search.console.service.impl;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import org.apache.lucene.analysis.Analyzer;
|
|
|
+import org.apache.lucene.analysis.TokenStream;
|
|
|
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
|
|
+import org.apache.lucene.document.Document;
|
|
|
+import org.apache.lucene.index.Term;
|
|
|
+import org.apache.lucene.search.BooleanClause;
|
|
|
+import org.apache.lucene.search.BooleanQuery;
|
|
|
+import org.apache.lucene.search.IndexSearcher;
|
|
|
+import org.apache.lucene.search.PrefixQuery;
|
|
|
+import org.apache.lucene.search.Query;
|
|
|
+import org.apache.lucene.search.ScoreDoc;
|
|
|
+import org.apache.lucene.search.TermQuery;
|
|
|
+import org.apache.lucene.search.TopDocs;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.wltea.analyzer.lucene.IKAnalyzer;
|
|
|
+
|
|
|
+import com.uas.search.console.support.IndexSearcherManager;
|
|
|
+import com.uas.search.console.util.SearchConstants;
|
|
|
+import com.uas.search.model.PageParams;
|
|
|
+import com.uas.search.service.SearchService;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class SearchServiceImpl implements SearchService {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认最大搜索的记录条数
|
|
|
+ */
|
|
|
+ private static final int TOP_NUM = 1024 * 1024 * 1024;
|
|
|
+
|
|
|
+ private static IndexSearcherManager searcherManager = new IndexSearcherManager();
|
|
|
+
|
|
|
+ public List<Long> getKindIds(String keyword) {
|
|
|
+ List<Long> ids = new ArrayList<Long>();
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (isKeywordInvalid(keyword)) {
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ }
|
|
|
+ if (searcher == null) {
|
|
|
+ throw new RuntimeException("获取索引文件失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+
|
|
|
+ @SuppressWarnings("resource")
|
|
|
+ Analyzer analyzer = new IKAnalyzer(true);
|
|
|
+ TokenStream tokenStream;
|
|
|
+ try {
|
|
|
+ tokenStream = analyzer.tokenStream(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
+ tokenStream.reset();
|
|
|
+ CharTermAttribute cta = tokenStream.addAttribute(CharTermAttribute.class);
|
|
|
+ while (tokenStream.incrementToken()) {
|
|
|
+ Query query1 = new PrefixQuery(new Term(SearchConstants.KIND_NAMECN_FIELD, cta.toString()));
|
|
|
+ booleanQuery.add(query1, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ for (ScoreDoc doc : scoreDocs) {
|
|
|
+ Document document = searcher.doc(doc.doc);
|
|
|
+ ids.add(Long.parseLong(document.get(SearchConstants.KIND_ID_FIELD)));
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(searcher);
|
|
|
+ }
|
|
|
+ return ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Long> getBrandIds(String keyword) {
|
|
|
+ List<Long> ids = new ArrayList<Long>();
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (isKeywordInvalid(keyword)) {
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ }
|
|
|
+ if (searcher == null) {
|
|
|
+ throw new RuntimeException("获取索引文件失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+
|
|
|
+ @SuppressWarnings("resource")
|
|
|
+ Analyzer analyzer = new IKAnalyzer(true);
|
|
|
+ TokenStream tokenStream;
|
|
|
+ try {
|
|
|
+ tokenStream = analyzer.tokenStream(SearchConstants.BRAND_NAMECN_FIELD, keyword);
|
|
|
+ tokenStream.reset();
|
|
|
+ CharTermAttribute cta = tokenStream.addAttribute(CharTermAttribute.class);
|
|
|
+ while (tokenStream.incrementToken()) {
|
|
|
+ Query query1 = new PrefixQuery(new Term(SearchConstants.BRAND_NAMECN_FIELD, cta.toString()));
|
|
|
+ booleanQuery.add(query1, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ for (ScoreDoc doc : scoreDocs) {
|
|
|
+ Document document = searcher.doc(doc.doc);
|
|
|
+ ids.add(Long.parseLong(document.get(SearchConstants.BRAND_ID_FIELD)));
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(searcher);
|
|
|
+ }
|
|
|
+
|
|
|
+ return ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, Object> getComponentIds(String keyword, PageParams page) {
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ List<Long> ids = new ArrayList<Long>();
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (isKeywordInvalid(keyword)) {
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ }
|
|
|
+ if (searcher == null) {
|
|
|
+ throw new RuntimeException("获取索引文件失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (page.getPage() == 0)
|
|
|
+ page.setPage(1);
|
|
|
+ if (page.getSize() == 0)
|
|
|
+ page.setSize(20);
|
|
|
+
|
|
|
+ try {
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+ // 因SearcherController里调用URLEncoder.encode,搜索词中的空格(若有)会被替换为"+",因此需要进行恢复
|
|
|
+ keyword = URLDecoder.decode(keyword, "UTF-8");
|
|
|
+ keyword = keyword.toLowerCase();
|
|
|
+ PrefixQuery prefixQuery = new PrefixQuery(new Term(SearchConstants.COMPONENT_CODE_FIELD, keyword));
|
|
|
+ booleanQuery.add(prefixQuery, BooleanClause.Occur.MUST);
|
|
|
+ if (!StringUtils.isEmpty(page.getFilters())) {
|
|
|
+ if (!StringUtils.isEmpty(page.getFilters().get("kindId"))) {// 筛选类目
|
|
|
+ String kindId = String.valueOf(page.getFilters().get("kindId"));
|
|
|
+ TermQuery kindQuery = new TermQuery(new Term(SearchConstants.COMPONENT_KINDID_FIELD, kindId));
|
|
|
+ booleanQuery.add(kindQuery, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+ if (!StringUtils.isEmpty(page.getFilters().get("brandId"))) {// 筛选品牌
|
|
|
+ String brandId = String.valueOf(page.getFilters().get("brandId"));
|
|
|
+ TermQuery brandQuery = new TermQuery(new Term(SearchConstants.COMPONENT_BRANDID_FIELD, brandId));
|
|
|
+ booleanQuery.add(brandQuery, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ TopDocs hits;
|
|
|
+ if (page.getPage() > 1) {// 不是第一页
|
|
|
+ TopDocs previousHits = searcher.search(booleanQuery, (page.getPage() - 1) * page.getSize());
|
|
|
+ ScoreDoc[] previousScoreDocs = previousHits.scoreDocs;
|
|
|
+ ScoreDoc after = previousScoreDocs[previousScoreDocs.length - 1];
|
|
|
+ hits = searcher.searchAfter(after, booleanQuery, page.getSize());
|
|
|
+ } else {
|
|
|
+ hits = searcher.search(booleanQuery, page.getSize());
|
|
|
+ }
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ if (hits.totalHits > 0) {
|
|
|
+ for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
+ Document document = searcher.doc(scoreDoc.doc);
|
|
|
+ String componentId = document.get(SearchConstants.COMPONENT_ID_FIELD);
|
|
|
+ ids.add(Long.parseLong(componentId));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ map.put("components", ids);
|
|
|
+ map.put("page", page.getPage());
|
|
|
+ map.put("size", page.getSize());
|
|
|
+ map.put("total", hits.totalHits);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(searcher);
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Set<Long> getKindIdsBySearchComponent(String keyword, String brandId) {
|
|
|
+ Set<Long> kindIds = new HashSet<Long>();
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (isKeywordInvalid(keyword)) {
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ }
|
|
|
+ if (searcher == null) {
|
|
|
+ throw new RuntimeException("获取索引文件失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+
|
|
|
+ keyword = URLDecoder.decode(keyword, "UTF-8");
|
|
|
+ keyword = keyword.toLowerCase();
|
|
|
+ PrefixQuery prefixQuery = new PrefixQuery(new Term(SearchConstants.COMPONENT_CODE_FIELD, keyword));
|
|
|
+ booleanQuery.add(prefixQuery, BooleanClause.Occur.MUST);
|
|
|
+
|
|
|
+ // 筛选品牌
|
|
|
+ if (!StringUtils.isEmpty(brandId)) {
|
|
|
+ TermQuery brandQuery = new TermQuery(new Term(SearchConstants.COMPONENT_BRANDID_FIELD, brandId));
|
|
|
+ booleanQuery.add(brandQuery, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
+ if (hits.totalHits > 0) {
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
+ Document document = searcher.doc(scoreDoc.doc);
|
|
|
+ String cmp_kind_id = document.get(SearchConstants.COMPONENT_KINDID_FIELD);
|
|
|
+ if (!StringUtils.isEmpty(cmp_kind_id)) {
|
|
|
+ kindIds.add(Long.parseLong(cmp_kind_id));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(searcher);
|
|
|
+ }
|
|
|
+ return kindIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Set<Long> getBrandIdsBySearchComponent(String keyword, String kindId) {
|
|
|
+ Set<Long> brandIds = new HashSet<Long>();
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (isKeywordInvalid(keyword) || searcher == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+
|
|
|
+ keyword = URLDecoder.decode(keyword, "UTF-8");
|
|
|
+ keyword = keyword.toLowerCase();
|
|
|
+ PrefixQuery prefixQuery = new PrefixQuery(new Term(SearchConstants.COMPONENT_CODE_FIELD, keyword));
|
|
|
+ booleanQuery.add(prefixQuery, BooleanClause.Occur.MUST);
|
|
|
+
|
|
|
+ // 筛选类目
|
|
|
+ if (!StringUtils.isEmpty(kindId)) {
|
|
|
+ TermQuery kindQuery = new TermQuery(new Term(SearchConstants.COMPONENT_KINDID_FIELD, kindId));
|
|
|
+ booleanQuery.add(kindQuery, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
+ if (hits.totalHits > 0) {
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
+ Document document = searcher.doc(scoreDoc.doc);
|
|
|
+ String cmp_brand_id = document.get(SearchConstants.COMPONENT_BRANDID_FIELD);
|
|
|
+ if (!StringUtils.isEmpty(cmp_brand_id)) {
|
|
|
+ brandIds.add(Long.parseLong(cmp_brand_id));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(searcher);
|
|
|
+ }
|
|
|
+
|
|
|
+ return brandIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断搜索词是否为无效的(比如只包含特殊字符,不含有任何字母、数字、汉字等有意义的字符)
|
|
|
+ *
|
|
|
+ * @param keyword
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean isKeywordInvalid(String keyword) {
|
|
|
+ if (StringUtils.isEmpty(keyword)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ // 将特殊字符剔除
|
|
|
+ keyword = keyword.replaceAll("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]+", "");
|
|
|
+ if (StringUtils.isEmpty(keyword)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Map<String, Object>> getKinds(String keyword) {
|
|
|
+ List<Map<String, Object>> kinds = new ArrayList<Map<String, Object>>();
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (isKeywordInvalid(keyword)) {
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ }
|
|
|
+ if (searcher == null) {
|
|
|
+ throw new RuntimeException("获取索引文件失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+
|
|
|
+ @SuppressWarnings("resource")
|
|
|
+ Analyzer analyzer = new IKAnalyzer(true);
|
|
|
+ TokenStream tokenStream;
|
|
|
+ try {
|
|
|
+ tokenStream = analyzer.tokenStream(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
+ tokenStream.reset();
|
|
|
+ CharTermAttribute cta = tokenStream.addAttribute(CharTermAttribute.class);
|
|
|
+ while (tokenStream.incrementToken()) {
|
|
|
+ Query query1 = new PrefixQuery(new Term(SearchConstants.KIND_NAMECN_FIELD, cta.toString()));
|
|
|
+ booleanQuery.add(query1, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ for (ScoreDoc doc : scoreDocs) {
|
|
|
+ Document document = searcher.doc(doc.doc);
|
|
|
+ Map<String, Object> kind = new HashMap<String, Object>();
|
|
|
+ kind.put("id", Long.parseLong(document.get(SearchConstants.KIND_ID_FIELD)));
|
|
|
+ kind.put("nameCn", document.get(SearchConstants.KIND_NAMECN_FIELD));
|
|
|
+ kinds.add(kind);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(searcher);
|
|
|
+ }
|
|
|
+ return kinds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Map<String, Object>> getBrands(String keyword) {
|
|
|
+ List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (isKeywordInvalid(keyword)) {
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ }
|
|
|
+ if (searcher == null) {
|
|
|
+ throw new RuntimeException("获取索引文件失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+
|
|
|
+ @SuppressWarnings("resource")
|
|
|
+ Analyzer analyzer = new IKAnalyzer(true);
|
|
|
+ TokenStream tokenStream;
|
|
|
+ try {
|
|
|
+ tokenStream = analyzer.tokenStream(SearchConstants.BRAND_NAMECN_FIELD, keyword);
|
|
|
+ tokenStream.reset();
|
|
|
+ CharTermAttribute cta = tokenStream.addAttribute(CharTermAttribute.class);
|
|
|
+ while (tokenStream.incrementToken()) {
|
|
|
+ Query query1 = new PrefixQuery(new Term(SearchConstants.BRAND_NAMECN_FIELD, cta.toString()));
|
|
|
+ booleanQuery.add(query1, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ for (ScoreDoc doc : scoreDocs) {
|
|
|
+ Document document = searcher.doc(doc.doc);
|
|
|
+ Map<String, Object> brand = new HashMap<String, Object>();
|
|
|
+ brand.put("id", Long.parseLong(document.get(SearchConstants.BRAND_ID_FIELD)));
|
|
|
+ brand.put("uuid", document.get(SearchConstants.BRAND_UUID_FIELD));
|
|
|
+ brand.put("nameCn", document.get(SearchConstants.BRAND_NAMECN_FIELD));
|
|
|
+ brands.add(brand);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(searcher);
|
|
|
+ }
|
|
|
+ return brands;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Map<String, Object>> getKindsBySearchComponent(String keyword, String brandId) {
|
|
|
+ Set<Long> kindIds = new HashSet<Long>();
|
|
|
+ List<Map<String, Object>> kinds = new ArrayList<Map<String, Object>>();
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (isKeywordInvalid(keyword)) {
|
|
|
+ throw new IllegalArgumentException("搜索关键词无效");
|
|
|
+ }
|
|
|
+ if (searcher == null) {
|
|
|
+ throw new RuntimeException("获取索引文件失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+
|
|
|
+ keyword = URLDecoder.decode(keyword, "UTF-8");
|
|
|
+ keyword = keyword.toLowerCase();
|
|
|
+ PrefixQuery prefixQuery = new PrefixQuery(new Term(SearchConstants.COMPONENT_CODE_FIELD, keyword));
|
|
|
+ booleanQuery.add(prefixQuery, BooleanClause.Occur.MUST);
|
|
|
+
|
|
|
+ // 筛选品牌
|
|
|
+ if (!StringUtils.isEmpty(brandId)) {
|
|
|
+ TermQuery brandQuery = new TermQuery(new Term(SearchConstants.COMPONENT_BRANDID_FIELD, brandId));
|
|
|
+ booleanQuery.add(brandQuery, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
+ if (hits.totalHits > 0) {
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
+ Document document = searcher.doc(scoreDoc.doc);
|
|
|
+ String cmp_kind_id = document.get(SearchConstants.COMPONENT_KINDID_FIELD);
|
|
|
+ if (!StringUtils.isEmpty(cmp_kind_id)) {
|
|
|
+ kindIds.add(Long.parseLong(cmp_kind_id));
|
|
|
+ Map<String, Object> kind = new HashMap<String, Object>();
|
|
|
+ kind.put("id", Long.parseLong(cmp_kind_id));
|
|
|
+ kind.put("nameCn", document.get(SearchConstants.COMPONENT_KINDNAME_FIELD));
|
|
|
+ kinds.add(kind);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(searcher);
|
|
|
+ }
|
|
|
+ return kinds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Map<String, Object>> getBrandsBySearchComponent(String keyword, String kindId) {
|
|
|
+ Set<Long> brandIds = new HashSet<Long>();
|
|
|
+ List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher searcher = searcherManager.get();
|
|
|
+ if (isKeywordInvalid(keyword) || searcher == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+
|
|
|
+ keyword = URLDecoder.decode(keyword, "UTF-8");
|
|
|
+ keyword = keyword.toLowerCase();
|
|
|
+ PrefixQuery prefixQuery = new PrefixQuery(new Term(SearchConstants.COMPONENT_CODE_FIELD, keyword));
|
|
|
+ booleanQuery.add(prefixQuery, BooleanClause.Occur.MUST);
|
|
|
+
|
|
|
+ // 筛选类目
|
|
|
+ if (!StringUtils.isEmpty(kindId)) {
|
|
|
+ TermQuery kindQuery = new TermQuery(new Term(SearchConstants.COMPONENT_KINDID_FIELD, kindId));
|
|
|
+ booleanQuery.add(kindQuery, BooleanClause.Occur.MUST);
|
|
|
+ }
|
|
|
+
|
|
|
+ TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
+ if (hits.totalHits > 0) {
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
+ Document document = searcher.doc(scoreDoc.doc);
|
|
|
+ String cmp_brand_id = document.get(SearchConstants.COMPONENT_BRANDID_FIELD);
|
|
|
+ if (!StringUtils.isEmpty(cmp_brand_id)) {
|
|
|
+ brandIds.add(Long.parseLong(cmp_brand_id));
|
|
|
+ Map<String, Object> brand = new HashMap<String, Object>();
|
|
|
+ brand.put("id", Long.parseLong(cmp_brand_id));
|
|
|
+ brand.put("uuid", document.get(SearchConstants.COMPONENT_BRANDUUID_FIELD));
|
|
|
+ brand.put("nameCn", document.get(SearchConstants.COMPONENT_BRANDNAME_FIELD));
|
|
|
+ brands.add(brand);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(searcher);
|
|
|
+ }
|
|
|
+ return brands;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|