/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Everest.Search.Convertor;
using Lucene.Net.Highlight;
namespace Everest.Search{
/// <summary>
/// Search use lucene
/// </summary>
public class LuceneSearcher : ISearcher
{
string directory = string.Empty;
IDocumentConvertor defaultConvertor;
/// <summary>
/// Initializes a new instance of the <see cref="LuceneSearcher"/> class.
/// </summary>
public LuceneSearcher()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LuceneSearcher"/> class.
/// </summary>
/// <param name="directory">The directory.</param>
/// <param name="documentConvertor">The document convertor.</param>
public LuceneSearcher(string directory, IDocumentConvertor documentConvertor)
{
this.directory = directory;
defaultConvertor = documentConvertor;
}
#region ISearcher Members
/// <summary>
/// Searches the specified key.
/// default search all the field in the class T;
/// if have configuration ,priority to use the query configuration.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">The key.</param>
/// <param name="start">The start.</param>
/// <param name="limit">The limit.</param>
/// <param name="totalCount">The total count.</param>
/// <returns></returns>
public IEnumerable<T> Search<T>(string key, int start, int limit, out int totalCount) where T : class, new()
{
return Search<T>(key, start, limit, out totalCount, null);
}
#endregion
#region ISearcher Members
/// <summary>
/// Searches the specified key.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">The key.</param>
/// <param name="start">The start.The minimum start index is 1</param>
/// <param name="limit">The limit.</param>
/// <param name="totalCount">The total count.</param>
/// <param name="searchFields">The search fields.</param>
/// <returns></returns>
public IEnumerable<T> Search<T>(string key, int start, int limit, out int totalCount, params SearchOption[] searchOptions)
{
ConfigurationSerivce configService = new ConfigurationSerivce(typeof(T), directory, defaultConvertor);
IndexReader reader = null;
Searcher searcher = null;
try
{
reader = IndexReader.Open(configService.Directory);
searcher = new IndexSearcher(reader);
Query query = configService.ParseQuery(key, searchOptions);
Hits hits = searcher.Search(query);
totalCount = hits.Length();
IList<T> list = new List<T>();
if (start <= totalCount)
{
IDocumentConvertor convertor = configService.GetConvertor();
convertor.SetAnalyzer(configService.Analyzer);
Lucene.Net.Highlight.Highlighter lighter =
new Highlighter(new Lucene.Net.Highlight.QueryScorer(query));
convertor.SetHighlighter(lighter);
int i = start - 1;
if (start < 0)
{
start = 0;
}
while (list.Count < limit && i < hits.Length())
{
T o = (T)convertor.ToObject(hits.Doc(i));
list.Add(o);
i++;
}
}
return list;
}
finally
{
if (searcher != null)
{
searcher.Close();
}
if (reader != null)
{
reader.Close();
}
}
}
#endregion
}
}
|