예제 #1
0
        public void IndexMenuItems(IList <Restaurant> restaurants)
        {
            using var analyzer = new EnglishAnalyzer(LuceneVersion.LUCENE_48);
            using var writer   = new IndexWriter(GetDirectory(MenuItemIndexFolder), new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer));
            foreach (var restaurant in restaurants)
            {
                foreach (MenuCategory category in restaurant.Categories)
                {
                    foreach (MenuItem item in category.MenuItems)
                    {
                        var doc = new Document();

                        //TODO: implement custom attributes to mark which entity fields should indexed
                        doc.Add(new StringField("RestaurantId", restaurant.Id.ToString(), Field.Store.YES));
                        doc.Add(new StringField("CategoryId", category.Id.ToString(), Field.Store.YES));
                        doc.Add(new TextField("CategoryName", category.Name, Field.Store.YES)
                        {
                            Boost = 4.0f
                        });
                        doc.Add(new StringField("MenuItemId", item.Id.ToString(), Field.Store.YES));
                        doc.Add(new TextField("MenuItemName", item.Name, Field.Store.YES));

                        writer.AddDocument(doc);
                    }
                }
            }

            writer.Commit();
        }
예제 #2
0
        static public void query(Sentence[] sentences, string query, string indexPath = "luceneIndex")
        {
            Dictionary <string, Sentence> map = new Dictionary <string, Sentence>();

            foreach (Sentence s in sentences)
            {
                if (!map.ContainsKey(s.sentnece))
                {
                    map.Add(s.sentnece, s);
                }
            }

            java.io.File    indexDir       = new java.io.File(indexPath);
            FSDirectory     indexFSDir     = new SimpleFSDirectory(indexDir);
            IndexSearcher   searcher       = new IndexSearcher(IndexReader.open(indexFSDir));
            EnglishAnalyzer luceneAnalyzer = new EnglishAnalyzer(org.apache.lucene.util.Version.LUCENE_35);
            QueryParser     qp             = new QueryParser(org.apache.lucene.util.Version.LUCENE_35, "text", luceneAnalyzer);

            Query   q     = qp.parse(query);
            TopDocs tdocs = searcher.search(q, 99999999);

            ScoreDoc[] sdocs = tdocs.scoreDocs;
            for (int i = 0; i < sdocs.Length; i++)
            {
                ScoreDoc sd  = sdocs[i];
                Document res = searcher.doc(sd.doc);

                string docText = res.get("text");
                float  score   = sd.score;

                map[docText].lucene = score;
            }
            searcher.close();
        }
예제 #3
0
        private IList <Document> Search(string searchText, string itemsFolder, string[] fieldNames)
        {
            if (string.IsNullOrEmpty(searchText))
            {
                return(new List <Document>());
            }

            try
            {
                using var analyzer = new EnglishAnalyzer(LuceneVersion.LUCENE_48);
                using var reader   = DirectoryReader.Open(GetDirectory(itemsFolder));

                var searcher = new IndexSearcher(reader);
                var parser   = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, fieldNames, analyzer);
                var query    = parser.Parse(QueryParserBase.Escape(searchText.Trim()));

                //TODO: change 100 to page size when paging is implemented
                var hits = searcher.Search(query, null, 100, Sort.RELEVANCE).ScoreDocs;


                return(hits.Select(x => searcher.Doc(x.Doc)).ToList());
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #4
0
        /// <summary>
        /// 文本语法高亮的方法
        /// </summary>
        /// <param name="text">输入的文本</param>
        /// <param name="keys">搜索关键字 需要高亮的文本</param>
        /// <param name="analyEnum">选择分词方式</param>
        /// <returns>成功返回高亮的文本 失败返回空字符串</returns>
        public static string HightLightText(string text, string keys, AnalyzerEnum analyEnum)
        {
            string _hightText = string.Empty;
            SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color=\"red\">", "</font>");
            Highlighter         highlighter         = null;

            switch (analyEnum)
            {
            case AnalyzerEnum.SimpleAnalyzer:
                highlighter = new Highlighter(simpleHTMLFormatter, new SimpleAnalyzer());
                break;

            case AnalyzerEnum.EnglishAnalyzer:
                EnglishAnalyzer engEa = new EnglishAnalyzer();
                engEa.Init();
                highlighter = new Highlighter(simpleHTMLFormatter, engEa);
                break;

            case AnalyzerEnum.PanGuSegment:
                highlighter = new Highlighter(simpleHTMLFormatter, new PanGuAnalyzer());
                break;

            default:
                break;
            }
            if (highlighter != null)
            {
                highlighter.FragmentSize = 500000;
                _hightText = highlighter.GetBestFragment(keys, text);
            }
            return(_hightText);
        }
