Пример #1
0
 /// <summary>
 /// Subclass can override to choose a specific
 /// <see cref="Directory"/> implementation.
 /// </summary>
 protected internal virtual Directory GetDirectory(DirectoryInfo path)
 {
     return(FSDirectory.Open(path));
 }
        private void InitializeLocalIndexAndDirectory(Lucene.Net.Store.Directory baseLuceneDirectory, Analyzer analyzer, string configuredPath)
        {
            lock (Locker)
            {
                if (!Directory.Exists(_tempPath))
                {
                    Directory.CreateDirectory(_tempPath);
                }
                else
                {
                    //if we are syncing storage to the main file system to temp files, then clear out whatever is
                    //currently there since we'll re-copy it over
                    if (_syncStorage)
                    {
                        //clear it!
                        Directory.Delete(_tempPath, true);
                        //recreate it
                        Directory.CreateDirectory(_tempPath);
                    }
                }


                //if we are syncing storage to the main file system to temp files, then sync from the main FS to our temp FS
                if (_syncStorage)
                {
                    //copy index

                    using (new IndexWriter(
                               //read from the underlying/default directory, not the temp codegen dir
                               baseLuceneDirectory,
                               analyzer,
                               Snapshotter,
                               IndexWriter.MaxFieldLength.UNLIMITED))
                    {
                        try
                        {
                            var basePath = IOHelper.MapPath(configuredPath);

                            var commit    = Snapshotter.Snapshot();
                            var fileNames = commit.GetFileNames();

                            foreach (var fileName in fileNames)
                            {
                                File.Copy(
                                    Path.Combine(basePath, "Index", fileName),
                                    Path.Combine(_tempPath, Path.GetFileName(fileName)), true);
                            }

                            var segments = commit.GetSegmentsFileName();
                            if (segments.IsNullOrWhiteSpace() == false)
                            {
                                File.Copy(
                                    Path.Combine(basePath, "Index", segments),
                                    Path.Combine(_tempPath, Path.GetFileName(segments)), true);
                            }
                        }
                        finally
                        {
                            Snapshotter.Release();
                        }
                    }

                    //create the custom lucene directory which will keep the main and temp FS's in sync

                    LuceneDirectory = new TempStorageDirectory(
                        new DirectoryInfo(_tempPath),
                        baseLuceneDirectory);
                }
                else
                {
                    //just return a normal lucene directory that uses the codegen folder

                    LuceneDirectory = FSDirectory.Open(new DirectoryInfo(_tempPath));
                }
            }
        }
        public void Integration()
        {
            var builder = new ContainerBuilder();

            builder.RegisterModule(new RootModule(@"Files\Shorthand.xml"));
            var container = builder.Build();


            // CORRECT DATA AND INITIAL LOAD
            using (var cn = new SqlServerConnectionFactory(InputConnection).GetConnection()) {
                cn.Open();
                Assert.AreEqual(2, cn.Execute(@"
                    UPDATE [Order Details] SET UnitPrice = 14.40, Quantity = 42 WHERE OrderId = 10253 AND ProductId = 39;
                    UPDATE Orders SET CustomerID = 'CHOPS', Freight = 22.98 WHERE OrderId = 10254;
                "));
            }

            var root     = ResolveRoot(container, TestFile, true);
            var response = new PipelineAction(root).Execute();

            Assert.AreEqual(200, response.Code);
            Assert.AreEqual(string.Empty, response.Content);

            using (var reader = IndexReader.Open(FSDirectory.Open(new DirectoryInfo(Path.Combine(OutputConnection.Folder, "Order Details"))), true)) {
                Assert.AreEqual(2155, reader.NumDocs());
            }

            // FIRST DELTA, NO CHANGES
            root     = ResolveRoot(container, TestFile, false);
            response = new PipelineAction(root).Execute();

            Assert.AreEqual(200, response.Code);
            Assert.AreEqual(string.Empty, response.Content);

            using (var reader = IndexReader.Open(FSDirectory.Open(new DirectoryInfo(Path.Combine(OutputConnection.Folder, "Order Details"))), true)) {
                Assert.AreEqual(2155, reader.NumDocs());
            }

            // CHANGE 2 FIELDS IN 1 RECORD IN MASTER TABLE THAT WILL CAUSE CALCULATED FIELD TO BE UPDATED TOO
            using (var cn = new SqlServerConnectionFactory(InputConnection).GetConnection()) {
                cn.Open();
                const string sql = @"UPDATE [Order Details] SET UnitPrice = 15, Quantity = 40 WHERE OrderId = 10253 AND ProductId = 39;";
                Assert.AreEqual(1, cn.Execute(sql));
            }

            root     = ResolveRoot(container, TestFile, false);
            response = new PipelineAction(root).Execute();

            Assert.AreEqual(200, response.Code);
            Assert.AreEqual(string.Empty, response.Content);

            using (var searcher = new IndexSearcher(FSDirectory.Open(new DirectoryInfo(Path.Combine(OutputConnection.Folder, "Order Details"))), true)) {
                var hits = searcher.Search(new TermQuery(new Term("TflId", "1025339")), null, 1);
                Assert.AreEqual(1, hits.TotalHits);
                var hit = searcher.Doc(hits.ScoreDocs[0].Doc);
                Assert.AreEqual(15.0d, Convert.ToDecimal(hit.Get("OrderDetailsUnitPrice")));
                Assert.AreEqual(40, Convert.ToInt32(hit.Get("OrderDetailsQuantity")));
                Assert.AreEqual(40 * 15.0d, Convert.ToDecimal(hit.Get("OrderDetailsExtendedPrice")));
            }

            // CHANGE 1 RECORD'S CUSTOMERID AND FREIGHT ON ORDERS TABLE
            using (var cn = new SqlServerConnectionFactory(InputConnection).GetConnection()) {
                cn.Open();
                Assert.AreEqual(1, cn.Execute("UPDATE Orders SET CustomerID = 'VICTE', Freight = 20.11 WHERE OrderId = 10254;"));
            }

            root     = ResolveRoot(container, TestFile, false);
            response = new PipelineAction(root).Execute();

            Assert.AreEqual(200, response.Code);
            Assert.AreEqual(string.Empty, response.Content);

            using (var searcher = new IndexSearcher(FSDirectory.Open(new DirectoryInfo(Path.Combine(OutputConnection.Folder, "Orders"))), true)) {
                var hits = searcher.Search(new TermQuery(new Term("OrdersOrderID", NumericUtils.IntToPrefixCoded(10254))), 1);
                Assert.AreEqual(1, hits.TotalHits);
                var hit = searcher.Doc(hits.ScoreDocs[0].Doc);
                Assert.AreEqual("VICTE", hit.Get("OrdersCustomerID"));
                Assert.AreEqual(20.11d, Convert.ToDecimal(hit.Get("OrdersFreight")));
            }
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        public void IndexCreate()
        {
            analyzer = new PanGuAnalyzer();

            IndexWriter writer = new IndexWriter(FSDirectory.Open(new DirectoryInfo(indexDirectory)), analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
        }
Пример #5
0
        /// <summary>
        ///     Searches the datasource using the specified criteria. Criteria is parsed by the query builder specified by
        ///     <typeparamref />
        ///     .
        /// </summary>
        /// <param name="scope">Name of the application.</param>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        /// <exception cref="VirtoCommerce.SearchModule.Data.Providers.Lucene.LuceneSearchException"></exception>
        public virtual ISearchResults Search(string scope, ISearchCriteria criteria)
        {
            ISearchResults result;

            var directoryInfo = new DirectoryInfo(GetDirectoryPath(GetFolderName(scope, criteria.DocumentType)));

            if (directoryInfo.Exists)
            {
                var dir      = FSDirectory.Open(directoryInfo);
                var searcher = new IndexSearcher(dir);

                var q = (QueryBuilder)QueryBuilder.BuildQuery(criteria);

                // filter out empty value
                var filter = q.Filter.ToString().Equals("BooleanFilter()") ? null : q.Filter;

                Debug.WriteLine("Search Lucene Query:{0}", q.ToString());

                TopDocs docs;

                try
                {
                    var numDocs = criteria.StartingRecord + criteria.RecordsToRetrieve;

                    // numDocs must be > 0
                    if (numDocs < 1)
                    {
                        numDocs = 1;
                    }

                    if (criteria.Sort != null)
                    {
                        var fields = criteria.Sort.GetSort();

                        docs = searcher.Search(q.Query, filter, numDocs,
                                               new Sort(fields.Select(field => new SortField(field.FieldName, field.DataType, field.IsDescending)).ToArray()));
                    }
                    else
                    {
                        docs = searcher.Search(q.Query, filter, numDocs);
                    }
                }
                catch (Exception ex)
                {
                    throw new LuceneSearchException("Search exception", ex);
                }

                var results = new LuceneSearchResults(searcher, searcher.IndexReader, docs, criteria, q.Query);

                // Cleanup here
                searcher.IndexReader.Dispose();
                searcher.Dispose();
                result = results.Results;
            }
            else
            {
                result = new SearchResults(criteria, null);
            }

            return(result);
        }
Пример #6
0
        public static void Show()
        {
            FSDirectory   dir      = FSDirectory.Open(StaticConstant.TestIndexPath);
            IndexSearcher searcher = new IndexSearcher(dir);               //查找器
            {
                TermQuery query = new TermQuery(new Term("title", "图书馆")); //包含
                TopDocs   docs  = searcher.Search(query, null, 10000);     //找到的数据
                foreach (ScoreDoc sd in docs.ScoreDocs)
                {
                    Document doc = searcher.Doc(sd.Doc);
                    Console.WriteLine("***************************************");
                    Console.WriteLine(string.Format("id={0}", doc.Get("id")));
                    Console.WriteLine(string.Format("title={0}", doc.Get("title")));
                    Console.WriteLine(string.Format("time={0}", doc.Get("time")));
                    Console.WriteLine(string.Format("price={0}", doc.Get("price")));
                    Console.WriteLine(string.Format("content={0}", doc.Get("content")));
                }
                Console.WriteLine("1一共命中了{0}个", docs.TotalHits);
            }

            QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "title", new PanGuAnalyzer());//解析器
            {
                //string keyword = "高中政治人教新课标选修生活中的法律常识";
                string keyword = "高中政治 人 教 新课 标 选修 生活 中的 法律常识";
                {
                    Query   query = parser.Parse(keyword);
                    TopDocs docs  = searcher.Search(query, null, 10000);//找到的数据

                    int i = 0;
                    foreach (ScoreDoc sd in docs.ScoreDocs)
                    {
                        if (i++ < 1000)
                        {
                            Document doc = searcher.Doc(sd.Doc);
                            Console.WriteLine("***************************************");
                            Console.WriteLine(string.Format("id={0}", doc.Get("id")));
                            Console.WriteLine(string.Format("title={0}", doc.Get("title")));
                            Console.WriteLine(string.Format("time={0}", doc.Get("time")));
                            Console.WriteLine(string.Format("price={0}", doc.Get("price")));
                        }
                    }
                    Console.WriteLine($"一共命中{docs.TotalHits}");
                }
                {
                    Query query = parser.Parse(keyword);
                    NumericRangeFilter <int> timeFilter = NumericRangeFilter.NewIntRange("time", 20190101, 20191231, true, true); //过滤
                    SortField sortPrice = new SortField("price", SortField.DOUBLE, false);                                        //降序
                    SortField sortTime  = new SortField("time", SortField.INT, true);                                             //升序
                    Sort      sort      = new Sort(sortTime, sortPrice);                                                          //排序 哪个前哪个后

                    TopDocs docs = searcher.Search(query, timeFilter, 10000, sort);                                               //找到的数据
                    int     i    = 0;
                    foreach (ScoreDoc sd in docs.ScoreDocs)
                    {
                        if (i++ < 1000)
                        {
                            Document doc = searcher.Doc(sd.Doc);
                            Console.WriteLine("***************************************");
                            Console.WriteLine(string.Format("id={0}", doc.Get("id")));
                            Console.WriteLine(string.Format("title={0}", doc.Get("title")));
                            Console.WriteLine(string.Format("time={0}", doc.Get("time")));
                            Console.WriteLine(string.Format("price={0}", doc.Get("price")));
                        }
                    }
                    Console.WriteLine("3一共命中了{0}个", docs.TotalHits);
                }
            }
        }
Пример #7
0
        /// <summary>
        ///根据关键字进行查询,根据int的范围进行搜索
        /// </summary>
        /// <param name="Title"></param>
        /// <returns></returns>
        public List <Bpo_JobEntity> SearchJobList4(string Title, string FullAddress, int MinId, int MaxId)
        {
            List <Bpo_JobEntity> jobList = new List <Bpo_JobEntity>();
            string      path             = "F://LuceneIndexDir";
            FSDirectory dir = FSDirectory.Open(path);

            //IndexSearcher searcher = new IndexSearcher(dir);//声明一个查询器,或者以下面一种方式声明
            IndexReader   reader   = IndexReader.Open(dir, true);//查询器
            IndexSearcher searcher = new IndexSearcher(reader);

            //搜索条件,BooleanQuery可以同时制定多个搜索条件
            BooleanQuery booleanQuery = new BooleanQuery();

            //Query query = new TermQuery(new Term("Title", $"*{Title}*"));//不支持通配符
            //Query query = new WildcardQuery(new Term("Title", $"*{Title}*")); // 通配符

            if (!string.IsNullOrEmpty(Title))//空格隔开,按照多个词进行搜索
            {
                Title = new LuceneAnalyze().AnalyzerKeyword("Title", Title);
                QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", new PanGuAnalyzer());
                Query       query  = parser.Parse(Title);
                booleanQuery.Add(query, Occur.MUST);
            }
            if (MinId > 0)//空格隔开,按照多个词进行搜索
            {
                //string idStr = new LuceneAnalyze().AnalyzerKeyword("Id", MinId.ToString());
                QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Id", new PanGuAnalyzer());
                Query       query  = parser.Parse(MinId.ToString());
                booleanQuery.Add(query, Occur.MUST);
            }

            if (!string.IsNullOrEmpty(FullAddress))
            {
                FullAddress = new LuceneAnalyze().AnalyzerKeyword("FullAddress", FullAddress);
                QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "FullAddress", new PanGuAnalyzer());
                Query       query  = parser.Parse(FullAddress);
                booleanQuery.Add(query, Occur.MUST);

                //使用WildcardQuery,相当于sql的like
                //Query query2 = new WildcardQuery(new Term("FullAddress", $"*{FullAddress}*")); // 通配符
                //booleanQuery.Add(query2, Occur.MUST);
            }

            //根据id区间区间搜索(此时id存储索引时要是NumericField类型)
            NumericRangeFilter <int> intFilter = null;
            //if (MinId > 0 && MaxId == 0)
            //{
            //    intFilter = NumericRangeFilter.NewIntRange("Id", MinId, int.MaxValue, true, true);
            //}
            //else if (MaxId > 0 && MinId == 0)
            //{
            //    intFilter = NumericRangeFilter.NewIntRange("Id", 0, MaxId, true, true);
            //}
            //else if (MaxId > 0 && MinId > 0)
            //{
            //    intFilter = NumericRangeFilter.NewIntRange("Id", MinId, MaxId, true, true);
            //}

            //定义排序
            SortField sortField  = new SortField("Id", SortField.STRING, false);     //降序
            SortField sortField2 = new SortField("CompanyId", SortField.INT, false); //降序
            Sort      sort       = new Sort(sortField, sortField2);

            //取搜索结果方法1:
            TopDocs docs = searcher.Search(booleanQuery, intFilter, 10000, sort); //找到的结果取100条数据

            foreach (ScoreDoc sd in docs.ScoreDocs)                               //从docs.ScoreDocs取数据
            {
                Document doc = searcher.Doc(sd.Doc);
                jobList.Add(new Bpo_JobEntity
                {
                    Id          = Convert.ToInt32(doc.Get("Id") ?? "-100"),
                    Title       = doc.Get("Title"),
                    UserId      = Convert.ToInt32(doc.Get("UserId") ?? "-100"),
                    UserName    = doc.Get("UserName"),
                    CompanyId   = Convert.ToInt32(doc.Get("CompanyId") ?? "-100"),
                    CompanyName = doc.Get("CompanyName"),
                    FullAddress = doc.Get("FullAddress"),
                    CreateDate  = Convert.ToDateTime(doc.Get("CreateDate"))
                });
            }
            return(jobList);
        }
Пример #8
0
 public NivelDocumentalComProdutoresSearcher(string orderBy, string defaultField)
     : base(FSDirectory.Open(Util.NivelDocumentalComProdutoresPath), orderBy, defaultField, new SynonymAnalyzer(new XmlSynonymEngine()))
 {
 }
Пример #9
0
        private bool Initialize()
        {
            if (initialized)
            {
                return(true);
            }

            if (failedToInitialize)
            {
                return(false);
            }

            var fileSystemPath = cmsConfiguration.Search.GetValue(LuceneSearchConstants.ConfigurationKeys.LuceneFileSystemDirectory);

            if (string.IsNullOrWhiteSpace(fileSystemPath))
            {
                Log.Error("Cannot Initialize Lucene indexer. Lucene file system path is not set.");
                failedToInitialize = true;

                return(false);
            }
            directory = GetLuceneDirectory(fileSystemPath);
            index     = FSDirectory.Open(directory);

            try
            {
                configurationExcludedNodeTypes = GetCollectionFromConfiguration(LuceneSearchConstants.ConfigurationKeys.LuceneExcludedNodes);
                configurationExcludedIds       = GetCollectionFromConfiguration(LuceneSearchConstants.ConfigurationKeys.LuceneExcludedIds);
                configurationExcludedClasses   = GetCollectionFromConfiguration(LuceneSearchConstants.ConfigurationKeys.LuceneExcludedClasses);

                bool.TryParse(cmsConfiguration.Search.GetValue(LuceneSearchConstants.ConfigurationKeys.LuceneSearchForPartOfWordsPrefix), out searchForPartOfWords);

                bool disableStopWords;
                if (!bool.TryParse(cmsConfiguration.Search.GetValue(LuceneSearchConstants.ConfigurationKeys.LuceneDisableStopWords), out disableStopWords))
                {
                    disableStopWords = false;
                }
                if (disableStopWords)
                {
                    analyzer = new StandardAnalyzer(Version.LUCENE_30, new HashSet <string>());
                }
                else
                {
                    analyzer = new StandardAnalyzer(Version.LUCENE_30);
                }

                if (searchForPartOfWords)
                {
                    parser = new PartialWordTermQueryParser(Version.LUCENE_30, "content", analyzer);
                }
                else
                {
                    parser = new QueryParser(Version.LUCENE_30, "content", analyzer);
                }
                parser.AllowLeadingWildcard = true;

                if (!IndexReader.IndexExists(index))
                {
                    OpenWriter(true);
                    CloseWriter();
                }
            }
            catch (Exception exc)
            {
                Log.Error("Failed to initialize Lucene search engine.", exc);
                failedToInitialize = true;

                return(false);
            }

            initialized = true;
            return(true);
        }
Пример #10
0
 public LuceneEngine()
 {
     _Analyzer  = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
     _Directory = FSDirectory.Open(_IndexPath);
 }
Пример #11
0
        public static bool MakeIndex(List <ArticleIndex> articleList)
        {
            bool retult   = false;
            var  indexDir = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "lucene");

            if (System.IO.Directory.Exists(indexDir) == false)
            {
                System.IO.Directory.CreateDirectory(indexDir);
            }

            var VERSION           = Lucene.Net.Util.LuceneVersion.LUCENE_48;
            var director          = FSDirectory.Open(new DirectoryInfo(indexDir));
            var analyzer          = new JieBaAnalyzer(TokenizerMode.Search);
            var indexWriterConfig = new IndexWriterConfig(VERSION, analyzer);
            var indexWriter       = new IndexWriter(director, indexWriterConfig);

            try
            {
                //Console.WriteLine($"[{DateTime.Now}] UpdateMerchIndex job begin...");

                if (File.Exists(Path.Combine(indexDir, "segments.gen")) == true)
                {
                    indexWriter.DeleteAll();
                }
                var index = 1;
                var size  = 200;

                var count = articleList.Count();

                if (count > 0)
                {
                    while (true)
                    {
                        var rs = articleList.OrderBy(t => t.CreateTime)
                                 .Skip((index - 1) * size)
                                 .Take(size).ToList();

                        if (rs.Count == 0)
                        {
                            break;
                        }

                        var docList = new List <Document>();

                        foreach (var item in rs)
                        {
                            var articleId = item.Id.ToString();

                            var doc    = new Document();
                            var field1 = new StringField("articleId", articleId, Field.Store.YES);
                            var field2 = new TextField("title", item.Title?.ToLower(), Field.Store.YES);
                            var field3 = new TextField("summary", item.Summary?.ToLower(), Field.Store.YES);
                            doc.Add(field1);
                            doc.Add(field2);
                            doc.Add(field3);
                            docList.Add(doc);
                        }

                        if (docList.Count > 0)
                        {
                            indexWriter.AddDocuments(docList);
                        }

                        index = index + 1;
                    }
                }

                if (!indexWriter.IsClosed)
                {
                    indexWriter.Dispose();
                }
                retult = true;
                //Console.WriteLine($"[{DateTime.Now}] UpdateMerchIndex job end!");
            }
            catch (Exception ex)
            {
                if (!indexWriter.IsClosed)
                {
                    indexWriter.Dispose();
                }
                LogHelper.WriteLog_LocalTxt(ex.ToString());
            }
            return(retult);
        }
Пример #12
0
        public static List <int> Search(string key, int page, int pageSize, out int totalHits)
        {
            totalHits = 0;
            if (string.IsNullOrEmpty(key))
            {
                return(null);
            }
            key = key.Trim().ToLower();

            var rs = new List <int>();

            try
            {
                var indexDir = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "lucene");

                //var VERSION = Lucene.Net.Util.LuceneVersion.LUCENE_48;

                if (System.IO.Directory.Exists(indexDir) == true)
                {
                    var reader = DirectoryReader.Open(FSDirectory.Open(new DirectoryInfo(indexDir)));
                    var search = new IndexSearcher(reader);

                    var directory = FSDirectory.Open(new DirectoryInfo(indexDir), NoLockFactory.GetNoLockFactory());
                    var reader2   = DirectoryReader.Open(directory);
                    var search2   = new IndexSearcher(reader2);

                    //var parser = new QueryParser(VERSION, "title", new JieBaAnalyzer(TokenizerMode.Search));
                    var booleanQuery = new BooleanQuery();

                    var list = CutKeyWord(key);
                    foreach (var word in list)
                    {
                        var query1 = new TermQuery(new Term("title", word));
                        var query2 = new TermQuery(new Term("summary", word));
                        booleanQuery.Add(query1, Occur.SHOULD);
                        booleanQuery.Add(query2, Occur.SHOULD);
                    }

                    var collector = TopScoreDocCollector.Create(1000, true);
                    search2.Search(booleanQuery, null, collector);
                    var docs = collector.GetTopDocs((page - 1) * pageSize, (page) * pageSize).ScoreDocs;
                    totalHits = collector.TotalHits;
                    foreach (var d in docs)
                    {
                        var num      = d.Doc;
                        var document = search.Doc(num);// 拿到指定的文档

                        var articleId = document.Get("articleId");
                        //var name = document.Get("title");

                        if (int.TryParse(articleId, out int mid) == true)
                        {
                            rs.Add(mid);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"SearchMerchs ex={ex}");
            }

            return(rs);
        }
        /// <summary>
        /// 提交内容
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="date"></param>
        /// <param name="param"></param>
        /// <param name="modcode"></param>
        /// <returns></returns>
        public static string CommitContent(string id, string title, string content, DateTime?date, string param, string modcode, string searchparam1, string searchparam2, string searchparam3)
        {
            #region 验证输入参数是否合格
            if (string.IsNullOrEmpty(id))
            {
                throw new Exception("参数id不能为空");
            }
            if (string.IsNullOrEmpty(title))
            {
                throw new Exception("参数title不能为空");
            }
            if (string.IsNullOrEmpty(content))
            {
                throw new Exception("参数content和filepathList不能同时为空");
            }
            #endregion
            if (date == null)
            {
                date = DateTime.Now;              //日期为null时使用当前时间
            }
            //Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
            Analyzer    analyzer  = new PanGuAnalyzer();
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
            bool        isUpdate  = IndexReader.IndexExists(directory);
            if (isUpdate)
            {
                //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁
                if (IndexWriter.IsLocked(directory))
                {
                    IndexWriter.Unlock(directory);
                }
            }
            IndexWriter writer = new IndexWriter(directory, analyzer, !isUpdate, IndexWriter.MaxFieldLength.UNLIMITED);
            //为避免重复索引,所以先删除id=id的记录,再重新添加
            writer.DeleteDocuments(new Term("id", id));

            string result = "";
            try
            {
                Document doc = new Document();
                doc.Add(new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED));                                                //存储
                doc.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));     //分词建立索引
                doc.Add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); //分词建立索引
                doc.Add(new Field("date", date.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));                                 //不分词建立索引
                doc.Add(new Field("param", param, Field.Store.YES, Field.Index.NO));                                                    //存储
                doc.Add(new Field("modcode", modcode, Field.Store.YES, Field.Index.NOT_ANALYZED));                                      //存储

                doc.Add(new Field("searchparam1", searchparam1, Field.Store.YES, Field.Index.NOT_ANALYZED));                            //存储
                doc.Add(new Field("searchparam2", searchparam2, Field.Store.YES, Field.Index.NOT_ANALYZED));                            //存储
                doc.Add(new Field("searchparam3", searchparam3, Field.Store.YES, Field.Index.NOT_ANALYZED));                            //存储

                writer.AddDocument(doc);

                //输出api调用记录
                string EnableApiLog = Util.GetAppSetting("EnableApiLog");
                if (EnableApiLog == "1")
                {
                    string logMsg = GetParamList(id, title, content, date, param, modcode) + "\r\n" + Utility.Util.GetClientInfo();
                    Utility.Util.WriteApiLog(logMsg);
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
                string errMsg = ex.Message + "\r\n" + GetParamList(id, title, content, date, param, modcode) + "\r\n" + Utility.Util.GetClientInfo();
                Utility.Util.WriteLog(errMsg);
            }

            //对索引文件进行优化
            //writer.Optimize();
            analyzer.Close();
            writer.Dispose();
            directory.Dispose();
            return(result);
        }
        /// <summary>
        /// 搜索内容
        /// </summary>
        /// <param name="word">搜索关键字</param>
        /// <param name="pagesize">每页显示记录数</param>
        /// <param name="pageindex">当前页码</param>
        /// <returns></returns>
        public static SearchResult SearchContent(string modcode, string word, int pagesize, int pageindex, string searchparam1, string searchparam2, string searchparam3)
        {
            SearchResult searchResult = new SearchResult();

            FSDirectory   directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
            IndexSearcher searcher  = new IndexSearcher(directory, true);
            var           analyzer  = new PanGuAnalyzer();

            //初始化MultiFieldQueryParser以便同时查询多列
            Lucene.Net.QueryParsers.MultiFieldQueryParser parser = new Lucene.Net.QueryParsers.MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new string[] { "title", "content" }, analyzer);
            Lucene.Net.Search.Query query = parser.Parse(word);//初始化Query
            parser.DefaultOperator = QueryParser.AND_OPERATOR;

            Lucene.Net.Search.BooleanQuery boolQuery = new Lucene.Net.Search.BooleanQuery();
            boolQuery.Add(query, Occur.MUST);
            if (!string.IsNullOrEmpty(modcode))
            {
                PhraseQuery queryModCode = new PhraseQuery();
                queryModCode.Add(new Term("modcode", modcode));
                boolQuery.Add(queryModCode, Occur.MUST);
            }

            if (!string.IsNullOrEmpty(searchparam1))
            {
                WildcardQuery query1 = new WildcardQuery(new Term("searchparam1", "*" + searchparam1 + "*"));
                boolQuery.Add(query1, Occur.MUST);
            }
            if (!string.IsNullOrEmpty(searchparam2))
            {
                WildcardQuery query1 = new WildcardQuery(new Term("searchparam2", "*" + searchparam2 + "*"));
                boolQuery.Add(query1, Occur.MUST);
            }
            if (!string.IsNullOrEmpty(searchparam3))
            {
                WildcardQuery query1 = new WildcardQuery(new Term("searchparam3", "*" + searchparam3 + "*"));
                boolQuery.Add(query1, Occur.MUST);
            }

            Sort sort   = new Sort(new SortField("date", SortField.STRING, true));
            var  result = searcher.Search(boolQuery, null, 1000, sort);

            if (result.TotalHits == 0)
            {
                searchResult.count = 0;
            }
            else
            {
                searchResult.count = result.TotalHits;
                int startNum = 0, endNum = result.TotalHits;
                if (pagesize > 0)
                {
                    //当pagesize>0时使用分页功能
                    startNum = (pageindex - 1) * pagesize;
                    endNum   = startNum + pagesize;
                }
                ScoreDoc[]     docs     = result.ScoreDocs;
                List <JObject> dataList = new List <JObject>();
                for (int i = 0; i < docs.Length; i++)
                {
                    if (i < startNum)
                    {
                        continue;
                    }
                    if (i >= endNum)
                    {
                        break;
                    }

                    Document doc     = searcher.Doc(docs[i].Doc);
                    string   id      = doc.Get("id").ToString();
                    string   title   = doc.Get("title").ToString();
                    string   content = doc.Get("content").ToString();
                    string   date    = doc.Get("date").ToString();
                    string   param   = doc.Get("param").ToString();
                    string   mcode   = doc.Get("modcode").ToString();
                    string   param1  = doc.Get("searchparam1").ToString();
                    string   param2  = doc.Get("searchparam2").ToString();
                    string   param3  = doc.Get("searchparam3").ToString();
                    JObject  obj     = new JObject();
                    obj["id"] = id;

                    //创建HTMLFormatter,参数为高亮单词的前后缀
                    string   highLightTag        = Util.GetAppSetting("HighLightTag", "<font color=\"red\">|</font>");
                    string[] tarArr              = highLightTag.Split('|');
                    var      simpleHTMLFormatter = new SimpleHTMLFormatter(tarArr[0], tarArr[1]);
                    //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent
                    var highlighter = new Highlighter(simpleHTMLFormatter, new PanGu.Segment());
                    //设置每个摘要段的字符数
                    int highlightFragmentSize = Util.GetAppSetting("HighlightFragmentSize", "100").ToInt();
                    highlighter.FragmentSize = highlightFragmentSize;
                    //获取最匹配的摘要段
                    String bodyPreview = highlighter.GetBestFragment(word, content);
                    string newTitle    = highlighter.GetBestFragment(word, title);
                    if (!string.IsNullOrEmpty(newTitle))
                    {
                        title = newTitle;
                    }

                    obj["title"]        = title;
                    obj["content"]      = bodyPreview;
                    obj["date"]         = date;
                    obj["param"]        = param;
                    obj["modcode"]      = mcode;
                    obj["searchparam1"] = param1;
                    obj["searchparam2"] = param2;
                    obj["searchparam3"] = param3;
                    dataList.Add(obj);
                }
                searchResult.data = dataList;
            }
            analyzer.Close();
            searcher.Dispose();
            directory.Dispose();

            return(searchResult);
        }
