Lucene.Net

Posted UcAsp.Net技术

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lucene.Net相关的知识,希望对你有一定的参考价值。

创建索引

Lucene.Net.Store.Directory dir = FSDirectory.Open(AppDomain.CurrentDomain.BaseDirectory + "/index/data");
            bool flag = true;
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/index/data/segments.gen"))
            {
                flag = false;
            }
            IndexWriter write = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT), flag, IndexWriter.MaxFieldLength.LIMITED);
            write.SetMaxFieldLength(2);


 Document doc = new Document();
                Field field0 = new Field("operation_log_ukid", f0.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
                Field field1 = new Field("related_order_ukid", f1.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
                Field field2 = new Field("related_type", f2, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
                Field field3 = new Field("operation_code", f3, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
write.AddDocument(doc);
 write.Optimize();
            write.Flush(true, true, true);
            write.Dispose();
            conn.Close();

 查询索引

Lucene.Net.Store.Directory dir = FSDirectory.Open(AppDomain.CurrentDomain.BaseDirectory + "/index/data");
            IndexReader rd = DirectoryReader.Open(dir, true);

            BooleanQuery booleanQuery = new BooleanQuery();
            IndexSearcher search = new IndexSearcher(rd);

            //QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, "operation_name", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT));           
            Query query = new TermRangeQuery("log_date", "2016051905", "2016052000", true, true);
            booleanQuery.Add(query,Occur.MUST);
            Query qyery2 = new QueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, "related_type", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT)).Parse("E");
            booleanQuery.Add(qyery2, Occur.MUST);
            Query query3 = new TermQuery(new Term("operation_log_ukid", "900216000021301"));
           booleanQuery.Add(query3, Occur.MUST);           
            TopDocs q = search.Search(booleanQuery, null, 100);
            int l = q.TotalHits;
            foreach (ScoreDoc sd in q.ScoreDocs)
            {

                Document doc = search.Doc(sd.Doc);
                long operation_log_ukid = long.Parse(doc.Get("operation_log_ukid"));
                string log_date = doc.Get("log_date");
                string related_type = doc.Get("related_type");

            }
            search.Dispose();

 

以上是关于Lucene.Net的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Lucene.net 实现自定义过滤器?

Lucene.Net

Lucene.Net 最佳实践

jieba.NET与Lucene.Net的集成

框架:Lucene.net

使用Lucene.Net实现全文检索