C# 如何使用lucene.net搜索特定数据
通常,我们使用类似sql的方法在数据库中搜索特定数据C# 如何使用lucene.net搜索特定数据,c#,search,lucene.net,C#,Search,Lucene.net,通常,我们使用类似sql的方法在数据库中搜索特定数据 select empid,empname,salary from employee where empname ='keith' OR select empid,empname,salary from employee where empid =2050 在这里,我粘贴了使用lucene搜索数据的代码 public static SearchResult BeginSearch(string strSearchTerm, int PageI
select empid,empname,salary from employee where empname ='keith'
OR
select empid,empname,salary from employee where empid =2050
在这里,我粘贴了使用lucene搜索数据的代码
public static SearchResult BeginSearch(string strSearchTerm, int PageIndex, int PageSize = 10)
{
int StartRecPos = 0, TmpEndRecpos = 0, EndRecpos = 0;
string multiWordPhrase = "";
multiWordPhrase = strSearchTerm.Replace("*", "").Replace("?", "").Replace("~", "");
IndexSearcher searcher = null;
SearchResult list = new SearchResult();
list.HasData = 0;
Result oSr = null;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
if (_directory.FileExists("segments.gen"))
{
if (!string.IsNullOrEmpty(multiWordPhrase))
{
string[] fieldList = { "Title", "Description", "Url" };
List<BooleanClause.Occur> occurs = new List<BooleanClause.Occur>();
foreach (string field in fieldList)
{
occurs.Add(BooleanClause.Occur.SHOULD);
}
searcher = new IndexSearcher(_directory, false);
Query qry = MultiFieldQueryParser.Parse(Version.LUCENE_29, multiWordPhrase, fieldList, occurs.ToArray(), new StandardAnalyzer(Version.LUCENE_29));
TopDocs topDocs = searcher.Search(qry, null, ((PageIndex + 1) * PageSize), Sort.RELEVANCE);
ScoreDoc[] scoreDocs = topDocs.ScoreDocs;
int resultsCount = topDocs.TotalHits;
list.HasData = resultsCount;
StartRecPos = (PageIndex * PageSize) + 1;
if (topDocs != null)
{
for (int i = (PageIndex * PageSize); i <= (((PageIndex + 1) * PageSize) - 1) && i < topDocs.ScoreDocs.Length; i++)
{
Document doc = searcher.Doc(topDocs.ScoreDocs[i].doc);
oSr = new Result();
oSr.ID = doc.Get("ID");
oSr.Title = doc.Get("Title");
oSr.Description = doc.Get("Description");
//oSr.WordCount = AllExtension.WordCount(oSr.Description, WordExist(oSr.Title, multiWordPhrase));
string preview =
oSr.Description = BBAReman.AllExtension.HighlightKeywords(oSr.Description, multiWordPhrase); //sr.Description;
oSr.Url = doc.Get("Url");
TmpEndRecpos++;
list.Result.Add(oSr);
}
}
if (StartRecPos > 1)
{
EndRecpos = (TmpEndRecpos + StartRecPos) - 1;
}
else
{
EndRecpos = TmpEndRecpos;
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}", ts.Seconds, ts.Milliseconds);
PageSize = (resultsCount < PageSize ? resultsCount : PageSize);
list.Pager = SimplePager.GetLinks(PageIndex + 1, resultsCount, PageSize, "Search.aspx?Page={0}&SearchTerm=" + strSearchTerm, "pagerSC", new ResultStatus((PageIndex), PageSize, resultsCount, elapsedTime, StartRecPos, EndRecpos));
searcher.Close();
}
}
return list;
}
private static Query parseQuery(string searchQuery, QueryParser parser)
{
Query query;
try
{
query = parser.Parse(searchQuery.Trim());
}
catch (ParseException)
{
query = parser.Parse(QueryParser.Escape(searchQuery.Trim()));
}
return query;
}
告诉我上面的代码是对的?哪个可以帮助我搜索特定用户或员工ID的数据
请指导我在BeginSearch例程中需要修改哪些内容,使我能够搜索特定用户的数据。谢谢
public void UserSpecificSearch() throws Exception {
TermQuery query = new TermQuery(new Term("keywords", "info"));
IndexSearcher searcher = new IndexSearcher(directory);
TopDocs hits = searcher.search(query, 10);
assertEquals("Both documents match", 2, hits.totalHits);
Filter jakeFilter = new QueryWrapperFilter(
new TermQuery(new Term("owner", "jake")));
hits = searcher.search(query, jakeFilter, 10);
assertEquals(1, hits.totalHits);
assertEquals("elwood is safe",
"jake's sensitive info",
searcher.doc(hits.scoreDocs[0].doc).get("keywords"));
}