Пример #15
0
        public override void Start()
        {
            string refreshPeriod = properties.ContainsKey("refresh") ? properties["refresh"] : "3600";
            long   period;

            if (!long.TryParse(refreshPeriod, out period))
            {
                period = 3600;
                log.Warn("Error parsing refresh period, defaulting to 1 hour");
            }
            log.DebugFormat("Refresh period {0} seconds", period);
            period *= 1000;  // per second
            try
            {
                // Initialize
                this.directory1 = InitializeIndex(new DirectoryInfo(Path.Combine(indexName, "1")));
                this.directory2 = InitializeIndex(new DirectoryInfo(Path.Combine(indexName, "2")));

                string current1Marker = Path.Combine(indexName, "current1");
                string current2Marker = Path.Combine(indexName, "current2");

                if (File.Exists(current1Marker))
                {
                    current = 1;
                }
                else if (File.Exists(current2Marker))
                {
                    current = 2;
                }
                else
                {
                    // no default
                    log.Debug("Setting directory 1 as current");
                    current = 1;
                    DirectoryInfo srcDir        = new DirectoryInfo(source);
                    DirectoryInfo destDir       = new DirectoryInfo(Path.Combine(indexName, current.ToString()));
                    int           sourceCurrent = -1;
                    if (File.Exists(Path.Combine(srcDir.Name, "current1")))
                    {
                        sourceCurrent = 1;
                    }
                    else if (File.Exists(Path.Combine(srcDir.Name, "current2")))
                    {
                        sourceCurrent = 2;
                    }

                    if (sourceCurrent != -1)
                    {
                        try
                        {
                            FileHelper.Synchronize(
                                new DirectoryInfo(Path.Combine(source, sourceCurrent.ToString())), destDir, true);
                        }
                        catch (IOException e)
                        {
                            throw new HibernateException("Umable to synchonize directory: " + indexName, e);
                        }
                    }

                    try
                    {
                        File.Create(current1Marker).Dispose();
                    }
                    catch (IOException e)
                    {
                        throw new HibernateException("Unable to create the directory marker file: " + indexName, e);
                    }
                }
                log.Debug("Current directory: " + current);
            }
            catch (IOException e)
            {
                throw new HibernateException("Unable to initialize index: " + directoryProviderName, e);
            }

            task  = new TriggerTask(this, source, indexName);
            timer = new Timer(task.Run, null, period, period);
        }
