Ejemplo n.º 1
0
        /// <summary>
        /// 移除某个用户的索引
        /// </summary>
        /// <param name="userId"></param>
        public void RemoveIndex(List <int> userIds, string maxshop)
        {
            var isNew = false;
            var path  = _indexPath + "\\" + maxshop;

            // 从磁盘directory 中移除
            var directory = FSDirectory.Open(path);

            isNew = !IndexReader.IndexExists(directory);
            var maxFieldLength = new IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH);

            using (var writer = new IndexWriter(directory, new StandardAnalyzer(Version.LUCENE_30), isNew, maxFieldLength))
            {
                foreach (var userId in userIds)
                {
                    writer.DeleteDocuments(new Term("UserId", userId.ToString()));
                }
                writer.Optimize();
            }

            // 从内存directory中移除
            var ramDirectory = RamDirectoryFactory.GetRamDirectory(path, directory);

            isNew = !IndexReader.IndexExists(ramDirectory);
            using (var writer = new IndexWriter(ramDirectory, new StandardAnalyzer(Version.LUCENE_30), isNew, maxFieldLength))
            {
                foreach (var userId in userIds)
                {
                    writer.DeleteDocuments(new Term("UserId", userId.ToString()));
                }
                writer.Optimize();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 新增某个用户的索引
        /// </summary>
        /// <param name="user"></param>
        public void AddUserIndex(UserInfo user)
        {
            if (string.IsNullOrEmpty(user.MaxShopId))
            {
                throw new ArgumentNullException("max_shop");
            }

            var document = GetUserFromIndexDirectoryByUid(user.UserId, user.MaxShopId);

            if (document != null)
            {
                RemoveIndex(user.UserId, user.MaxShopId);
            }

            var isNew     = false;
            var path      = _indexPath + "\\" + user.MaxShopId;
            var directory = FSDirectory.Open(path);

            isNew = !IndexReader.IndexExists(directory);
            AddUserIndexToDirectory(user, directory, isNew);

            var ramDirectory = RamDirectoryFactory.GetRamDirectory(path, directory);

            isNew = !IndexReader.IndexExists(ramDirectory);
            AddUserIndexToDirectory(user, ramDirectory, isNew);
        }
Ejemplo n.º 3
0
        private Document GetUserFromIndexDirectoryByUid(string accId, string maxShopId)
        {
            // 搜索用户是否存在
            var           indexPath    = _indexPath + "\\" + maxShopId;
            FSDirectory   directory    = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
            var           ramDirectory = RamDirectoryFactory.GetRamDirectory(indexPath, directory);
            IndexReader   reader       = IndexReader.Open(ramDirectory, true);
            IndexSearcher searcher     = new IndexSearcher(reader);

            var idTerm  = new Term("UserId", accId);
            var idQuery = new TermQuery(idTerm);

            var result   = searcher.Search(idQuery, 1);
            var userInfo = new UserInfo()
            {
                ShopId = accId, MaxShopId = maxShopId
            };

            if (result.TotalHits == 0)
            {
                return(null);
            }

            var document = searcher.Doc(result.ScoreDocs[0].Doc);

            return(document);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 移除某个用户的索引
        /// </summary>
        /// <param name="userId"></param>
        public void RemoveIndex(string userId, string maxshop)
        {
            var isNew = false;
            var path  = _indexPath + "\\" + maxshop;

            // 从磁盘directory 中移除
            var directory = FSDirectory.Open(path);

            isNew = !IndexReader.IndexExists(directory);
            RemoveIndexFromDirectory(directory, userId, isNew);

            // 从内存directory中移除
            var ramDirectory = RamDirectoryFactory.GetRamDirectory(path, directory);

            isNew = !IndexReader.IndexExists(ramDirectory);
            RemoveIndexFromDirectory(ramDirectory, userId, isNew);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 搜索用户
        /// </summary>
        /// <param name="shopIds">店铺Id列表</param>
        /// <param name="term">关键字</param>
        /// <returns></returns>
        public List <int> Search(int maxShop, string term)
        {
            var         indexPath = _indexPath + "\\" + maxShop;
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());

            var           ramDirectory = RamDirectoryFactory.GetRamDirectory(indexPath, directory);
            IndexReader   reader       = IndexReader.Open(ramDirectory, true);
            IndexSearcher searcher     = new IndexSearcher(reader);

            var nameParse = new QueryParser(Version.LUCENE_30, "Name", new StandardAnalyzer(Version.LUCENE_30));
            var nameQuery = nameParse.Parse(term);

            var phoneTerm  = new Term("Phone", string.Format("*{0}*", term));
            var phoneQuery = new WildcardQuery(phoneTerm);

            var numberTerm  = new Term("Number", string.Format("*{0}*", term));
            var numberQuery = new WildcardQuery(numberTerm);

            var uPinYinShortTerm  = new Term("UPinYinShort", string.Format("*{0}*", term.ToUpper()));
            var uPinYinShortQuery = new WildcardQuery(uPinYinShortTerm);

            var uPinYinFullTerm  = new Term("UPinYinFull", string.Format("*{0}*", term.ToUpper()));
            var uPinYinFullQuery = new WildcardQuery(uPinYinFullTerm);

            var query = new BooleanQuery();

            query.Add(uPinYinShortQuery, Occur.SHOULD);
            query.Add(uPinYinFullQuery, Occur.SHOULD);
            query.Add(phoneQuery, Occur.SHOULD);
            query.Add(numberQuery, Occur.SHOULD);
            query.Add(nameQuery, Occur.SHOULD);

            var docs = searcher.Search(query, 20);

            var result = new List <int>();

            foreach (var doc in docs.ScoreDocs)
            {
                var document = searcher.Doc(doc.Doc);
                result.Add(Convert.ToInt32(document.Get("UserId")));
            }

            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 新增某个用户的索引
        /// </summary>
        /// <param name="user"></param>
        public void AddUserIndex(List <UserInfo> users, int maxshopId)
        {
            var isNew          = false;
            var path           = _indexPath + "\\" + maxshopId;
            var directory      = FSDirectory.Open(path);
            var maxFieldLength = new IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH);

            isNew = !IndexReader.IndexExists(directory);

            // 添加到文件索引
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);

            using (var writer = new IndexWriter(directory, analyzer, isNew, maxFieldLength))
            {
                foreach (var user in users)
                {
                    var document = new Document();

                    document.Add(new Field("UserId", user.UserId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("ShopId", user.ShopId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("UPinYinShort", user.UPinYinShort.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("UPinYinFull", user.UPinYinFull.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("Name", user.Name.ToString(), Field.Store.YES, Field.Index.ANALYZED));

                    if (!string.IsNullOrWhiteSpace(user.Phone))
                    {
                        document.Add(new Field("Phone", user.Phone.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    }
                    if (!string.IsNullOrWhiteSpace(user.Number))
                    {
                        document.Add(new Field("Number", user.Number.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    }


                    writer.AddDocument(document);
                }

                writer.Optimize();
            }

            // 添加到内存索引
            var ramDirectory = RamDirectoryFactory.GetRamDirectory(path, directory);

            using (var writer = new IndexWriter(ramDirectory, analyzer, isNew, maxFieldLength))
            {
                foreach (var user in users)
                {
                    var document = new Document();

                    document.Add(new Field("UserId", user.UserId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("ShopId", user.ShopId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("UPinYinShort", user.UPinYinShort.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("UPinYinFull", user.UPinYinFull.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("Name", user.Name.ToString(), Field.Store.YES, Field.Index.ANALYZED));

                    if (!string.IsNullOrWhiteSpace(user.Phone))
                    {
                        document.Add(new Field("Phone", user.Phone.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    }
                    if (!string.IsNullOrWhiteSpace(user.Number))
                    {
                        document.Add(new Field("Number", user.Number.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    }

                    writer.AddDocument(document);
                }

                writer.Optimize();
            }
        }