Пример #1
0
        public void FullTextSearchTest()
        {
            Analyzer  analyzer  = new YKChineseAnalyzer();
            Directory directory = new RAMDirectory();

            IndexWriter iwriter = new IndexWriter(directory, analyzer, new IndexWriter.MaxFieldLength(100));

            Document[] doc = new Document[6];
            for (int i = 0; i < 6; i++)
            {
                doc[i] = new Document();
            }
            string[] text = { "穷 人,中华人民共和国中央人民政府", "中国是个伟大的国家", "我出生在美丽的中国,我爱中国,中国",
                              "中华美丽的中国爱你",         "美国跟中国式的国家", "文革,卧槽,你是中国的,xinsheng" };

            doc[0].Add(new Field("fieldname0", new System.IO.StringReader(text[0]), Field.TermVector.YES));
            doc[0].Add(new Field("fieldname1", new System.IO.StringReader(text[1]), Field.TermVector.YES));
            doc[0].Add(new Field("fieldname2", new System.IO.StringReader(text[2]), Field.TermVector.YES));
            doc[0].Add(new Field("fieldname3", new System.IO.StringReader(text[3]), Field.TermVector.YES));
            doc[0].Add(new Field("fieldname4", new System.IO.StringReader(text[4]), Field.TermVector.YES));
            doc[0].Add(new Field("fieldname5", new System.IO.StringReader(text[5]), Field.TermVector.YES));
            iwriter.AddDocument(doc[0]);
            iwriter.Dispose();

            IndexSearcher isearcher = new IndexSearcher(directory, true);

            string[] multiFields         = { "fieldname0", "fieldname1", "fieldname5" };
            MultiFieldQueryParser parser = new MultiFieldQueryParser(
                Lucene.Net.Util.Version.LUCENE_29, multiFields, analyzer);
            var token = parser.GetNextToken();

            // 设定具体的搜索词
            Query query = parser.Parse("文革");

            TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true);

            isearcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器
            var docs2 = collector.TopDocs();

            System.Console.WriteLine(docs2);
        }