Пример #16
0
        /// <summary>
        /// 分页获取商品信息数据
        /// </summary>
        /// <param name="queryString"></param>
        /// <param name="pageIndex">第一页为1</param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List <Commodity> QueryIndexPage(string queryString, int pageIndex, int pageSize, out int totalCount, string priceFilter, string priceOrderBy)
        {
            totalCount = 0;
            IndexSearcher searcher = null;

            try
            {
                List <Commodity> ciList = new List <Commodity>();
                FSDirectory      dir    = FSDirectory.Open(StaticConstant.IndexPath);
                searcher = new IndexSearcher(dir);
                Analyzer analyzer = new PanGuAnalyzer();

                //--------------------------------------这里配置搜索条件
                QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "title", analyzer);
                Query       query  = parser.Parse(queryString);

                pageIndex = Math.Max(1, pageIndex);//索引从1开始
                int startIndex = (pageIndex - 1) * pageSize;
                int endIndex   = pageIndex * pageSize;

                NumericRangeFilter <float> numPriceFilter = null;
                if (!string.IsNullOrWhiteSpace(priceFilter))
                {
                    bool     isContainStart = priceFilter.StartsWith("[");
                    bool     isContainEnd   = priceFilter.EndsWith("]");
                    string[] floatArray     = priceFilter.Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Split(',');
                    float    start          = 0;
                    float    end            = 0;
                    if (!float.TryParse(floatArray[0], out start) || !float.TryParse(floatArray[1], out end))
                    {
                        throw new Exception("Wrong priceFilter");
                    }
                    numPriceFilter = NumericRangeFilter.NewFloatRange("price", start, end, isContainStart, isContainEnd);
                }

                Sort sort = new Sort();
                if (!string.IsNullOrWhiteSpace(priceOrderBy))
                {
                    SortField sortField = new SortField("price", SortField.FLOAT, priceOrderBy.EndsWith("asc", StringComparison.CurrentCultureIgnoreCase));
                    sort.SetSort(sortField);
                }

                TopDocs docs = searcher.Search(query, numPriceFilter, 10000, sort);
                //TopDocs docs = searcher.Search(query, null, 10000);

                totalCount = docs.TotalHits;
                //PrintScores(docs, startIndex, endIndex, searcher);
                for (int i = startIndex; i < endIndex && i < totalCount; i++)
                {
                    Document doc = searcher.Doc(docs.ScoreDocs[i].Doc);
                    ciList.Add(DocumentToCommodityInfo(doc));
                }

                return(ciList);
            }
            finally
            {
                if (searcher != null)
                {
                    searcher.Dispose();
                }
            }
        }
