|
|
@@ -12,11 +12,9 @@ import java.util.Set;
|
|
|
|
|
|
import org.apache.lucene.document.Document;
|
|
|
import org.apache.lucene.index.Term;
|
|
|
-import org.apache.lucene.sandbox.queries.DuplicateFilter;
|
|
|
import org.apache.lucene.search.BooleanClause;
|
|
|
import org.apache.lucene.search.BooleanClause.Occur;
|
|
|
import org.apache.lucene.search.BooleanQuery;
|
|
|
-import org.apache.lucene.search.FilteredQuery;
|
|
|
import org.apache.lucene.search.IndexSearcher;
|
|
|
import org.apache.lucene.search.NumericRangeQuery;
|
|
|
import org.apache.lucene.search.PrefixQuery;
|
|
|
@@ -206,9 +204,7 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
if (!SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
- keyword = keyword.toLowerCase();
|
|
|
- PrefixQuery prefixQuery = new PrefixQuery(new Term(SearchConstants.COMPONENT_CODE_FIELD, keyword));
|
|
|
- booleanQuery.add(prefixQuery, BooleanClause.Occur.MUST);
|
|
|
+ booleanQuery.add(setBoost(keyword), BooleanClause.Occur.MUST);
|
|
|
}
|
|
|
|
|
|
Map<String, Object> filters = pageParams.getFilters();
|
|
|
@@ -297,15 +293,18 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
booleanQuery.add(booleanQuery2, Occur.MUST);
|
|
|
}
|
|
|
logger.info(booleanQuery.toString());
|
|
|
+
|
|
|
+ Sort sort = new Sort(SortField.FIELD_SCORE);
|
|
|
+
|
|
|
TopDocs hits;
|
|
|
if (pageParams.getPage() > 1) {// 不是第一页
|
|
|
TopDocs previousHits = indexSearcher.search(booleanQuery,
|
|
|
- (pageParams.getPage() - 1) * pageParams.getSize());
|
|
|
+ (pageParams.getPage() - 1) * pageParams.getSize(), sort, true, false);
|
|
|
ScoreDoc[] previousScoreDocs = previousHits.scoreDocs;
|
|
|
ScoreDoc after = previousScoreDocs[previousScoreDocs.length - 1];
|
|
|
- hits = indexSearcher.searchAfter(after, booleanQuery, pageParams.getSize());
|
|
|
+ hits = indexSearcher.searchAfter(after, booleanQuery, pageParams.getSize(), sort, true, false);
|
|
|
} else {
|
|
|
- hits = indexSearcher.search(booleanQuery, pageParams.getSize());
|
|
|
+ hits = indexSearcher.search(booleanQuery, pageParams.getSize(), sort, true, false);
|
|
|
}
|
|
|
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
@@ -316,6 +315,9 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
Document document = indexSearcher.doc(scoreDoc.doc, fieldsToLoad);
|
|
|
String componentId = document.get(SearchConstants.COMPONENT_ID_FIELD);
|
|
|
ids.add(Long.parseLong(componentId));
|
|
|
+ // System.out.println(indexSearcher.explain(booleanQuery,
|
|
|
+ // scoreDoc.doc).toString());
|
|
|
+ System.out.println(componentId + "\t" + scoreDoc.score);
|
|
|
}
|
|
|
map.put("componentIds", ids);
|
|
|
map.put("page", pageParams.getPage());
|
|
|
@@ -329,6 +331,32 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 同时搜索器件、类目、品牌,并设置boost
|
|
|
+ *
|
|
|
+ * @param keyword
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Query setBoost(String keyword) {
|
|
|
+ BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+ PrefixQuery prefixQuery = new PrefixQuery(
|
|
|
+ new Term(SearchConstants.COMPONENT_CODE_FIELD, keyword.toLowerCase()));
|
|
|
+ prefixQuery.setBoost(100);
|
|
|
+ booleanQuery.add(prefixQuery, BooleanClause.Occur.SHOULD);
|
|
|
+ BooleanQuery kindNameQuery = SearchUtils.getBooleanQuery(SearchConstants.COMPONENT_KINDNAME_FIELD, keyword);
|
|
|
+ kindNameQuery.setBoost(10);
|
|
|
+ booleanQuery.add(kindNameQuery, BooleanClause.Occur.SHOULD);
|
|
|
+ BooleanQuery brandNameCnQuery = SearchUtils.getBooleanQuery(SearchConstants.COMPONENT_BRANDNAMECN_FIELD,
|
|
|
+ keyword);
|
|
|
+ brandNameCnQuery.setBoost(1);
|
|
|
+ booleanQuery.add(brandNameCnQuery, BooleanClause.Occur.SHOULD);
|
|
|
+ BooleanQuery brandNameEnQuery = SearchUtils.getBooleanQuery(SearchConstants.COMPONENT_BRANDNAMEEN_FIELD,
|
|
|
+ keyword);
|
|
|
+ brandNameEnQuery.setBoost(1);
|
|
|
+ booleanQuery.add(brandNameEnQuery, BooleanClause.Occur.SHOULD);
|
|
|
+ return booleanQuery;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Set<Long> getKindIdsBySearchComponent(String keyword, String brandId) {
|
|
|
if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
@@ -341,9 +369,7 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
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);
|
|
|
+ booleanQuery.add(setBoost(keyword), BooleanClause.Occur.MUST);
|
|
|
|
|
|
// 筛选品牌
|
|
|
if (!StringUtils.isEmpty(brandId)) {
|
|
|
@@ -399,9 +425,7 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
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);
|
|
|
+ booleanQuery.add(setBoost(keyword), BooleanClause.Occur.MUST);
|
|
|
|
|
|
// 筛选类目
|
|
|
if (!StringUtils.isEmpty(kindId)) {
|