/// <summary>
        /// RAMDirectoryのインデックスをFSIndexとして保存する
        /// </summary>
        /// <param name="ramDir"></param>
        /// <param name="savePath"></param>
        /// <param name="analyzer"></param>
        private static void SaveFSIndexFromRAMIndex(FlexLucene.Store.Directory ramDir, string savePath, Analyzer analyzer)
        {
            var         fsIndexDir    = FSDirectory.Open(FileSystems.getDefault().getPath(savePath));
            var         fsConfig      = new IndexWriterConfig(analyzer);
            IndexWriter fsIndexWriter = new IndexWriter(fsIndexDir, fsConfig);

            try {
                fsIndexWriter.AddIndexes(new FlexLucene.Store.Directory[] { ramDir });
                fsIndexWriter.Commit();
            } finally {
                fsIndexWriter.Close();
            }
        }
Beispiel #2
0
        public static void TestFlexLuceneRAM(string[] args)
        {
            StandardAnalyzer analyzer = new StandardAnalyzer();

            FlexLucene.Store.Directory index = (FlexLucene.Store.Directory) new RAMDirectory();
            config = new IndexWriterConfig((Analyzer)analyzer);
            cnf    = new FacetsConfig();
            cnf.SetIndexFieldName("title", "facet_title");
            cnf.SetIndexFieldName("isbn", "facet_isbn");
            LuceneTest.taxoDir    = (FlexLucene.Store.Directory) new RAMDirectory();
            LuceneTest.taxoWriter = (TaxonomyWriter) new FlexLucene.Facet.Taxonomy.Directory.DirectoryTaxonomyWriter(LuceneTest.taxoDir, IndexWriterConfigOpenMode.CREATE);

            IndexWriter w = new IndexWriter(index, LuceneTest.config);

            addDoc(w, "Lucene in Action", "9900001");
            addDoc(w, "Lucene for Dummies", "9900002");
            addDoc(w, "Lucene for Dummies 2", "9900003");

            w.close();
            String               querystr    = "isbn:99*";
            Query                q           = new QueryParser("title", (Analyzer)analyzer).Parse(querystr);
            int                  hitsPerPage = 10;
            IndexReader          reader      = (IndexReader)DirectoryReader.Open(index);
            IndexSearcher        searcher    = new IndexSearcher(reader);
            TopScoreDocCollector collector   = TopScoreDocCollector.Create(hitsPerPage);

            searcher.Search(q, (Collector)collector);
            ScoreDoc[] hits = collector.TopDocs().ScoreDocs;
            Console.WriteLine("Found " + hits.Length + " hits.");
            for (int i = 0; i < hits.Length; ++i)
            {
                int      docId = hits [i].Doc;
                Document d     = searcher.Doc(docId);
                Console.WriteLine(i + 1 + ". " + d.Get("isbn") + "\t" + d.Get("title"));
            }
            SortedSetDocValuesReaderState state = (SortedSetDocValuesReaderState) new DefaultSortedSetDocValuesReaderState(reader, "facet_isbn");
            FacetsCollector fc = new FacetsCollector();

            FacetsCollector.Search(searcher, q, 10, (Collector)fc);
            Facets      facets = (Facets) new SortedSetDocValuesFacetCounts(state, fc);
            FacetResult result = facets.GetTopChildren(10, "isbn", new String[0]);

            for (int j = 0; j < result.ChildCount; ++j)
            {
                LabelAndValue lv = result.LabelValues [j];
                Console.WriteLine(String.Format("Label={0}, Value={1}", lv.Label, lv.Value));
            }
            reader.close();
        }
        /// <summary>
        /// インデックスをマージして本番ディレクトリへ移動
        /// </summary>
        /// <param name="analyzer"></param>
        /// <param name="rootPath"></param>
        /// <param name="targetDir"></param>
        public static void MergeAndMove(
            Analyzer analyzer,
            string rootPath,
            string targetDir)
        {
            System.IO.Directory.CreateDirectory(rootPath + BuildDirName);
            FileUtil.DeleteDirectory(new DirectoryInfo(rootPath + BuildDirName));

            //2つのインデックスをマージ
            var fsIndexDir1 = FSDirectory.Open(FileSystems.getDefault().getPath(rootPath + BuildDirName + "1"));
            var fsIndexDir2 = FSDirectory.Open(FileSystems.getDefault().getPath(rootPath + BuildDirName + "2"));
            var tmpConf     = new IndexWriterConfig(analyzer);
            var dirs        = new FlexLucene.Store.Directory[] { fsIndexDir1, fsIndexDir2 };
            var ramIndexDir = MergeIndexAsRAMDir(dirs, tmpConf);

            //インデックスファイルを一時構築用に保存
            SaveFSIndexFromRAMIndex(ramIndexDir, rootPath + BuildDirName, analyzer);

            //インデックスファイルを一時構築用から検索用に移動
            CopyIndexDir(rootPath + BuildDirName, rootPath + IndexDirName);
        }