Пример #17
0
 /// <summary>
 /// Searches Lucene.
 /// </summary>
 /// <param name="luceneVersion">The lucene version.</param>
 /// <param name="fsDirectory">The fs directory.</param>
 /// <param name="hitLimit">The hit limit.</param>
 /// <param name="input">The input.</param>
 /// <returns></returns>
 public virtual SearchResult <TSearchData> Search(Version luceneVersion, FSDirectory fsDirectory, int hitLimit, string input)
 {
     return(string.IsNullOrEmpty(input) ? new SearchResult <TSearchData>() : this.PerformSearch(luceneVersion, fsDirectory, hitLimit, input));
 }
Пример #18
0
        private static void CreateIndexContent()
        {
            using (FSDirectory directory = FSDirectory.Open(new DirectoryInfo(m_directoryPath), new NativeFSLockFactory())) //指定索引文件(打开索引目录) FS指的是就是FileSystem
            {
                bool isUpdate = IndexReader.IndexExists(directory);                                                         //IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。
                if (isUpdate)
                {
                    //同时只能有一段代码对索引库进行写操作。当使用IndexWriter打开directory时会自动对索引库文件上锁。
                    //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不就解锁了吗?这个问题后面会解决)
                    if (IndexWriter.IsLocked(directory))
                    {
                        IndexWriter.Unlock(directory);
                    }
                }

                using (IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, IndexWriter.MaxFieldLength.UNLIMITED))                //向索引库中写索引。这时在这里加锁。
                {
                    //如果队列中有数据,获取队列中的数据写到Lucene.Net中。
                    while (Queue.Count > 0)
                    {
                        KeyValuePair <T, LuceneTypeEnum> keyValuePair = Queue.Dequeue();
                        T            model        = keyValuePair.Key;
                        Type         type         = model.GetType();
                        PropertyInfo propertyInfo = type.GetProperty(m_luceneDataModels[0].PropertyName);
                        writer.DeleteDocuments(new Term(m_luceneDataModels[0].FieldName, propertyInfo.GetValue(model).ObjectToString()));                        //删除
                        if (keyValuePair.Value == LuceneTypeEnum.Delete)
                        {
                            continue;
                        }
                        //表示一篇文档。
                        Document document = new Document();
                        //Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
                        //Field.Index. ANALYZED:进行分词保存:也就是要进行全文的字段要设置分词 保存(因为要进行模糊查询
                        //Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。
                        foreach (FieldDataModel item in m_luceneDataModels)
                        {
                            propertyInfo = type.GetProperty(item.PropertyName);
                            var propertyValue = propertyInfo.GetValue(model);
                            if (propertyValue != null)
                            {
                                string     valueString = propertyValue.ToString();
                                IFieldable fieldable   = null;
                                if (item.FieldType == TypeCode.String)
                                {
                                    fieldable = new Field(item.FieldName, propertyInfo.GetValue(model).ObjectToString(), item.Store, item.Index, item.TermVector);
                                }
                                else
                                {
                                    NumericField numericField = new NumericField(item.FieldName, item.Store, item.Index == Field.Index.ANALYZED_NO_NORMS);
                                    switch (item.FieldType)
                                    {
                                    case TypeCode.Double:
                                        numericField.SetDoubleValue(Convert.ToDouble(valueString));
                                        break;

                                    case TypeCode.Single:
                                        numericField.SetFloatValue(Convert.ToSingle(valueString));
                                        break;

                                    case TypeCode.Int32:
                                        numericField.SetIntValue(Convert.ToInt32(valueString));
                                        break;

                                    case TypeCode.Int64:
                                        numericField.SetLongValue(Convert.ToInt64(valueString));
                                        break;

                                    default:
                                        break;
                                    }
                                    fieldable = numericField;
                                }
                                document.Add(fieldable);
                            }
                        }
                        writer.AddDocument(document);
                    }
                }                //会自动解锁。
            }
        }
