高级搜索1

public List<XueBa.Entity.Document> SearchDocument(DocumentQuery queryStr)
{
BooleanQuery query = new BooleanQuery();
if (queryStr.NotOccurStr != null)
{
foreach (string word in queryStr.NotOccurStr)
{
Query titleQueryNot = new TermQuery(
new Term("title", word)
);
query.Add(titleQueryNot, BooleanClause.Occur.MUST_NOT);
Query contentQueryNot = new TermQuery(
new Term("content", word)
);
query.Add(contentQueryNot, BooleanClause.Occur.MUST_NOT);
}
}

Query titleQuery = queryParser.Parse(GetKeyWordsSplitBySpace(queryStr.KeyWords,keyWordTokenizer));
query.Add(titleQuery, BooleanClause.Occur.SHOULD);
Query contentQuery = contentParser.Parse(GetKeyWordsSplitBySpace(queryStr.KeyWords, keyWordTokenizer));
query.Add(contentQuery, BooleanClause.Occur.SHOULD);

if (queryStr.Author != null)
{
Query authorQuery = new TermQuery(new Term("author", queryStr.Author));
query.Add(authorQuery, BooleanClause.Occur.MUST);
}
if (queryStr.Institution != null)
{
Query instQuery = new TermQuery(new Term("institution", queryStr.Institution));
query.Add(instQuery, BooleanClause.Occur.MUST);
}
ScoreDoc[] result = DocumentIndexSearcher.Search(query, null, 100).scoreDocs;
List<XueBa.Entity.Document> list = fillDocResultList(result);
return list;
}

单元测试通过,太晚了明天有问题再改

posted @ 2012-12-06 01:27  MagicCode1023  阅读(205)  评论(0编辑  收藏  举报