예제 #5
0
        private static void MoveLuceneIndex(String lng, int indexServer)
        {
            T.TraceMessage("Moving lucene index to server {0}", indexServer);

            Directory[] readers = new Directory[1];
            string      impDomain = string.Empty, impUser = string.Empty, impPass = string.Empty;
            string      destIndexBasePath = string.Empty;

            if (indexServer == 1)
            {
                destIndexBasePath = ConfigurationController.IndexRootPath;
            }

            Analyzer analyzer      = new SpanishAnalyzer(ConfigurationController.Stop_Words);
            string   destIndexPath = destIndexBasePath + "\\ES\\IDX";
            string   tempIndexPath = ConfigurationController.TempIndexRootPath + "/ES/IDX";

            if (lng.ToLower().Trim().Equals("en"))
            {
                destIndexPath = destIndexBasePath + "\\EN\\IDX";
                tempIndexPath = ConfigurationController.TempIndexRootPath + "/EN/IDX";
                analyzer      = new EnglishAnalyzer(ConfigurationController.Stop_Words);
            }
            if (lng.ToLower().Trim().Equals("he"))
            {
                destIndexPath = destIndexBasePath + "\\HE\\IDX";
                tempIndexPath = ConfigurationController.TempIndexRootPath + "/HE/IDX";
                analyzer      = new MorphAnalyzer(ConfigurationController.MorphFilesPath);
            }

            MoveIndexFiles(impDomain, impUser, impPass, destIndexPath, tempIndexPath, analyzer);
        }
예제 #6
0
        private void OpenIndexWriter(IndexerType type)
        {
            Analyzer analyzer;

            if (type == IndexerType.ENGLISH)
            {
                analyzer = new EnglishAnalyzer(VERSION);
            }
            else
            {
                analyzer = new SerbianAnalyzer(VERSION);
            }
            indexWriterConfig          = new IndexWriterConfig(VERSION, analyzer);
            indexWriterConfig.OpenMode = OpenMode.CREATE_OR_APPEND;
            indexWriter = new IndexWriter(indexDirectory, indexWriterConfig);
        }
        public virtual void TestLUCENENET615()
        {
            var english = new EnglishAnalyzer(Lucene.Net.Util.LuceneVersion.LUCENE_48);

            var whitespace = new WhitespaceAnalyzer(Lucene.Net.Util.LuceneVersion.LUCENE_48);

            var pf = new PerFieldAnalyzerWrapper(english, new JCG.Dictionary <string, Analyzer>()
            {
                { "foo", whitespace }
            });

            var test1 = english.GetTokenStream(null, "test"); // Does not throw

            var test2 = pf.GetTokenStream("", "test");        // works

            Assert.DoesNotThrow(() => pf.GetTokenStream(null, "test"), "GetTokenStream should not throw NullReferenceException with a null key");
        }
예제 #8
0
        static public void indexing(Sentence[] sentences, string indexPath = "luceneIndex")
        {
            if (System.IO.Directory.Exists(indexPath))
            {
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(indexPath);
                di.Delete(true);
            }

            java.io.File      indexDir       = new java.io.File(indexPath);
            EnglishAnalyzer   luceneAnalyzer = new EnglishAnalyzer(org.apache.lucene.util.Version.LUCENE_35);
            IndexWriterConfig config         = new IndexWriterConfig(org.apache.lucene.util.Version.LUCENE_35, luceneAnalyzer);
            FSDirectory       indexFSDir     = new SimpleFSDirectory(indexDir);
            IndexWriter       writer         = new IndexWriter(indexFSDir, config);

            foreach (Sentence s in sentences)
            {
                Document doc = new Document();
                doc.add(new Field("text", s.sentnece, Field.Store.YES, Field.Index.ANALYZED));
                writer.addDocument(doc);
            }
            writer.close();
        }
예제 #9
0
        /// <summary>
        /// Update Index by Language
        /// </summary>
        /// <param name="indexDir"></param>
        /// <param name="lng"></param>
        /// <returns></returns>
        public static void UpdateIndex(String lng)
        {
            Analyzer analyzer = new SpanishAnalyzer(ConfigurationController.Stop_Words);

            Directory indexDir = FSDirectory.Open(new System.IO.DirectoryInfo(ConfigurationController.TempIndexRootPath + "/ES/IDX"));

            if (lng.ToLower().Trim().Equals("en"))
            {
                indexDir = FSDirectory.Open(new System.IO.DirectoryInfo(ConfigurationController.TempIndexRootPath + "/EN/IDX"));
                analyzer = new EnglishAnalyzer(ConfigurationController.Stop_Words);
            }
            if (lng.ToLower().Trim().Equals("he"))
            {
                indexDir = FSDirectory.Open(new System.IO.DirectoryInfo(ConfigurationController.TempIndexRootPath + "/HE/IDX"));
                analyzer = new MorphAnalyzer(ConfigurationController.MorphFilesPath);
            }

            LuceneDao dao = new LuceneDao();

            dao.Analizer = analyzer;

            dao.UpdateIndex(indexDir, lng);
        }