Пример #19
0
        /// <summary>
        /// Performs a simple search.
        /// </summary>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="hitLimit">The hit limit.</param>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public virtual SearchResult <TSearchData> SimpleSearch(Version luceneVersion, FSDirectory fsDirectory, int hitLimit, string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(new SearchResult <TSearchData>());
            }

            var terms = input.Trim().Replace("-", " ").Split(' ').Where(x => !string.IsNullOrEmpty(x)).Select(x => x.Trim() + "*");

            input = string.Join(" ", terms);

            return(this.PerformSearch(luceneVersion, fsDirectory, hitLimit, input));
        }
        private void BindSearchResult(string keyword, int startIndex, int pageSize, out int totalCount)
        {
            string        indexPath = Context.Server.MapPath("~/Index"); // 索引文档保存位置
            FSDirectory   directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
            IndexReader   reader    = IndexReader.Open(directory, true);
            IndexSearcher searcher  = new IndexSearcher(reader);

            #region v1.0 单条件查询
            //// 查询条件
            //PhraseQuery query = new PhraseQuery();
            //// 分词后加入查询
            //IEnumerable<string> keyList = SplitHelper.SplitWords(keyword);
            //foreach (var key in keyList)
            //{
            //    query.Add(new Term("msg", key));
            //}
            //// 两个词的距离大于100(经验值)就不放入搜索结果,因为距离太远相关度就不高了
            //query.SetSlop(100);
            #endregion

            #region v2.0 多条件查询
            IEnumerable <string> keyList = SplitHelper.SplitWords(keyword);

            PhraseQuery queryTitle = new PhraseQuery();
            foreach (var key in keyList)
            {
                queryTitle.Add(new Term("title", key));
            }
            queryTitle.SetSlop(100);

            PhraseQuery queryMsg = new PhraseQuery();
            foreach (var key in keyList)
            {
                queryMsg.Add(new Term("msg", key));
            }
            queryMsg.SetSlop(100);

            BooleanQuery query = new BooleanQuery();
            query.Add(queryTitle, BooleanClause.Occur.SHOULD); // SHOULD => 可以有,但不是必须的
            query.Add(queryTitle, BooleanClause.Occur.SHOULD); // SHOULD => 可以有,但不是必须的
            #endregion

            // TopScoreDocCollector:盛放查询结果的容器
            TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
            // 使用query这个查询条件进行搜索,搜索结果放入collector
            searcher.Search(query, null, collector);
            // 首先获取总条数
            totalCount = collector.GetTotalHits();
            // 从查询结果中取出第m条到第n条的数据
            ScoreDoc[] docs = collector.TopDocs(startIndex, pageSize).scoreDocs;
            // 遍历查询结果
            IList <SearchResult> resultList = new List <SearchResult>();
            for (int i = 0; i < docs.Length; i++)
            {
                // 拿到文档的id,因为Document可能非常占内存(DataSet和DataReader的区别)
                int docId = docs[i].doc;
                // 所以查询结果中只有id,具体内容需要二次查询
                // 根据id查询内容:放进去的是Document,查出来的还是Document
                Document     doc    = searcher.Doc(docId);
                SearchResult result = new SearchResult();
                result.Url   = "ViewArticle.aspx?id=" + doc.Get("id");
                result.Title = HighlightHelper.HighLight(keyword, doc.Get("title"));
                result.Msg   = HighlightHelper.HighLight(keyword, doc.Get("msg")) + "......";

                resultList.Add(result);
            }

            // 绑定到Repeater
            rptSearchResult.DataSource = resultList;
            rptSearchResult.DataBind();
        }
