Bladeren bron

代码清理

懒得勤快 6 jaren geleden
bovenliggende
commit
e30a8af9be

+ 0 - 2
Masuit.LuceneEFCore.SearchEngine/Extensions/DocumentExtension.cs

@@ -5,7 +5,6 @@ namespace Masuit.LuceneEFCore.SearchEngine.Extensions
 {
     public static class DocumentExtension
     {
-
         /// <summary>
         /// 获取文档的值
         /// </summary>
@@ -82,6 +81,5 @@ namespace Masuit.LuceneEFCore.SearchEngine.Extensions
                     return Convert.ChangeType(value, typeof(T));
             }
         }
-
     }
 }

+ 2 - 6
Masuit.LuceneEFCore.SearchEngine/Extensions/StringHelpers.cs

@@ -14,11 +14,7 @@ namespace Masuit.LuceneEFCore.SearchEngine.Extensions
         /// <returns></returns>
         public static string RemoveCharacters(this string s, IEnumerable<char> chars)
         {
-            if (string.IsNullOrEmpty(s))
-            {
-                return string.Empty;
-            }
-            return new string(s.Where(c => !chars.Contains(c)).ToArray());
+            return string.IsNullOrEmpty(s) ? string.Empty : new string(s.Where(c => !chars.Contains(c)).ToArray());
         }
 
         /// <summary>
@@ -28,7 +24,7 @@ namespace Masuit.LuceneEFCore.SearchEngine.Extensions
         /// <returns></returns>
         public static string RemoveHtmlTag(this string html)
         {
-            string strText = Regex.Replace(html, "<[^>]+>", "");
+            var strText = Regex.Replace(html, "<[^>]+>", "");
             strText = Regex.Replace(strText, "&[^;]+;", "");
             return strText;
         }

+ 41 - 16
Masuit.LuceneEFCore.SearchEngine/Linq/LinqExtension.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using System.Linq.Expressions;
 
 namespace Masuit.LuceneEFCore.SearchEngine.Linq
@@ -11,35 +12,59 @@ namespace Masuit.LuceneEFCore.SearchEngine.Linq
         /// <summary>
         /// 与连接
         /// </summary>
-        /// <typeparam name="T"></typeparam>
+        /// <typeparam name="T">类型</typeparam>
         /// <param name="left">左条件</param>
         /// <param name="right">右条件</param>
-        /// <returns></returns>
+        /// <returns>新表达式</returns>
         public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
         {
-            var dateExpr = Expression.Parameter(typeof(T));
-            var parameterReplacer = new ParameterReplacer(dateExpr);
-            var leftwhere = parameterReplacer.Replace(left.Body);
-            var rightwhere = parameterReplacer.Replace(right.Body);
-            var body = Expression.And(leftwhere, rightwhere);
-            return Expression.Lambda<Func<T, bool>>(body, dateExpr);
+            return CombineLambdas(left, right, ExpressionType.AndAlso);
         }
 
         /// <summary>
         /// 或连接
         /// </summary>
-        /// <typeparam name="T"></typeparam>
+        /// <typeparam name="T">类型</typeparam>
         /// <param name="left">左条件</param>
         /// <param name="right">右条件</param>
-        /// <returns></returns>
+        /// <returns>新表达式</returns>
         public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
         {
-            var dateExpr = Expression.Parameter(typeof(T));
-            var parameterReplacer = new ParameterReplacer(dateExpr);
-            var leftwhere = parameterReplacer.Replace(left.Body);
-            var rightwhere = parameterReplacer.Replace(right.Body);
-            var body = Expression.Or(leftwhere, rightwhere);
-            return Expression.Lambda<Func<T, bool>>(body, dateExpr);
+            return CombineLambdas(left, right, ExpressionType.OrElse);
+        }
+
+        private static Expression<Func<T, bool>> CombineLambdas<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right, ExpressionType expressionType)
+        {
+            if (IsExpressionBodyConstant(left))
+            {
+                return right;
+            }
+
+            var visitor = new SubstituteParameterVisitor
+            {
+                Sub =
+                {
+                    [right.Parameters[0]] = left.Parameters[0]
+                }
+            };
+
+            Expression body = Expression.MakeBinary(expressionType, left.Body, visitor.Visit(right.Body));
+            return Expression.Lambda<Func<T, bool>>(body, left.Parameters[0]);
+        }
+
+        private static bool IsExpressionBodyConstant<T>(Expression<Func<T, bool>> left)
+        {
+            return left.Body.NodeType == ExpressionType.Constant;
+        }
+
+        internal class SubstituteParameterVisitor : ExpressionVisitor
+        {
+            public Dictionary<Expression, Expression> Sub = new Dictionary<Expression, Expression>();
+
+            protected override Expression VisitParameter(ParameterExpression node)
+            {
+                return Sub.TryGetValue(node, out var newValue) ? newValue : node;
+            }
         }
     }
 }

