|
|
@@ -19,7 +19,6 @@ 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;
|
|
|
@@ -47,7 +46,7 @@ public class SearchServiceImpl implements SearchService {
|
|
|
* 默认搜索的最大的记录条数
|
|
|
*/
|
|
|
private static final int TOP_NUM = 1024 * 1024 * 1024;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取联想词时返回的最大数目
|
|
|
*/
|
|
|
@@ -68,15 +67,8 @@ public class SearchServiceImpl implements SearchService {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
- BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
try {
|
|
|
- List<Query> querys = getQuerys(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
- if (!CollectionUtils.isEmpty(querys)) {
|
|
|
- for (Query q : querys) {
|
|
|
- booleanQuery.add(q, BooleanClause.Occur.MUST);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ BooleanQuery booleanQuery = getBooleanQuery(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
@@ -106,15 +98,8 @@ public class SearchServiceImpl implements SearchService {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
- BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
try {
|
|
|
- List<Query> querys = getQuerys(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
- if (!CollectionUtils.isEmpty(querys)) {
|
|
|
- for (Query q : querys) {
|
|
|
- booleanQuery.add(q, BooleanClause.Occur.MUST);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ BooleanQuery booleanQuery = getBooleanQuery(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
for (ScoreDoc doc : scoreDocs) {
|
|
|
@@ -145,14 +130,10 @@ public class SearchServiceImpl implements SearchService {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
- BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
try {
|
|
|
- List<Query> querys = getQuerys(SearchConstants.BRAND_NAMECN_FIELD, keyword);
|
|
|
- if (!CollectionUtils.isEmpty(querys)) {
|
|
|
- for (Query q : querys) {
|
|
|
- booleanQuery.add(q, BooleanClause.Occur.MUST);
|
|
|
- }
|
|
|
- }
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+ booleanQuery.add(getBooleanQuery(SearchConstants.BRAND_NAMECN_FIELD, keyword), BooleanClause.Occur.SHOULD);
|
|
|
+ booleanQuery.add(getBooleanQuery(SearchConstants.BRAND_NAMEEN_FIELD, keyword), BooleanClause.Occur.SHOULD);
|
|
|
|
|
|
TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
@@ -184,14 +165,10 @@ public class SearchServiceImpl implements SearchService {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
- BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
try {
|
|
|
- List<Query> querys = getQuerys(SearchConstants.BRAND_NAMECN_FIELD, keyword);
|
|
|
- if (!CollectionUtils.isEmpty(querys)) {
|
|
|
- for (Query q : querys) {
|
|
|
- booleanQuery.add(q, BooleanClause.Occur.MUST);
|
|
|
- }
|
|
|
- }
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+ booleanQuery.add(getBooleanQuery(SearchConstants.BRAND_NAMECN_FIELD, keyword), BooleanClause.Occur.SHOULD);
|
|
|
+ booleanQuery.add(getBooleanQuery(SearchConstants.BRAND_NAMEEN_FIELD, keyword), BooleanClause.Occur.SHOULD);
|
|
|
|
|
|
TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
@@ -485,15 +462,8 @@ public class SearchServiceImpl implements SearchService {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
- BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
try {
|
|
|
- List<Query> querys = getQuerys(SearchConstants.COMPONENT_CODE_FIELD, componentCode);
|
|
|
- if (!CollectionUtils.isEmpty(querys)) {
|
|
|
- for (Query q : querys) {
|
|
|
- booleanQuery.add(q, BooleanClause.Occur.MUST);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ BooleanQuery booleanQuery = getBooleanQuery(SearchConstants.COMPONENT_CODE_FIELD, componentCode);
|
|
|
TopDocs hits = searcher.search(booleanQuery, SIMILAR_NUM);
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
@@ -529,24 +499,11 @@ public class SearchServiceImpl implements SearchService {
|
|
|
String[] keywords = brandName.split(" ");
|
|
|
for (String keyword : keywords) {
|
|
|
// 搜索nameCn
|
|
|
- BooleanQuery booleanQuery1 = new BooleanQuery();
|
|
|
- List<Query> querys1 = getQuerys(SearchConstants.BRAND_NAMECN_FIELD, keyword);
|
|
|
- if (!CollectionUtils.isEmpty(querys1)) {
|
|
|
- for (Query q : querys1) {
|
|
|
- booleanQuery1.add(q, BooleanClause.Occur.MUST);
|
|
|
- }
|
|
|
- }
|
|
|
- booleanQuery.add(booleanQuery1, BooleanClause.Occur.SHOULD);
|
|
|
-
|
|
|
+ booleanQuery.add(getBooleanQuery(SearchConstants.BRAND_NAMECN_FIELD, keyword),
|
|
|
+ BooleanClause.Occur.SHOULD);
|
|
|
// 搜索nameEn
|
|
|
- BooleanQuery booleanQuery2 = new BooleanQuery();
|
|
|
- List<Query> querys2 = getQuerys(SearchConstants.BRAND_NAMEEN_FIELD, keyword);
|
|
|
- if (!CollectionUtils.isEmpty(querys2)) {
|
|
|
- for (Query q : querys2) {
|
|
|
- booleanQuery1.add(q, BooleanClause.Occur.MUST);
|
|
|
- }
|
|
|
- }
|
|
|
- booleanQuery.add(booleanQuery2, BooleanClause.Occur.SHOULD);
|
|
|
+ booleanQuery.add(getBooleanQuery(SearchConstants.BRAND_NAMEEN_FIELD, keyword),
|
|
|
+ BooleanClause.Occur.SHOULD);
|
|
|
}
|
|
|
|
|
|
TopDocs hits = searcher.search(booleanQuery, SIMILAR_NUM);
|
|
|
@@ -570,17 +527,17 @@ public class SearchServiceImpl implements SearchService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 对搜索词进行分词得到多个Query
|
|
|
+ * 对搜索词进行分词后组合得到BooleanQuery
|
|
|
*
|
|
|
* @param field
|
|
|
* @param keyword
|
|
|
* @return
|
|
|
*/
|
|
|
- private List<Query> getQuerys(String field, String keyword) {
|
|
|
+ private BooleanQuery getBooleanQuery(String field, String keyword) {
|
|
|
if (StringUtils.isEmpty(field) || StringUtils.isEmpty(keyword)) {
|
|
|
return null;
|
|
|
}
|
|
|
- List<Query> querys = new ArrayList<>();
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
@SuppressWarnings("resource")
|
|
|
Analyzer analyzer = new IKAnalyzer(true);
|
|
|
try {
|
|
|
@@ -588,13 +545,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
tokenStream.reset();
|
|
|
CharTermAttribute cta = tokenStream.addAttribute(CharTermAttribute.class);
|
|
|
while (tokenStream.incrementToken()) {
|
|
|
- querys.add(new PrefixQuery(new Term(field, cta.toString())));
|
|
|
+ booleanQuery.add(new PrefixQuery(new Term(field, cta.toString())), BooleanClause.Occur.MUST);
|
|
|
}
|
|
|
tokenStream.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- return querys;
|
|
|
+ return booleanQuery;
|
|
|
}
|
|
|
|
|
|
/**
|