Пример #21
0
        public static void Main(string[] args)
        {
            if (args.Length < 5)
            {
                // LUCENENET specific - our wrapper console shows the correct usage
                throw new ArgumentException();
                //Console.Error.WriteLine("Usage: MultiPassIndexSplitter -out <outputDir> -num <numParts> [-seq] <inputIndex1> [<inputIndex2 ...]");
                //Console.Error.WriteLine("\tinputIndex\tpath to input index, multiple values are ok");
                //Console.Error.WriteLine("\t-out ouputDir\tpath to output directory to contain partial indexes");
                //Console.Error.WriteLine("\t-num numParts\tnumber of parts to produce");
                //Console.Error.WriteLine("\t-seq\tsequential docid-range split (default is round-robin)");
                //Environment.Exit(-1);
            }
            List <IndexReader> indexes = new List <IndexReader>();

            try
            {
                string outDir   = null;
                int    numParts = -1;
                bool   seq      = false;
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i].Equals("-out"))
                    {
                        outDir = args[++i];
                    }
                    else if (args[i].Equals("-num"))
                    {
                        numParts = Convert.ToInt32(args[++i]);
                    }
                    else if (args[i].Equals("-seq"))
                    {
                        seq = true;
                    }
                    else
                    {
                        DirectoryInfo file = new DirectoryInfo(args[i]);
                        if (!file.Exists)
                        {
                            Console.Error.WriteLine("Invalid input path - skipping: " + file);
                            continue;
                        }
                        using (Store.Directory dir = FSDirectory.Open(new DirectoryInfo(args[i])))
                        {
                            try
                            {
                                if (!DirectoryReader.IndexExists(dir))
                                {
                                    Console.Error.WriteLine("Invalid input index - skipping: " + file);
                                    continue;
                                }
                            }
                            catch (Exception)
                            {
                                Console.Error.WriteLine("Invalid input index - skipping: " + file);
                                continue;
                            }
                            indexes.Add(DirectoryReader.Open(dir));
                        }
                    }
                }
                if (outDir == null)
                {
                    throw new Exception("Required argument missing: -out outputDir");
                }
                if (numParts < 2)
                {
                    throw new Exception("Invalid value of required argument: -num numParts");
                }
                if (indexes.Count == 0)
                {
                    throw new Exception("No input indexes to process");
                }
                DirectoryInfo @out = new DirectoryInfo(outDir);
                @out.Create();
                if (!new DirectoryInfo(outDir).Exists)
                {
                    throw new Exception("Can't create output directory: " + @out);
                }
                Store.Directory[] dirs = new Store.Directory[numParts];
                try
                {
                    for (int i = 0; i < numParts; i++)
                    {
                        dirs[i] = FSDirectory.Open(new DirectoryInfo(Path.Combine(@out.FullName, "part-" + i)));
                    }
                    MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
                    IndexReader            input;
                    if (indexes.Count == 1)
                    {
                        input = indexes[0];
                    }
                    else
                    {
                        input = new MultiReader(indexes.ToArray());
                    }
#pragma warning disable 612, 618
                    splitter.Split(LuceneVersion.LUCENE_CURRENT, input, dirs, seq);
#pragma warning restore 612, 618
                }
                finally
                {
                    // LUCENENET specific - properly dispose directories to prevent resource leaks
                    IOUtils.Dispose(dirs);
                }
            }
            finally
            {
                // LUCENENET specific - properly dispose index readers to prevent resource leaks
                IOUtils.Dispose(indexes);
            }
        }
