示例#1
0
        protected void BeforeUpdateOrEditAsync(TArticle t)
        {
            var a = new SmartChineseAnalyzer(LuceneVersion.LUCENE_48);

            using var ts = a.GetTokenStream("tokens", new StringReader(t.SearchAbleString));
            var att    = ts.GetAttribute <ICharTermAttribute>();
            var tokens = new List <string>();

            ts.Reset();
            while (ts.IncrementToken())
            {
                tokens.Add(att.ToString());
            }
            t.Tokens = tokens;
        }
示例#2
0
        /// <inheritdoc/>
        public virtual void BeforeAddAndUpdate(T t)
        {
            var a = new SmartChineseAnalyzer(LuceneVersion.LUCENE_48);

            using var ts = a.GetTokenStream("tokens", new StringReader(t.SearchAbleString ?? ""));
            var att    = ts.GetAttribute <ICharTermAttribute>();
            var tokens = new List <string>();

            ts.Reset();
            while (ts.IncrementToken())
            {
                tokens.Add(att.ToString());
            }
            t.Tokens = tokens;
        }
        public static void ClearLuceneIndexRecord(string record_id)
        {
            // init lucene
            var analyzer    = new SmartChineseAnalyzer(AppLuceneVersion); //new StandardAnalyzer(Version.LUCENE_30);
            var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);

            using (var writer = new IndexWriter(_directory, indexConfig))
            {
                // remove older index entry
                var searchQuery = new TermQuery(new Term("Id", record_id));
                writer.DeleteDocuments(searchQuery);

                // close handles
                analyzer.Dispose();
                writer.Dispose();
            }
        }
        public static void AddUpdateLuceneIndex(IEnumerable <SearchData> sampleDatas)
        {
            // init lucene
            var analyzer    = new SmartChineseAnalyzer(AppLuceneVersion); //new StandardAnalyzer(Version.LUCENE_30);
            var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);

            using (var writer = new IndexWriter(_directory, indexConfig))
            {
                // add data to lucene search index (replaces older entries if any)
                foreach (var sampleData in sampleDatas)
                {
                    _addToLuceneIndex(sampleData, writer);
                }

                // close handles
                analyzer.Dispose();
                writer.Dispose();
            }
        }
        public static bool ClearLuceneIndex()
        {
            try
            {
                var analyzer    = new SmartChineseAnalyzer(AppLuceneVersion); //new StandardAnalyzer(Version.LUCENE_30);
                var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
                using (var writer = new IndexWriter(_directory, indexConfig))
                {
                    // remove older index entries
                    writer.DeleteAll();

                    // close handles
                    analyzer.Dispose();
                    writer.Dispose();
                }
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }
        private static IEnumerable <SearchData> _search(int pageIndex, int pageSize, out int totalCount, string keyword)
        {
            var hits_limit = 1000;
            //create an analyzer to process the text
            var analyzer = new SmartChineseAnalyzer(AppLuceneVersion);

            //create an index writer
            var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
            var writer      = new IndexWriter(_directory, indexConfig);



            var searcher = new IndexSearcher(writer.GetReader(applyAllDeletes: true));
            var parser   = new MultiFieldQueryParser
                               (AppLuceneVersion, new[] { "Id", "Name", "Description" }, analyzer);
            var query   = parseQuery(keyword, parser);
            var hits    = searcher.Search(query, null, hits_limit, Sort.RELEVANCE).ScoreDocs;
            var results = _mapLuceneToDataList(pageIndex, pageSize, hits, searcher);

            totalCount = hits.Length;
            analyzer.Dispose();
            return(results);
        }