1、使用queryparser完成解析搜索请求
2、基本格式如:
QueryParser parser=new QueryParser("字段名称","分析器实例");
Query q=parser.parse("关键词")
3、例如:解析一个关键字太阳
QueryParser parser=new QueryParser("context",new StandardAnalyzer());
Query q=parser.parse("太阳");
IndexSearcher searcher=new IndexSearcher(indexpath);
Hits hit=searcher.search(q);
PhraseQuery使用位置信息来进行相关查询,比如TermQuery使用“我们”和“祖国”进行查询,那么文档中含有这两个词的所有记录都会被查询出来。但是有一种情况,我们可能需要查询“我们”和“中国”之间只隔一个字和两个字或者两个字等,而不是它们之间字距相差十万八千里,就可以使用PhraseQuery。
PhraseQuery正是Lucene所提供的满足上述需求的一种Query对象。它的add方法可以让用户往其内部添加关键字,在添加完毕后,用户还可以通过setSlop()方法来设定一个称之为“坡度”的变量来确定关键字之间是否允许、允许多少个无关词汇的存在。
第一种分页
//起始页
var num = (Convert.ToInt32(pageindex) - 1) * Convert.ToInt32(pageSize);
//读取字典
var searcher = LucenceHelp.GetDictPath();
//搜索条件
var query = new PhraseQuery();
//对搜索条件进行盘古分词
var panGuList = LucenceHelp.GetPanGuAnalyzer(name);
foreach (string word in panGuList)//先用空格,让用户去分词,空格分隔的就是词“计算机 专业”
{
query.Add(new Term("body", word));
}
//关键词之间可以允许的距离
query.SetSlop(5);
//创建容器TopDocCollector类继承自HitCollector,而HitCollector抽象类定义了实现查询(queries)、排序(sorting)、过滤(filtering)的功能
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
//根据query查询条件进行查询,查询结果放入collector容器
searcher.Search(query, null, collector);
//分页查询
ScoreDoc[] docs = collector.TopDocs(num, pageSize).scoreDocs;
//总条数
var Count = collector.GetTotalHits().ToString();
var list = new List<string>();
for (int i = 0; i < docs.Length; i++)
{
//得到查询结果文档的id(Lucene内部分配的id)
int docId = docs[i].doc;
//找到文档id对应的文档详细信息
Document doc = searcher.Doc(docId);
var _str = doc.Get("body");
list.Add(_str);
}
list.Insert(0, Count);
return list;
第二种
//起始页
var num = (Convert.ToInt32(pageindex) - 1) * Convert.ToInt32(pageSize);
//读取字典
var searcher = LucenceHelp.GetDictPath();
//生成hits结果对象,保存返回的检索结果
Hits myhit = null;
var q = new QueryParser("body", new StandardAnalyzer());
Query query = q.Parse(name);
var f = new SortField("number", SortField.FLOAT, true);//true表示降序
var sort = new Sort();
sort.SetSort(f);
myhit = searcher.Search(query, sort);
#region 分页查询数据
var list = new List<string>();
for (int i = num; i < myhit.Length(); i++)
{
if (i <= Convert.ToInt32(pageindex) * Convert.ToInt32(pageSize))
{
var str = myhit.Doc(i);
var bname = str.Get("body");
list.Add(bname);
}
}
var Count = myhit.Length().ToString();
list.Insert(0, Count);
#endregion
return list;
多条件排序
var oneTxt = Request.Form["onetxt"];
var twoTxt = Request.Form["twotxt"];
var type = Request.Form["type"];
//读取字典
var searcher = LucenceHelp.GetDictPath();
//根据id降序排列
var t = new Sort("number", true);
#region 多条件组合查询
var query = new BooleanQuery();
Analyzer analyzer = new PanGuAnalyzer();
var query2 = new QueryParser("body", analyzer);
if (!string.IsNullOrWhiteSpace(oneTxt) && !string.IsNullOrWhiteSpace(twoTxt))
{
var tquery1 = query2.Parse(oneTxt);
var tquery2 = query2.Parse(twoTxt);
if (type == "must")
{
query.Add(tquery1, BooleanClause.Occur.MUST);
query.Add(tquery2, BooleanClause.Occur.MUST);
}
else if (type == "or")
{
query.Add(tquery1, BooleanClause.Occur.SHOULD);
query.Add(tquery2, BooleanClause.Occur.SHOULD);
}
}
else if (!string.IsNullOrWhiteSpace(oneTxt) && string.IsNullOrWhiteSpace(twoTxt))
{
var tquery1 = query2.Parse(oneTxt);
if (type == "must")
{
query.Add(tquery1, BooleanClause.Occur.MUST);
}
else if (type == "or")
{
query.Add(tquery1, BooleanClause.Occur.SHOULD);
}
}
else
{
var tquery2 = query2.Parse(twoTxt);
if (type == "must")
{
query.Add(tquery2, BooleanClause.Occur.MUST);
}
else if (type == "or")
{
query.Add(tquery2, BooleanClause.Occur.SHOULD);
}
}
#endregion
Hits hits; //结果集
//hits = searcher.Search(query); 不排序
hits = searcher.Search(query, t); //排序
var list = new List<string>();
for (int i = 0; i < hits.Length(); i++)
{
var str = hits.Doc(i);
var bname = str.Get("body");
list.Add(bname);
}
ViewBag.List = list;
帮助类
public class LucenceHelp
{
//读取字典
public static IndexSearcher GetDictPath()
{
//索引库文件夹的路径
var path = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["IndexDateBase"];
//判断是否存在索引库文件夹,如果没有则创建
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
string indexPath = path + "\\";
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
var searcher = new IndexSearcher(reader);
return searcher;
}
//获取目录
public static FSDirectory GetDirectory()
{
//索引库文件夹的路径
var path = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["IndexDateBase"];
//判断是否存在索引库文件夹,如果没有则创建
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
string indexPath = path + "\\";
var directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
return directory;
}
// 对传递过来的字符串进行盘古分词
public static List<string> GetPanGuAnalyzer(string txt)
{
List<string> list = new List<string>();
Analyzer analyzer = new PanGuAnalyzer();
TokenStream tokenStream = analyzer.TokenStream("", new StringReader(txt));
Lucene.Net.Analysis.Token token = null;
while ((token = tokenStream.Next()) != null)
{
list.Add(token.TermText());
}
return list;
}
}