• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            為生存而奔跑

               :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團(tuán)隊

            搜索

            •  

            積分與排名

            • 積分 - 328415
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

            本文僅記錄一些簡單的使用方法,供初學(xué)者參考。
            以下例子采用 Lucene.NET 1.9 版本,可取去 Lucene.Net 下載。

            1. 基本應(yīng)用
            using System;
            using System.Collections.Generic;
            using System.Text;
            using Lucene.Net;
            using Lucene.Net.Analysis;
            using Lucene.Net.Analysis.Standard;
            using Lucene.Net.Documents;
            using Lucene.Net.Index;
            using Lucene.Net.QueryParsers;
            using Lucene.Net.Search;
            using Lucene.Net.Store;
            using Lucene.Net.Util;

            namespace ConsoleApplication1.Lucene
            {
              public class LuceneTest
              {
                private const string FieldName = "name";
                private const string FieldValue = "value";

                private Directory directory = new RAMDirectory();
                private Analyzer analyzer = new StandardAnalyzer();

                public LuceneTest()
                {
                }

                private void Index()
                {
                  IndexWriter writer = new IndexWriter(directory, analyzer, true);
                  writer.maxFieldLength = 1000;
                  
                  for (int i = 1; i <= 100; i++)
                  {
                    Document document = new Document();

                    document.Add(new Field(FieldName, "name" + i, Field.Store.YES, Field.Index.UN_TOKENIZED));
                    document.Add(new Field(FieldValue, "Hello, World!", Field.Store.YES, Field.Index.TOKENIZED));

                    writer.AddDocument(document);
                  }

                  writer.Optimize();
                  writer.Close();
                }

                private void Search()
                {
                  Query query = QueryParser.Parse("name*", FieldName, analyzer);

                  IndexSearcher searcher = new IndexSearcher(directory);

                  Hits hits = searcher.Search(query);
                  
                  Console.WriteLine("符合條件記錄:{0}; 索引庫記錄總數(shù):{1}", hits.Length(), searcher.Reader.NumDocs());
                  for (int i = 0; i < hits.Length(); i++)
                  {
                    int docId = hits.Id(i);
                    string name = hits.Doc(i).Get(FieldName);
                    string value = hits.Doc(i).Get(FieldValue);
                    float score = hits.Score(i);

                    Console.WriteLine("{0}: DocId:{1}; Name:{2}; Value:{3}; Score:{4}", 
                      i + 1, docId, name, value, score);
                  }

                  searcher.Close();
                }
              }
            }

            除了 RAMDirectory,還可以使用 FSDirectory。(注意 FSDirectory.GetDirectory 的 create 參數(shù),為 true 時將刪除已有索引庫文件,可以通過 IndexReader.IndexExists() 方法判斷。)

            從指定目錄打開已有索引庫。
            private Directory directory = FSDirectory.GetDirectory("c:\index", false);

            將索引庫載入內(nèi)存,以提高搜索速度。
            private Directory directory = new RAMDirectory(FSDirectory.GetDirectory(@"c:\index", false));
            //或
            //private Directory directory = new RAMDirectory(c:\index");

            2. 多字段搜索

            使用 MultiFieldQueryParser 可以指定多個搜索字段。
            Query query = MultiFieldQueryParser.Parse("name*", new string[] { FieldName, FieldValue }, analyzer);

            IndexReader reader = IndexReader.Open(directory);
            IndexSearcher searcher = new IndexSearcher(reader);
            Hits hits = searcher.Search(query);

            3. 多條件搜索

            除了使用 QueryParser.Parse 分解復(fù)雜的搜索語法外,還可以通過組合多個 Query 來達(dá)到目的。
            Query query1 = new TermQuery(new Term(FieldValue, "name1")); // 詞語搜索
            Query query2 = new WildcardQuery(new Term(FieldName, "name*")); // 通配符 
            //Query query3 = new PrefixQuery(new Term(FieldName, "name1")); // 字段搜索 Field:Keyword,自動在結(jié)尾添加 *
            //Query query4 = new RangeQuery(new Term(FieldNumber, NumberTools.LongToString(11L)), new Term(FieldNumber, NumberTools.LongToString(13L)), true); // 范圍搜索
            //Query query5 = new FilteredQuery(query, filter); // 帶過濾條件的搜索
                  
            BooleanQuery query = new BooleanQuery();
            query.Add(query1, BooleanClause.Occur.MUST);
            query.Add(query2, BooleanClause.Occur.MUST);

            IndexSearcher searcher = new IndexSearcher(reader);
            Hits hits = searcher.Search(query);

            4. 設(shè)置權(quán)重

            可以給 Document 和 Field 增加權(quán)重(Boost),使其在搜索結(jié)果排名更加靠前。缺省情況下,搜索結(jié)果以 Document.Score 作為排序依據(jù),該數(shù)值越大排名越靠前。Boost 缺省值為 1。
            Score = Score * Boost

            通過上面的公式,我們就可以設(shè)置不同的權(quán)重來影響排名。

            如下面的例子中根據(jù) VIP 級別設(shè)定不同的權(quán)重。
            Document document = new Document();
            switch (vip)
            {
              case VIP.Gold: document.SetBoost(2F); break;
              case VIP.Argentine: document.SetBoost(1.5F); break;
            }

            只要 Boost 足夠大,那么就可以讓某個命中結(jié)果永遠(yuǎn)排第一位,這就是百度等網(wǎng)站的"收費(fèi)排名"業(yè)務(wù)。明顯有失公平,鄙視一把。 [no] 

            5. 排序

            通過 SortField 的構(gòu)造參數(shù),我們可以設(shè)置排序字段,排序條件,以及倒排。
            Sort sort = new Sort(new SortField(FieldName, SortField.DOC, false));

            IndexSearcher searcher = new IndexSearcher(reader);
            Hits hits = searcher.Search(query, sort);

            排序?qū)λ阉魉俣扔绊戇€是很大的,盡可能不要使用多個排序條件。

            6. 過濾

            使用 Filter 對搜索結(jié)果進(jìn)行過濾,可以獲得更小范圍內(nèi)更精確的結(jié)果。

            舉個例子,我們搜索上架時間在 2005-10-1 到 2005-10-30 之間的商品。
            對于日期時間,我們需要轉(zhuǎn)換一下才能添加到索引庫,同時還必須是索引字段。
            // index
            document.Add(FieldDate, DateField.DateToString(date), Field.Store.YES, Field.Index.UN_TOKENIZED);

            //...

            // search
            Filter filter = new DateFilter(FieldDate, DateTime.Parse("2005-10-1"), DateTime.Parse("2005-10-30"));
            Hits hits = searcher.Search(query, filter);

            除了日期時間,還可以使用整數(shù)。比如搜索價格在 100 ~ 200 之間的商品。
            Lucene.Net NumberTools 對于數(shù)字進(jìn)行了補(bǔ)位處理,如果需要使用浮點數(shù)可以自己參考源碼進(jìn)行。
            // index
            document.Add(new Field(FieldNumber, NumberTools.LongToString((long)price), Field.Store.YES, Field.Index.UN_TOKENIZED));

            //...

            // search
            Filter filter = new RangeFilter(FieldNumber, NumberTools.LongToString(100L), NumberTools.LongToString(200L), true, true);
            Hits hits = searcher.Search(query, filter);

            使用 Query 作為過濾條件。
            QueryFilter filter = new QueryFilter(QueryParser.Parse("name2", FieldValue, analyzer));

            我們還可以使用 FilteredQuery 進(jìn)行多條件過濾。
            Filter filter = new DateFilter(FieldDate, DateTime.Parse("2005-10-10"), DateTime.Parse("2005-10-15"));
            Filter filter2 = new RangeFilter(FieldNumber, NumberTools.LongToString(11L), NumberTools.LongToString(13L), true, true);

            Query query = QueryParser.Parse("name*", FieldName, analyzer);
            query = new FilteredQuery(query, filter);
            query = new FilteredQuery(query, filter2);

            IndexSearcher searcher = new IndexSearcher(reader);
            Hits hits = searcher.Search(query);

            7. 分布搜索

            我們可以使用 MultiReader 或 MultiSearcher 搜索多個索引庫。
            MultiReader reader = new MultiReader(new IndexReader[] { IndexReader.Open(@"c:\index"), IndexReader.Open(@"\\server\index") });
            IndexSearcher searcher = new IndexSearcher(reader);
            Hits hits = searcher.Search(query);


            IndexSearcher searcher1 = new IndexSearcher(reader1);
            IndexSearcher searcher2 = new IndexSearcher(reader2);
            MultiSearcher searcher = new MultiSearcher(new Searchable[] { searcher1, searcher2 });
            Hits hits = searcher.Search(query);

            還可以使用 ParallelMultiSearcher 進(jìn)行多線程并行搜索。

            8. 合并索引庫

            將 directory1 合并到 directory2 中。
            Directory directory1 = FSDirectory.GetDirectory("index1", false);
            Directory directory2 = FSDirectory.GetDirectory("index2", false);

            IndexWriter writer = new IndexWriter(directory2, analyzer, false);
            writer.AddIndexes(new Directory[] { directory });
            Console.WriteLine(writer.DocCount());
            writer.Close();

            9. 顯示搜索語法字符串

            我們組合了很多種搜索條件,或許想看看與其對等的搜索語法串是什么樣的。
            BooleanQuery query = new BooleanQuery();
            query.Add(query1, true, false);
            query.Add(query2, true, false);
            //...

            Console.WriteLine("Syntax: {0}", query.ToString());

            輸出:
            Syntax: +(name:name* value:name*) +number:[0000000000000000b TO 0000000000000000d]

            呵呵,就這么簡單。

            10. 操作索引庫

            刪除 (軟刪除,僅添加了刪除標(biāo)記。調(diào)用 IndexWriter.Optimize() 后真正刪除。)
            IndexReader reader = IndexReader.Open(directory);

            // 刪除指定序號(DocId)的 Document。
            reader.Delete(123);

            // 刪除包含指定 Term 的 Document。
            reader.Delete(new Term(FieldValue, "Hello"));

            // 恢復(fù)軟刪除。
            reader.UndeleteAll();

            reader.Close();

            增量更新 (只需將 create 參數(shù)設(shè)為 false,即可往現(xiàn)有索引庫添加新數(shù)據(jù)。)
            Directory directory = FSDirectory.GetDirectory("index", false);
            IndexWriter writer = new IndexWriter(directory, analyzer, false);
            writer.AddDocument(doc1);
            writer.AddDocument(doc2);
            writer.Optimize();
            writer.Close();

            11. 優(yōu)化

            批量向 FSDirectory 增加索引時,增大合并因子(mergeFactor )和最小文檔合并數(shù)(minMergeDocs)有助于提高性能,減少索引時間。
            IndexWriter writer = new IndexWriter(directory, analyzer, true);

            writer.maxFieldLength = 1000; // 字段最大長度
            writer.mergeFactor = 1000;
            writer.minMergeDocs = 1000;

            for (int i = 0; i < 10000; i++)
            {
              // Add Documentes...
            }

            writer.Optimize();
            writer.Close();

            相關(guān)參數(shù)說明 


            轉(zhuǎn)自《深入 Lucene 索引機(jī)制

            利用 Lucene,在創(chuàng)建索引的工程中你可以充分利用機(jī)器的硬件資源來提高索引的效率。當(dāng)你需要索引大量的文件時,你會注意到索引過程的瓶頸是在往磁盤上寫索引文件的過程中。為了解決這個問題, Lucene 在內(nèi)存中持有一塊緩沖區(qū)。但我們?nèi)绾慰刂?Lucene 的緩沖區(qū)呢?幸運(yùn)的是,Lucene 的類 IndexWriter 提供了三個參數(shù)用來調(diào)整緩沖區(qū)的大小以及往磁盤上寫索引文件的頻率。

            1.合并因子 (mergeFactor)

            這個參數(shù)決定了在 Lucene 的一個索引塊中可以存放多少文檔以及把磁盤上的索引塊合并成一個大的索引塊的頻率。比如,如果合并因子的值是 10,那么當(dāng)內(nèi)存中的文檔數(shù)達(dá)到 10 的時候所有的文檔都必須寫到磁盤上的一個新的索引塊中。并且,如果磁盤上的索引塊的隔數(shù)達(dá)到 10 的話,這 10 個索引塊會被合并成一個新的索引塊。這個參數(shù)的默認(rèn)值是 10,如果需要索引的文檔數(shù)非常多的話這個值將是非常不合適的。對批處理的索引來講,為這個參數(shù)賦一個比較大的值會得到比較好的索引效果。

            2.最小合并文檔數(shù) (minMergeDocs)

            這個參數(shù)也會影響索引的性能。它決定了內(nèi)存中的文檔數(shù)至少達(dá)到多少才能將它們寫回磁盤。這個參數(shù)的默認(rèn)值是10,如果你有足夠的內(nèi)存,那么將這個值盡量設(shè)的比較大一些將會顯著的提高索引性能。

            3.最大合并文檔數(shù) (maxMergeDocs)

            這個參數(shù)決定了一個索引塊中的最大的文檔數(shù)。它的默認(rèn)值是 Integer.MAX_VALUE,將這個參數(shù)設(shè)置為比較大的值可以提高索引效率和檢索速度,由于該參數(shù)的默認(rèn)值是整型的最大值,所以我們一般不需要改動這個參數(shù)。


            -------------------迷糊中的分割線-----------------------------

            Lucene 相關(guān)資源:

            1. Lucene 官方網(wǎng)站
            2. Apache Lucene
            3. Lucene FAQ
            4. Lucene.Net
            5. Lucene API (Java)
            6. DotLucene
            7. Luke - Lucene Index Toolbox
            8. Nutch
            9. LUCENE.COM.CN 中國
            10. Compass
            11. 實戰(zhàn) Lucene,第 1 部分: 初識 Lucene
            12. 深入 Lucene 索引機(jī)制
            posted on 2010-03-21 13:49 baby-fly 閱讀(519) 評論(0)  編輯 收藏 引用 所屬分類: Information Retrival / Data Mining
            久久国产劲爆AV内射—百度| 人人狠狠综合久久亚洲88| 中文字幕久久欲求不满| 亚洲一区中文字幕久久| 久久精品国产精品亚洲下载| 三级片免费观看久久| 性高朝久久久久久久久久| 久久久久亚洲AV无码专区网站| 色偷偷91久久综合噜噜噜噜| 狠狠色丁香婷婷久久综合五月| 人妻精品久久无码区| 精品久久久久久国产免费了| 久久精品国产99国产精品导航| 精品国产91久久久久久久a| 精品久久久无码21p发布| 亚洲国产成人久久综合一 | 久久ZYZ资源站无码中文动漫| 久久精品无码一区二区三区| 久久受www免费人成_看片中文| 国产精品久久久亚洲| 久久久久国产精品三级网| 亚洲AV无码1区2区久久| 久久男人中文字幕资源站| 精品人妻久久久久久888| 亚洲国产二区三区久久| 国产成人久久精品一区二区三区| 99精品伊人久久久大香线蕉| 亚洲AV无码久久精品狠狠爱浪潮| 99久久国产免费福利| 久久亚洲日韩看片无码| 国产视频久久| 91久久九九无码成人网站| 久久香蕉超碰97国产精品| 精品综合久久久久久97| 综合久久给合久久狠狠狠97色 | 亚洲一区中文字幕久久| 国产精品99精品久久免费| 久久国产色AV免费看| 久久99亚洲网美利坚合众国| 久久久国产打桩机| 亚洲国产精品成人久久|