- public class LuceneDBSearcher: LuceneDB
- {
- private Type _searchType;
- public
- LuceneDBSearcher(
- string
- path) :
- base(path)
- {
- }
- public LuceneDBSearcher(Type type)
- {
- SetDBPath(type);
- }
- public LuceneDBSearcher() { }
- public Type SearchType
- {
- set
- {
- //判断该类型是否实现 某 约定
- _searchType =
- value;
- }
- get
- {
- return _searchType; }
- }
- public
- IEnumerable Search(
- string
- searchText, IEnumerable<
- string
- > fields,
- int
- page,
- int
- pageSize, Dictionary<
- string
- ,
- string
- > condition=
- null
- )
- where
- T :
- new()
- {
- return
- GetModels
- (SearchText(searchText, fields, page, pageSize, condition));
- }
- private
- IEnumerable SearchText(
- string
- searchText,IEnumerable<
- string
- > fields,
- int
- page,
- int
- pageSize, Dictionary<
- string
- ,
- string
- >
- condition)
- {
- StringBuilder conditionWhere
- =
- new StringBuilder();
- foreach
- (
- var
- item
- in condition)
- {
- conditionWhere.Append(" +"
- + item.Key +
- ":"
- +
- item.Value);
- }
- Open();
- var
- parser =
- new Lucene.Net.QueryParsers.MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields.ToArray(), _analyzer);
- var
- search =
- new
- Lucene.Net.Search.IndexSearcher(_luDirectory,
- true);
- var
- query = parser.Parse(
- "+"
- + searchText +
- conditionWhere.ToString());
- var
- searchDocs = search.Search(query,
- 100).ScoreDocs;
- return
- searchDocs.Select(t =>
- search.Doc(t.Doc));
- }
- protected override void Open()
- {
- _luDirectory
- = Lucene.Net.Store.FSDirectory.Open(
- new System.IO.DirectoryInfo(DBPath));
- if (Lucene.Net.Index.IndexWriter.IsLocked(_luDirectory))
- {
- Lucene.Net.Index.IndexWriter.Unlock(_luDirectory);
- }
- var
- lockFilePath = System.IO.Path.Combine(DBPath,
- "write.lock");
- if (System.IO.File.Exists(lockFilePath))
- {
- System.IO.File.Delete(lockFilePath);
- }
- _analyzer
- =
- new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
- }
- private
- IEnumerable GetModels(IEnumerable documents)
- where
- T:
- new()
- {
- var
- type =
- typeof(T);
- var
- props = type.GetProperties().Where(prop =>
- null
- != prop.GetCustomAttribute(
- typeof
- (IndexerAttribute),
- true));
- var
- objs =
- new
- List
- ();
- foreach
- (
- var
- document
- in documents)
- {
- var
- obj =
- new T();
- foreach
- (
- var
- prop
- in props)
- {
- var
- attr = prop.GetCustomAttribute
- ();
- if
- (
- null
- != attr &&
- attr.Store)
- {
- object
- v =
- Convert.ChangeType(document.Get(prop.Name), prop.PropertyType);
- prop.SetValue(obj, v);
- }
- }
- objs.Add(obj);
- }
- return objs;
- }
- private
- T GetModel(Document document)
- where
- T :
- new()
- {
- var
- type =
- typeof(T);
- var
- props = type.GetProperties().Where(prop =>
- null
- != prop.GetCustomAttribute(
- typeof
- (IndexerAttribute),
- true));
- var
- obj =
- new T();
- foreach
- (
- var
- prop
- in props)
- {
- var
- attr = prop.GetCustomAttribute
- ();
- if
- (
- null
- != attr &&
- attr.Store)
- {
- object
- v =
- Convert.ChangeType(document.Get(prop.Name), prop.PropertyType);
- prop.SetValue(obj, v);
- }
- }
- return obj;
- }
- public override void Dispose()
- {
- _analyzer.Dispose();
- base.Dispose();
- }
- }
来源: http://www.cnblogs.com/cheesebar/p/7192554.html