+ 0 - 43
Masuit.LuceneEFCore.SearchEngine/Linq/ParameterReplacer.cs

@@ -1,43 +0,0 @@
-using System.Linq.Expressions;
-
-namespace Masuit.LuceneEFCore.SearchEngine.Linq
-{
-    /// <summary>
-    /// linq参数替换器
-    /// </summary>
-    public class ParameterReplacer : ExpressionVisitor
-    {
-        /// <summary>
-        /// linq参数替换器
-        /// </summary>
-        /// <param name="paramExpr"></param>
-        public ParameterReplacer(ParameterExpression paramExpr)
-        {
-            this.ParameterExpression = paramExpr;
-        }
-
-        /// <summary>
-        /// 参数表达式
-        /// </summary>
-        public ParameterExpression ParameterExpression { get; private set; }
-
-        /// <summary>
-        /// 表达式替换
-        /// </summary>
-        /// <param name="expr"></param>
-        /// <returns></returns>
-        public Expression Replace(Expression expr)
-        {
-            return this.Visit(expr);
-        }
-        /// <summary>
-        /// 表达式参数访问
-        /// </summary>
-        /// <param name="p"></param>
-        /// <returns></returns>
-        protected override Expression VisitParameter(ParameterExpression p)
-        {
-            return this.ParameterExpression;
-        }
-    }
-}

+ 3 - 6
Masuit.LuceneEFCore.SearchEngine/LuceneIndexChangeset.cs

@@ -55,12 +55,9 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// 构造函数
         /// </summary>
         /// <param name="change">被修改的实体</param>
-        public LuceneIndexChangeset(LuceneIndexChange change)
+        public LuceneIndexChangeset(LuceneIndexChange change) => Entries = new List<LuceneIndexChange>
         {
-            Entries = new List<LuceneIndexChange>
-            {
-                change
-            };
-        }
+            change
+        };
     }
 }

+ 7 - 11
Masuit.LuceneEFCore.SearchEngine/LuceneIndexSearcher.cs

@@ -114,28 +114,25 @@ namespace Masuit.LuceneEFCore.SearchEngine
                 if (options.Fields.Count == 1)
                 {
                     // 单字段搜索
-                    QueryParser queryParser = new QueryParser(Lucene.Net.Util.LuceneVersion.LUCENE_48, options.Fields[0], _analyzer);
+                    var queryParser = new QueryParser(Lucene.Net.Util.LuceneVersion.LUCENE_48, options.Fields[0], _analyzer);
                     query = queryParser.Parse(options.Keywords);
                 }
                 else
                 {
                     // 多字段搜索
-                    MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(Lucene.Net.Util.LuceneVersion.LUCENE_48, options.Fields.ToArray(), _analyzer, options.Boosts);
+                    var multiFieldQueryParser = new MultiFieldQueryParser(Lucene.Net.Util.LuceneVersion.LUCENE_48, options.Fields.ToArray(), _analyzer, options.Boosts);
                     query = GetFuzzyquery(multiFieldQueryParser, options.Keywords);
                 }
 
-                List<SortField> sortFields = new List<SortField>
+                var sortFields = new List<SortField>
                 {
                     SortField.FIELD_SCORE
                 };
+                sortFields.AddRange(options.OrderBy.Select(sortField => new SortField(sortField, SortFieldType.STRING)));
 
                 // 排序规则处理
