利用Boost影响Lucene查询结果的排序
以下代码在Lucene2.1下通过,主要是通过设置Document的Boost来影响文档的权重,以达到控制查询结果顺序的目的(前提是不利用Sort排序的情况下):
 private void btnSearch_Click(object sender, EventArgs e)
private void btnSearch_Click(object sender, EventArgs e) {
        { RAMDirectory ramDir = new RAMDirectory();
            RAMDirectory ramDir = new RAMDirectory(); IndexWriter iw = new IndexWriter(ramDir,new StandardAnalyzer(),true);
            IndexWriter iw = new IndexWriter(ramDir,new StandardAnalyzer(),true);
 string[] NameList = { "you are my friend", "you are my wife", "I love you" };
            string[] NameList = { "you are my friend", "you are my wife", "I love you" }; string[] FileList = { "1", "2", "3" };
            string[] FileList = { "1", "2", "3" };
 for (int i = 0; i < NameList.Length; i++)
            for (int i = 0; i < NameList.Length; i++) {
            { Document doc = new Document();
                Document doc = new Document(); doc.Add(new Field("name", NameList[i], Field.Store.YES, Field.Index.TOKENIZED));
                doc.Add(new Field("name", NameList[i], Field.Store.YES, Field.Index.TOKENIZED)); doc.Add(new Field("file", FileList[i], Field.Store.YES, Field.Index.TOKENIZED));
                doc.Add(new Field("file", FileList[i], Field.Store.YES, Field.Index.TOKENIZED));
 if (i == 2) { doc.SetBoost(2.0f); }//这里设置了第三个文档优先级最高,所以在搜索出来的结果中,该文档排在最前
                if (i == 2) { doc.SetBoost(2.0f); }//这里设置了第三个文档优先级最高,所以在搜索出来的结果中,该文档排在最前 iw.AddDocument(doc);
                iw.AddDocument(doc); }
            } iw.Close();
            iw.Close();
 IndexSearcher _searcher = new IndexSearcher(ramDir);
            IndexSearcher _searcher = new IndexSearcher(ramDir); QueryParser _parser = new QueryParser("name",new StandardAnalyzer());
            QueryParser _parser = new QueryParser("name",new StandardAnalyzer()); Query  _query = _parser.Parse("you");
            Query  _query = _parser.Parse("you"); Hits hitDoc = _searcher.Search(_query);
            Hits hitDoc = _searcher.Search(_query);
 lstResult.Items.Clear();//lstResult为一个ListBox
            lstResult.Items.Clear();//lstResult为一个ListBox
 for (int i = 0; i < hitDoc.Length(); i++)
            for (int i = 0; i < hitDoc.Length(); i++) {
            { Document doc = hitDoc.Doc(i);
                Document doc = hitDoc.Doc(i); lstResult.Items.Add(doc.Get("file") + " " + doc.Get("name"));
                lstResult.Items.Add(doc.Get("file") + " " + doc.Get("name")); }
            }             _searcher.Close();
            _searcher.Close(); }
        } 
 
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 
                    
                     
                    
                 
                    
                 
        
 
             
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号