예제 #1
0
        /// <summary>
        /// 初始化索引库
        /// </summary>
        /// <param name="options"></param>
        private void InitializeLucene(LuceneIndexerOptions options)
        {
            if (_directory == null)
            {
                _directory = FSDirectory.Open(options.Path);
            }

            _analyzer = new JieBaAnalyzer(TokenizerMode.Search);
            _indexer  = new LuceneIndexer(_directory, _analyzer);
            _searcher = new LuceneIndexSearcher(_directory, _analyzer, _memoryCache);
        }
 /// <summary>
 /// 搜索引擎
 /// </summary>
 /// <param name="context">数据库上下文</param>
 /// <param name="directory"></param>
 /// <param name="analyzer"></param>
 /// <param name="memoryCache"></param>
 public SearchEngine(TContext context, Directory directory, Analyzer analyzer, IMemoryCache memoryCache)
 {
     Context             = context;
     LuceneIndexer       = new LuceneIndexer(directory, analyzer);
     LuceneIndexSearcher = new LuceneIndexSearcher(directory, analyzer, memoryCache);
     //if (isInitialized == false || overrideIfExists)
     //{
     //    InitializeLucene(indexerOptions);
     //    isInitialized = true;
     //}
 }
        /// <summary>
        /// 保存数据更改并同步索引
        /// </summary>
        /// <returns></returns>
        public int SaveChanges(bool index = true)
        {
            int result = 0;

            if (Context.ChangeTracker.HasChanges())
            {
                // 获取要变更的实体集
                LuceneIndexChangeset changes = GetChangeset();
                result = Context.SaveChanges();
                if (changes.HasChanges && index)
                {
                    LuceneIndexer.Update(changes);
                }
            }

            return(result);
        }
        /// <summary>
        /// 保存数据更改并同步索引
        /// </summary>
        /// <param name="index">是否需要被重新索引</param>
        /// <returns></returns>
        public async Task <int> SaveChangesAsync(bool index = true)
        {
            int result = 0;

            if (Context.ChangeTracker.HasChanges())
            {
                // 获取要变更的结果集
                LuceneIndexChangeset changes = GetChangeset();
                result = await Context.SaveChangesAsync();

                if (changes.HasChanges && index)
                {
                    LuceneIndexer.Update(changes);
                }
            }

            return(result);
        }
예제 #5
0
        /// <summary>
        /// 创建指定数据表的索引
        /// </summary>
        public void CreateIndex(List <string> tables)
        {
            if (LuceneIndexer == null)
            {
                return;
            }

            var properties = Context.GetType().GetProperties();

            foreach (var pi in properties)
            {
                if (typeof(IQueryable <ILuceneIndexable>).IsAssignableFrom(pi.PropertyType) && tables.Contains(pi.Name))
                {
                    var entities = Context.GetType().GetProperty(pi.Name).GetValue(Context, null) as IQueryable <ILuceneIndexable>;
                    LuceneIndexer.CreateIndex(entities, false);
                }
            }
        }
        /// <summary>
        /// 创建指定数据表的索引
        /// </summary>
        public void CreateIndex(List <string> tables)
        {
            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>);
                    }
                }

                if (index.Any())
                {
                    LuceneIndexer.CreateIndex(index);
                }
            }
        }
예제 #7
0
        /// <summary>
        /// 扫描数据库上下文并对所有已实现ILuceneIndexable的对象,并创建索引
        /// </summary>
        public void CreateIndex()
        {
            if (LuceneIndexer == null)
            {
                return;
            }

            var index      = new List <ILuceneIndexable>();
            var properties = Context.GetType().GetProperties();

            foreach (var 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>);
                }
            }

            if (index.Any())
            {
                LuceneIndexer.CreateIndex(index);
            }
        }
 /// <summary>
 /// 删除索引
 /// </summary>
 public void DeleteIndex()
 {
     LuceneIndexer?.DeleteAll();
 }
예제 #9
0
 /// <summary>
 /// 搜索引擎
 /// </summary>
 /// <param name="context">数据库上下文</param>
 /// <param name="directory"></param>
 /// <param name="analyzer"></param>
 /// <param name="memoryCache"></param>
 public SearchEngine(TContext context, Directory directory, Analyzer analyzer, IMemoryCache memoryCache)
 {
     Context             = context;
     LuceneIndexer       = new LuceneIndexer(directory, analyzer);
     LuceneIndexSearcher = new LuceneIndexSearcher(directory, analyzer, memoryCache);
 }