-                foreach (var sortField in options.OrderBy)
-                {
-                    sortFields.Add(new SortField(sortField, SortFieldType.STRING));
-                }
 
-                Sort sort = new Sort(sortFields.ToArray());
+                var sort = new Sort(sortFields.ToArray());
                 Expression<Func<ScoreDoc, bool>> where = _ => true;
                 if (options.Type != null)
                 {
@@ -190,9 +187,8 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <returns></returns>
         public ILuceneSearchResultCollection ScoredSearch(SearchOptions options)
         {
-            Stopwatch sw = new Stopwatch();
             ILuceneSearchResultCollection results;
-            sw.Start();
+            var sw = Stopwatch.StartNew();
             try
             {
                 results = PerformSearch(options, false);
@@ -221,7 +217,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <returns></returns>
         public ILuceneSearchResultCollection ScoredSearch(string keywords, string fields, int maximumNumberOfHits, Dictionary<string, float> boosts, Type type, string sortBy, int? skip, int? take)
         {
-            SearchOptions options = new SearchOptions(keywords, fields, maximumNumberOfHits, boosts, type, sortBy, skip, take);
+            var options = new SearchOptions(keywords, fields, maximumNumberOfHits, boosts, type, sortBy, skip, take);
             return ScoredSearch(options);
         }
     }

+ 8 - 9
Masuit.LuceneEFCore.SearchEngine/LuceneIndexableBaseEntity.cs

@@ -2,8 +2,6 @@
 using Masuit.LuceneEFCore.SearchEngine.Extensions;
 using Masuit.LuceneEFCore.SearchEngine.Interfaces;
 using Newtonsoft.Json;
-using System;
-using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations.Schema;
 using System.Reflection;
@@ -53,21 +51,22 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <returns></returns>
         public virtual Document ToDocument()
         {
-            Document doc = new Document();
-            Type type = GetType();
+            var doc = new Document();
+            var type = GetType();
             if (type.Assembly.IsDynamic && type.FullName.Contains("Prox"))
             {
                 type = type.BaseType;
             }
-            PropertyInfo[] classProperties = type.GetProperties();
+
+            var classProperties = type.GetProperties();
             doc.Add(new Field("Type", type.AssemblyQualifiedName, Field.Store.YES, Field.Index.NOT_ANALYZED));
-            foreach (PropertyInfo propertyInfo in classProperties)
+            foreach (var propertyInfo in classProperties)
             {
-                object propertyValue = propertyInfo.GetValue(this);
+                var propertyValue = propertyInfo.GetValue(this);
                 if (propertyValue != null)
                 {
-                    IEnumerable<LuceneIndexAttribute> attrs = propertyInfo.GetCustomAttributes<LuceneIndexAttribute>();
-                    foreach (LuceneIndexAttribute attr in attrs)
+                    var attrs = propertyInfo.GetCustomAttributes<LuceneIndexAttribute>();
+                    foreach (var attr in attrs)
                     {
                         string name = !string.IsNullOrEmpty(attr.Name) ? attr.Name : propertyInfo.Name;
                         string value = attr.IsHtml ? propertyValue.ToString().RemoveHtmlTag() : propertyValue.ToString();

+ 4 - 4
Masuit.LuceneEFCore.SearchEngine/LuceneIndexer.cs

@@ -47,7 +47,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <param name="recreate">是否需要覆盖</param>
         public void CreateIndex(IEnumerable<ILuceneIndexable> entities, bool recreate = true)
         {
-            IndexWriterConfig config = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, _analyzer);
+            var config = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, _analyzer);
 
             using (var writer = new IndexWriter(_directory, config))
             {
@@ -95,7 +95,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <param name="commit">是否提交</param>
         public void DeleteAll(bool commit = true)
         {
-            IndexWriterConfig config = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, _analyzer);
+            var config = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, _analyzer);
 
             using (var writer = new IndexWriter(_directory, config))
             {
@@ -130,7 +130,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <param name="change">实体</param>
         public void Update(LuceneIndexChange change)
         {
-            LuceneIndexChangeset changeset = new LuceneIndexChangeset(change);
+            var changeset = new LuceneIndexChangeset(change);
             Update(changeset);
         }
 
@@ -140,7 +140,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <param name="changeset">实体</param>
         public void Update(LuceneIndexChangeset changeset)
         {
-            IndexWriterConfig config = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, _analyzer);
+            var config = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, _analyzer);
             using (var writer = new IndexWriter(_directory, config))
             {
                 foreach (var change in changeset.Entries)

+ 4 - 34
Masuit.LuceneEFCore.SearchEngine/Masuit.LuceneEFCore.SearchEngine.xml

@@ -351,49 +351,19 @@
             <summary>
             与连接
             </summary>
-            <typeparam name="T"></typeparam>
+            <typeparam name="T">类型</typeparam>
             <param name="left">左条件</param>
             <param name="right">右条件</param>
-            <returns></returns>
+            <returns>新表达式</returns>
         </member>
         <member name="M:Masuit.LuceneEFCore.SearchEngine.Linq.LinqExtension.Or``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
             <summary>
             或连接
             </summary>
-            <typeparam name="T"></typeparam>
+            <typeparam name="T">类型</typeparam>
             <param name="left">左条件</param>
             <param name="right">右条件</param>
-            <returns></returns>
-        </member>
-        <member name="T:Masuit.LuceneEFCore.SearchEngine.Linq.ParameterReplacer">
-            <summary>
-            linq参数替换器
-            </summary>
-        </member>
-        <member name="M:Masuit.LuceneEFCore.SearchEngine.Linq.ParameterReplacer.#ctor(System.Linq.Expressions.ParameterExpression)">
-            <summary>
-            linq参数替换器
-            </summary>
-            <param name="paramExpr"></param>
-        </member>
-        <member name="P:Masuit.LuceneEFCore.SearchEngine.Linq.ParameterReplacer.ParameterExpression">
-            <summary>
-            参数表达式
-            </summary>
-        </member>
-        <member name="M:Masuit.LuceneEFCore.SearchEngine.Linq.ParameterReplacer.Replace(System.Linq.Expressions.Expression)">
-            <summary>
-            表达式替换
-            </summary>
-            <param name="expr"></param>
-            <returns></returns>
-        </member>
-        <member name="M:Masuit.LuceneEFCore.SearchEngine.Linq.ParameterReplacer.VisitParameter(System.Linq.Expressions.ParameterExpression)">
-            <summary>
-            表达式参数访问
-            </summary>
-            <param name="p"></param>
-            <returns></returns>
+            <returns>新表达式</returns>
         </member>
         <member name="T:Masuit.LuceneEFCore.SearchEngine.LuceneIndexableBaseEntity">
             <summary>

+ 63 - 88
Masuit.LuceneEFCore.SearchEngine/SearchEngine.cs

@@ -52,66 +52,42 @@ namespace Masuit.LuceneEFCore.SearchEngine
             Context = context;
             LuceneIndexer = new LuceneIndexer(directory, analyzer);
             LuceneIndexSearcher = new LuceneIndexSearcher(directory, analyzer, memoryCache);
-            //if (isInitialized == false || overrideIfExists)
-            //{
-            //    InitializeLucene(indexerOptions);
-            //    isInitialized = true;
-            //}
         }
 
-        ///// <summary>
-        ///// 初始化索引库
-        ///// </summary>
-        ///// <param name="options"></param>
-        //private void InitializeLucene(LuceneIndexerOptions options)
-        //{
-        //    if (_directory == null)
-        //    {
-        //        _directory = FSDirectory.Open(options.Path);
-        //    }
-
-        //    _analyzer = new JieBaAnalyzer(TokenizerMode.Search);
-        //    LuceneIndexer = new LuceneIndexer(_directory, _analyzer);
-        //    LuceneIndexSearcher = new LuceneIndexSearcher(_directory, _analyzer, _memoryCache);
-        //}
-
         /// <summary>
         /// 检查数据库上下文更改,并返回LuceneIndexChanges类型的集合
         /// </summary>
         /// <returns> LuceneIndexChangeset  - 转换为LuceneIndexChanges类型的实体更改集合</returns>
         private LuceneIndexChangeset GetChangeset()
         {
-            LuceneIndexChangeset changes = new LuceneIndexChangeset();
-
+            var changes = new LuceneIndexChangeset();
             foreach (var entity in Context.ChangeTracker.Entries().Where(x => x.State != EntityState.Unchanged))
             {
-                Type entityType = entity.Entity.GetType();
-                bool implementsILuceneIndexable = typeof(ILuceneIndexable).IsAssignableFrom(entityType);
-                if (implementsILuceneIndexable)
+                var entityType = entity.Entity.GetType();
+                if (!typeof(ILuceneIndexable).IsAssignableFrom(entityType) || entityType.GetMethod("ToDocument") is null)
                 {
-                    MethodInfo method = entityType.GetMethod("ToDocument");
-                    if (method != null)
-                    {
-                        LuceneIndexChange change = new LuceneIndexChange(entity.Entity as ILuceneIndexable);
-
-                        switch (entity.State)
-                        {
-                            case EntityState.Added:
-                                change.State = LuceneIndexState.Added;
-                                break;
-                            case EntityState.Deleted:
-                                change.State = LuceneIndexState.Removed;
-                                break;
-                            case EntityState.Modified:
-                                change.State = LuceneIndexState.Updated;
-                                break;
-                            default:
-                                change.State = LuceneIndexState.Unchanged;
-                                break;
-                        }
-                        changes.Entries.Add(change);
-                    }
+                    continue;
                 }
+
+                var change = new LuceneIndexChange(entity.Entity as ILuceneIndexable);
+
+                switch (entity.State)
+                {
+                    case EntityState.Added:
+                        change.State = LuceneIndexState.Added;
+                        break;
+                    case EntityState.Deleted:
+                        change.State = LuceneIndexState.Removed;
+                        break;
+                    case EntityState.Modified:
+                        change.State = LuceneIndexState.Updated;
+                        break;
+                    default:
+                        change.State = LuceneIndexState.Unchanged;
+                        break;
+                }
+
+                changes.Entries.Add(change);
             }
 
             return changes;
@@ -124,7 +100,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <returns></returns>
         private ILuceneIndexable GetConcreteFromDocument(Document doc)
         {
-            Type t = Type.GetType(doc.Get("Type"));
+            var t = Type.GetType(doc.Get("Type"));
             var obj = t.Assembly.CreateInstance(t.FullName, true) as ILuceneIndexable;
             foreach (var p in t.GetProperties().Where(p => p.GetCustomAttributes<LuceneIndexAttribute>().Any()))
             {
@@ -144,7 +120,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
             if (Context.ChangeTracker.HasChanges())
             {
                 // 获取要变更的实体集
-                LuceneIndexChangeset changes = GetChangeset();
+                var changes = GetChangeset();
                 result = Context.SaveChanges();
                 if (changes.HasChanges && index)
                 {
@@ -167,7 +143,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
             if (Context.ChangeTracker.HasChanges())
             {
                 // 获取要变更的结果集
-                LuceneIndexChangeset changes = GetChangeset();
+                var changes = GetChangeset();
                 result = await Context.SaveChangesAsync();
                 if (changes.HasChanges && index)
                 {
@@ -183,24 +159,26 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// </summary>
         public void CreateIndex()
         {
-            if (LuceneIndexer != null)
+            if (LuceneIndexer == null)
             {
-                List<ILuceneIndexable> index = new List<ILuceneIndexable>();
-                PropertyInfo[] properties = Context.GetType().GetProperties();
-                foreach (PropertyInfo pi in properties)
-                {
-                    if (typeof(IEnumerable<ILuceneIndexable>).IsAssignableFrom(pi.PropertyType))
-                    {
-                        var entities = Context.GetType().GetProperty(pi.Name).GetValue(Context, null);
-                        index.AddRange(entities as IEnumerable<ILuceneIndexable>);
-                    }
-                }
+                return;
+            }
 
-                if (index.Any())
+            var index = new List<ILuceneIndexable>();
+            var properties = Context.GetType().GetProperties();
+            foreach (var pi in properties)
+            {
+                if (typeof(IEnumerable<ILuceneIndexable>).IsAssignableFrom(pi.PropertyType))
                 {
-                    LuceneIndexer.CreateIndex(index);
+                    var entities = Context.GetType().GetProperty(pi.Name).GetValue(Context, null);
+                    index.AddRange(entities as IEnumerable<ILuceneIndexable>);
                 }
             }
+
+            if (index.Any())
+            {
+                LuceneIndexer.CreateIndex(index);
+            }
         }
 
         /// <summary>
@@ -208,24 +186,26 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// </summary>
         public void CreateIndex(List<string> tables)
         {
-            if (LuceneIndexer != null)
+            if (LuceneIndexer == null)
             {
-                List<ILuceneIndexable> index = new List<ILuceneIndexable>();
-                PropertyInfo[] properties = Context.GetType().GetProperties();
-                foreach (PropertyInfo pi in properties)
-                {
-                    if (typeof(IEnumerable<ILuceneIndexable>).IsAssignableFrom(pi.PropertyType) && tables.Contains(pi.Name))
-                    {
-                        var entities = Context.GetType().GetProperty(pi.Name).GetValue(Context, null);
-                        index.AddRange(entities as IEnumerable<ILuceneIndexable>);
-                    }
-                }
+                return;
+            }
 
-                if (index.Any())
+            var index = new List<ILuceneIndexable>();
+            var properties = Context.GetType().GetProperties();
+            foreach (var pi in properties)
+            {
+                if (typeof(IEnumerable<ILuceneIndexable>).IsAssignableFrom(pi.PropertyType) && tables.Contains(pi.Name))
                 {
-                    LuceneIndexer.CreateIndex(index);
+                    var entities = Context.GetType().GetProperty(pi.Name).GetValue(Context, null);
+                    index.AddRange(entities as IEnumerable<ILuceneIndexable>);
                 }
             }
+
+            if (index.Any())
+            {
+                LuceneIndexer.CreateIndex(index);
+            }
         }
 
         /// <summary>
@@ -246,22 +226,20 @@ namespace Masuit.LuceneEFCore.SearchEngine
         {
             options.Type = typeof(T);
             var indexResults = LuceneIndexSearcher.ScoredSearch(options);
-
-            ISearchResultCollection<T> resultSet = new SearchResultCollection<T>()
+            ISearchResultCollection<T> resultSet = new SearchResultCollection<T>
             {
                 TotalHits = indexResults.TotalHits
             };
 
-            Stopwatch sw = new Stopwatch();
-            sw.Start();
+            var sw = Stopwatch.StartNew();
             foreach (var indexResult in indexResults.Results)
             {
-                T entity = (T)GetConcreteFromDocument(indexResult.Document);
+                var entity = (T)GetConcreteFromDocument(indexResult.Document);
                 resultSet.Results.Add(entity);
             }
+
             sw.Stop();
             resultSet.Elapsed = indexResults.Elapsed + sw.ElapsedMilliseconds;
-
             return resultSet;
         }
 
@@ -280,12 +258,9 @@ namespace Masuit.LuceneEFCore.SearchEngine
             }
 
             var indexResults = LuceneIndexSearcher.ScoredSearch(options);
-
             IScoredSearchResultCollection<T> results = new ScoredSearchResultCollection<T>();
             results.TotalHits = indexResults.TotalHits;
-
-            Stopwatch sw = new Stopwatch();
-            sw.Start();
+            var sw = Stopwatch.StartNew();
             foreach (var indexResult in indexResults.Results)
             {
                 IScoredSearchResult<T> result = new ScoredSearchResult<T>();
@@ -293,9 +268,9 @@ namespace Masuit.LuceneEFCore.SearchEngine
                 result.Entity = (T)GetConcreteFromDocument(indexResult.Document);
                 results.Results.Add(result);
             }
+
             sw.Stop();
             results.Elapsed = indexResults.Elapsed + sw.ElapsedMilliseconds;
-
             return results;
         }