发段代码,看看大家怎样重构
程序员文章站
2024-03-21 19:20:28
...
public void warmUpContactSearcher(IndexSearcher newSearcher) throws Exception {
final Sort sortByName = new Sort(new SortField(FLD_NAME_LAST, SortField.STRING));
final Analyzer analyzer = LuceneSearcher.getAnalyzer();
final QueryParser parser = new QueryParser(FLD_COMPANY_KEYWORDS, analyzer);
final int maxContactResults = 100;
logger.debug("Contact search: company keyword jigsaw");
Query query = getKeywordQuery(LuceneSearcher.FLD_COMPANY_KEYWORDS, "jigsaw");
Sort sort = new Sort(new SortField(LuceneSearcher.FLD_COMPANY_NAME,
SortField.STRING, false));
newSearcher.search(query, null, maxContactResults, sort);
logger.debug("Contact search: company keyword intel");
query = new TermQuery(new Term(FLD_COMPANY_KEYWORDS, "intel"));
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: company keyword Intel Corporation");
query = LuceneSearcher.getInstance().getKeywordQuery(FLD_COMPANY_KEYWORDS,
"Intel Corporation");
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: company keyword :intel");
query = parser.parse(FLD_COMPANY_KEYWORDS + ":Intel");
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: title keyword director");
query = new TermQuery(new Term(FLD_TITLE_KEYWORDS, "director"));
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: title keyword :Director");
query = parser.parse(FLD_TITLE_KEYWORDS + ":Director");
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: company class1 1100000");
query = new TermQuery(new Term(FLD_COMPANY_CLASS1, "1100000"));
newSearcher.search(query, null, maxContactResults, sortByName);
char lastChar = 'z';
String alphabet = "abcdefghijklmnopqrstuvwxy";
for (int i = 0; i < alphabet.length(); i++) {
String lowerName = alphabet.substring(i, i + 1);
logger.debug("Contact search: last name " + lowerName + "-" + lastChar);
query = new ConstantScoreRangeQuery(FLD_NAME_FIRST, lowerName,
String.valueOf(lastChar), true, false);
newSearcher.search(query, null, maxContactResults, sortByName);
}
logger.debug("Contact search: last name smith");
query = new TermQuery(new Term(FLD_NAME_LAST, "smith"));
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: company keyword intel, title keyword director");
BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(new TermQuery(new Term(FLD_COMPANY_KEYWORDS, "intel")),
BooleanClause.Occur.MUST);
booleanQuery.add(new TermQuery(new Term(FLD_TITLE_KEYWORDS, "director")),
BooleanClause.Occur.MUST);
newSearcher.search(booleanQuery, null, maxContactResults, sortByName);
logger.debug("Contact search: company keyword intel, title keyword :Director");
query = parser.parse("+" + FLD_COMPANY_KEYWORDS + ":Intel +" + FLD_TITLE_KEYWORDS
+ ":Director");
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: dept1 " + Constant.DEPT_ITIS);
query = new TermQuery(new Term(FLD_DEPT1, "" + Constant.DEPT_ITIS));
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: dept1 " + Constant.DEPT_ITIS +
", rank directory, state CA ");
booleanQuery = new BooleanQuery();
Query dept = new TermQuery(new Term(FLD_DEPT1, "" + Constant.DEPT_ITIS));
booleanQuery.add(dept, BooleanClause.Occur.MUST);
Query rank = new TermQuery(new Term(FLD_RANK, "" + Constant.RANK_DIRECTOR));
booleanQuery.add(rank, BooleanClause.Occur.MUST);
Query state = new TermQuery(new Term(FLD_STATE, ""
+ GeoConstant.US_STATE_CALIFORNIA));
booleanQuery.add(state, BooleanClause.Occur.MUST);
newSearcher.search(booleanQuery, null, maxContactResults, sortByName);
logger.debug("Contact search: area code 415");
query = new TermQuery(new Term(FLD_AREA_CODE, "415"));
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: rank other");
query = new TermQuery(new Term(FLD_RANK, "500"));
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: state 209");
query = new TermQuery(new Term(FLD_STATE, "209"));
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: company id C- 0000023080");
query = new TermQuery(new Term(FLD_COMPANY_ID, "C- 0000023080"));
newSearcher.search(query, null, maxContactResults, sortByName);
logger.debug("Contact search: last name aa - al");
booleanQuery = new BooleanQuery();
booleanQuery.add(new TermQuery(new Term(FLD_IS_SEARCH_DISABLED,
Boolean.FALSE.toString())), BooleanClause.Occur.MUST);
booleanQuery.add(new ConstantScoreRangeQuery(FLD_NAME_LAST, "aa", "al", true,
false), BooleanClause.Occur.MUST);
newSearcher.search(booleanQuery, null, maxContactResults, sortByName);
logger.debug("Contact search: last name aa - al");
String queryText = "+(class1:1020000 class2:1020000 class3:1020000 class1:1030000 class2:1030000 class3:1030000 class1:1230000 class2:1230000 class3:1230000 class1:1060000 class2:1060000 class3:1060000 class1:1070000 class2:1070000 class3:1070000 class1:1100000 class2:1100000 class3:1100000 class1:1110000 class2:1110000 class3:1110000 class1:1120000 class2:1120000 class3:1120000 class1:1140000 class2:1140000 class3:1140000 class1:1160000 class2:1160000 class3:1160000 class1:1180000 class2:1180000 class3:1180000 class1:1190000 class2:1190000 class3:1190000 class1:1200000 class2:1200000 class3:1200000 class1:1210000 class2:1210000 class3:1210000 class1:1220000 class2:1220000 class3:1220000) +type:1 +(employees:40 employees:20 employees:30) +(revenue:40 revenue:20 revenue:30) +(rank:10 rank:30 rank:20) +(dept1:10 dept2:10 dept3:10 dept1:20 dept2:20 dept3:20) +country:9000 +state:9005";
logger.debug("Contact search: " + queryText);
query = parser.parse(queryText);
newSearcher.search(query, null, maxContactResults, sortByName);
}
上一篇: Java学习笔记-多态
下一篇: 向上转型和向下转型
推荐阅读