Пример #22
0
        // FIXME :default impl

        /*public BrowseServiceImpl() : this(new FileInfo(System.getProperty("index.directory")))
         * {
         * }*/

        private BoboIndexReader NewIndexReader()
        {
            Directory idxDir = FSDirectory.Open(_idxDir);

            return(NewIndexReader(idxDir));
        }
Пример #23
0
        public IEnumerable <IHit> Query(int pageIndex, int pageSize, out int totalCount, out IEnumerable <FacetGroup> facetedResults)
        {
            totalCount     = 0;
            facetedResults = null;

            if (searchPaths == null || searchPaths.Count <= 0)
            {
                searchPaths.AddRange(indexPaths.Values.Select(o => o.Path));
            }

            List <LuceneHit> results = new List <LuceneHit>();

            List <IndexSearcher> subSearchs = new List <IndexSearcher>();

            searchPaths.ForEach(o => subSearchs.Add(new IndexSearcher(FSDirectory.Open(o))));

            if (facetFields != null && facetFields.Count > 0)
            {
                var         facetGroups     = new List <FacetGroup>();
                var         mainQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(query));
                MultiReader readers         = new MultiReader(subSearchs.Select(o => o.IndexReader).ToArray());

                foreach (var facetField in facetFields)
                {
                    FacetGroup fg = new FacetGroup();
                    fg.FieldName = facetFieldNameProvider.GetMapName(TypeName, facetField);
                    var items = new List <FacetItem>();

                    var allDistinctField = FieldCache_Fields.DEFAULT.GetStrings(readers, facetField).Distinct().ToArray();
                    int totalHits        = 0;

                    Parallel.ForEach(allDistinctField, fieldValue =>
                    {
                        //foreach (var fieldValue in allDistinctField)
                        //{
                        var facetQuery       = new TermQuery(new Term(facetField, fieldValue));
                        var facetQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(facetQuery));

                        var bs = new OpenBitSetDISI(facetQueryFilter.GetDocIdSet(readers).Iterator(), readers.MaxDoc);
                        bs.InPlaceAnd(mainQueryFilter.GetDocIdSet(readers).Iterator());
                        int count = (Int32)bs.Cardinality();

                        FacetItem item  = new FacetItem();
                        item.GroupValue = fieldValue;
                        item.Count      = count;

                        items.Add(item);
                        totalHits += count;
                    }
                                     );

                    fg.FacetItems = items.OrderByDescending(o => o.Count);
                    fg.TotalHits  = totalHits;

                    facetGroups.Add(fg);
                }

                facetedResults = facetGroups.OrderBy(o => o.FieldName);
            }
            ParallelMultiSearcher searcher = new ParallelMultiSearcher(subSearchs.ToArray());
            Sort sort = null;

            if (sortFields != null && sortFields.Count > 0)
            {
                sort = new Sort(sortFields.ToArray());
            }

            int maxDoc     = searcher.MaxDoc;
            int startIndex = 0;

            if (pageIndex >= 0 && pageSize > 0)
            {
                startIndex = pageIndex * pageSize;
                maxDoc     = pageSize * (pageIndex + 1);
            }
            var docs = sort == null?searcher.Search(query, null, maxDoc) : searcher.Search(query, null, maxDoc, sort);

            totalCount = docs.TotalHits;
            int endIndex = docs.TotalHits - startIndex;

            for (int i = startIndex; i < endIndex; i++)
            {
                LuceneHit h = new LuceneHit(TypeName, DocumentBuilder, searcher.Doc(docs.ScoreDocs[i].Doc));
                results.Add(h);
            }
            return(results);
        }
Пример #24
0
        /// <summary>Index all text files under a directory.</summary>
        public static void Main(string[] args)
        {
            // The <CONSOLE_APP_NAME> should be the assembly name of the application
            // this code is compiled into. In .NET Framework, it is the name of the EXE file.
            // In .NET Core, you have the option of compiling this into either an EXE or a DLL
            // (see https://docs.microsoft.com/en-us/dotnet/core/deploying/index).
            // In the latter case, the <CONSOLE_APP_NAME> will be "dotnet <DLL_NAME>.dll".
            string usage = "Usage: <CONSOLE_APP_NAME> <INDEX_DIRECTORY> <SOURCE_DIRECTORY> "
                           + "[-u|--update]\n\n"
                           + "This indexes the documents in <SOURCE_DIRECTORY>, creating a Lucene index"
                           + "in <INDEX_DIRECTORY> that can be searched with the search-files demo.";

            // Validate required arguments are present.
            // If not, show usage information.
            if (args.Length < 2)
            {
                Console.WriteLine(usage);
                Environment.Exit(1);
            }
            string indexPath  = args[0];
            string sourcePath = args[1];
            bool   create     = true;

            for (int i = 0; i < args.Length; i++)
            {
                if ("-u".Equals(args[i], StringComparison.Ordinal) || "--update".Equals(args[i], StringComparison.Ordinal))
                {
                    create = false;
                }
            }

            DirectoryInfo sourceDirectory = new DirectoryInfo(sourcePath);

            if (!sourceDirectory.Exists)
            {
                Console.WriteLine("Source directory '" + sourcePath + "' does not exist, please check the path");
                Environment.Exit(1);
            }

            DateTime start = DateTime.UtcNow;

            try
            {
                Console.WriteLine("Indexing to directory '" + indexPath + "'...");

                Store.Directory dir = FSDirectory.Open(indexPath);
                // :Post-Release-Update-Version.LUCENE_XY:
                Analyzer          analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);
                IndexWriterConfig iwc      = new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer);

                if (create)
                {
                    // Create a new index in the directory, removing any
                    // previously indexed documents:
                    iwc.OpenMode = OpenMode.CREATE;
                }
                else
                {
                    // Add new documents to an existing index:
                    iwc.OpenMode = OpenMode.CREATE_OR_APPEND;
                }

                // Optional: for better indexing performance, if you
                // are indexing many documents, increase the RAM
                // buffer.
                //
                // iwc.RAMBufferSizeMB = 256.0;

                using (IndexWriter writer = new IndexWriter(dir, iwc))
                {
                    IndexDocs(writer, sourceDirectory);

                    // NOTE: if you want to maximize search performance,
                    // you can optionally call forceMerge here.  This can be
                    // a terribly costly operation, so generally it's only
                    // worth it when your index is relatively static (ie
                    // you're done adding documents to it):
                    //
                    // writer.ForceMerge(1);
                }

                DateTime end = DateTime.UtcNow;
                Console.WriteLine((end - start).TotalMilliseconds + " total milliseconds");
            }
            catch (IOException e)
            {
                Console.WriteLine(" caught a " + e.GetType() +
                                  "\n with message: " + e.Message);
            }
        }
Пример #25
0
 public SnapshotManager(FSDirectory fsdir)
 {
     this.fsdir = fsdir;
 }
Пример #26
0
        //string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.IndexOf("\\bin"));


        //TREBA INJECTOVATI PUTANJU U OVAJ STATIC PROPERTY, STAVLJENO DA NE VRACA NISTA DA BI BUILD PROSAO
        //private static string LuceneIndexDirectory
        //{

        //    get
        //    {
        //        //return "";
        //        //kako radi
        //        //return Path.Combine(_env.WebRootPath, "/Lucene");

        //        return Path.Combine(_env.ContentRootPath, "~/Lucene");



        //        //kako je bilo
        //        //return HttpContext.Current.Server.MapPath("~/Lucene");

        //    }
        //}

        public List <KbSearchResultItemViewModel> DoSearch(string text, int page = 1, int resultCount = 20)
        {
            var LuceneIndexDirectory = Path.Combine(_env.ContentRootPath, "Lucene");

            try
            {
                if (page < 1)
                {
                    throw new ArgumentException("Page");
                }

                if (string.IsNullOrEmpty(text))
                {
                    throw new ArgumentNullException("Search Text");
                }

                var results  = new List <KbSearchResultItemViewModel>();
                var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
                var searcher = new IndexSearcher(FSDirectory.Open(LuceneIndexDirectory));


                var fields = new string[] { "Title", "Content" };
                var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields, analyzer);
                var q      = ParseQuery(text, parser);
                var hits   = searcher.Search(q, page * resultCount);
                if (hits.ScoreDocs.Any())
                {
                    for (int i = (page - 1) * resultCount; i < hits.ScoreDocs.Count(); i++)
                    {
                        var doc = searcher.Doc(hits.ScoreDocs[i].Doc);

                        var item = new KbSearchResultItemViewModel
                        {
                            ArticleId    = Convert.ToInt32(doc.Get("Id").ToString().Replace("KB-", string.Empty).Replace("AT-", string.Empty)),
                            IsArticle    = doc.Get("Id").StartsWith("KB-"),
                            IsAttachment = doc.Get("Id").StartsWith("AT-"),
                            ArticleTitle = doc.Get("Title")
                        };
                        //2808
                        var article = _context.Articles.FirstOrDefault(a => a.Id == item.ArticleId);
                        if (article != null)
                        {
                            item.ArticleSefName = article.SefName;
                        }
                        else
                        {
                            item.ArticleSefName = null;
                        }

                        results.Add(item);
                    }
                }

                searcher.Dispose();
                return(results);
            }
            catch (Exception ex)
            {
                LogManager.GetCurrentClassLogger().Error(ex);
                throw;
            }
        }
Пример #27
0
        private void btnFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dia = new FolderBrowserDialog();
            DialogResult        res = dia.ShowDialog();

            if (res != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            FSDirectory dir = FSDirectory.GetDirectory(Environment.CurrentDirectory + "\\LuceneIndex");

            //Lucene.Net.Store.RAMDirectory dir = new RAMDirectory();
            Lucene.Net.Analysis.StopAnalyzer an = new Lucene.Net.Analysis.StopAnalyzer();
            IndexWriter   wr     = new IndexWriter(dir, an, true);
            DirectoryInfo diMain = new DirectoryInfo(dia.SelectedPath);

            foreach (FileInfo fi in diMain.GetFiles())
            {
                Document doc = new Document();
                doc.Add(new Field("title", fi.Name, Field.Store.YES, Field.Index.NO));
                doc.Add(new Field("text", File.ReadAllText(fi.FullName), Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES));
                wr.AddDocument(doc);
            }
            wr.Optimize();
            wr.Flush();
            wr.Close();
            dir.Close();

            IndexReader reader = IndexReader.Open(dir);

            for (int i = 0; i < reader.MaxDoc(); i++)
            {
                if (reader.IsDeleted(i))
                {
                    continue;
                }

                Document doc   = reader.Document(i);
                String   docId = doc.Get("docId");
                foreach (TermFreqVector vector in reader.GetTermFreqVectors(i))
                {
                    foreach (string term in vector.GetTerms())
                    {
                        Console.WriteLine(term);
                    }
                }
                // do something with docId here...
            }
            //IndexSearcher search = new IndexSearcher(wr.GetReader());

            //MoreLikeThis mlt = new MoreLikeThis(wr.GetReader());
            //FileInfo fitarget = new FileInfo(@"C:\Users\peacemaker\Desktop\TestNoBitcoin\test.txt");
            //Query query = mlt.Like(fitarget);

            //var hits = search.Search(query, int.MaxValue);
            //foreach (ScoreDoc doc in hits.ScoreDocs)
            //{
            //    textBox1.Text += doc.Score + Environment.NewLine;
            //}
        }
Пример #28
0
 public TipologiasUpdater()
     : base(FSDirectory.Open(Util.TipologiasPath))
 {
 }
Пример #29
0
        /// <summary>
        /// 更新索引库操作
        /// </summary>
        private void CRUDIndex()
        {
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
            bool        isExist   = IndexReader.IndexExists(directory);

            if (isExist)
            {
                if (IndexWriter.IsLocked(directory))
                {
                    IndexWriter.Unlock(directory);
                }
            }
            IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isExist, IndexWriter.MaxFieldLength.UNLIMITED);

            if (bookQueue.Count > 0)
            {
                for (int i = 0; i < 5; i++)
                {
                    Document document = new Document();
                    if (bookQueue.Count <= 0)
                    {
                        break;
                    }

                    MovieViewModel book = bookQueue.Dequeue();

                    if (book.IT == IndexType.Delete)
                    {
                        writer.DeleteDocuments(new Term("id", book.MovieID.ToString()));
                    }
                    else if (book.IT == IndexType.Modify || book.IT == IndexType.Insert)
                    {
                        //先删除 再新增
                        writer.DeleteDocuments(new Term("id", book.MovieID.ToString()));

                        document.Add(new Field("id", book.MovieID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                        document.Add(new Field("title", book.MovieTitle, Field.Store.YES, Field.Index.ANALYZED,
                                               Field.TermVector.WITH_POSITIONS_OFFSETS));

                        document.Add(new Field("content", book.MovieContent, Field.Store.YES, Field.Index.ANALYZED,
                                               Field.TermVector.WITH_POSITIONS_OFFSETS));

                        document.Add(new Field("stars", book.Stars, Field.Store.YES, Field.Index.NO,
                                               Field.TermVector.NO));

                        document.Add(new Field("director", book.Director ?? "", Field.Store.YES, Field.Index.ANALYZED,
                                               Field.TermVector.WITH_POSITIONS_OFFSETS));
                        document.Add(new Field("picfile", book.PictureFile, Field.Store.YES, Field.Index.NO,
                                               Field.TermVector.NO));

                        document.Add(new Field("grade", book.Grade.ToString(), Field.Store.YES, Field.Index.NO,
                                               Field.TermVector.NO));

                        writer.AddDocument(document);
                    }
                }
            }
            writer.Close();
            directory.Close();
        }
Пример #30
0
 public LuceneService(Console console)
 {
     luceneIndexDirectory = FSDirectory.Open(CONFIG.LUCENE_INDEX_PATH);
     this.console         = console;
 }
Пример #31
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public IndexSplitter(java.io.File dir) throws java.io.IOException
	  public IndexSplitter(File dir)
	  {
		this.dir = dir;
		fsDir = FSDirectory.open(dir);
		infos = new SegmentInfos();
		infos.read(fsDir);
	  }