利用Boost影响Lucene查询结果的排序

前提:不对结果做sort操作.

在搜索中,并不是所有的Document和Fields都是平等的.有些技术会要求到对其Doucment或者Fields的权值改变,默认值为:1.0F,以上需求都是通过改变Document的boost因子来改变的.下面是通过lucene3.0,IKAnalyzer

1.通过设置docboost改变排序结果

 
/**
     * 设置DOC boost 值影响查询排序结果
     * @throws Exception
     */
    public void testBoost1() throws Exception{
        System.out.println("设置DOC boost 值影响查询排序结果");
        RAMDirectory ramDir = new RAMDirectory();
        Analyzer analyzer = new IKAnalyzer();
        IndexWriter iw = new IndexWriter(ramDir, analyzer, true ,IndexWriter.MaxFieldLength.LIMITED);
        
        String[] nameList = { "you are my friend", "a are my wife", "I love you" };
        String[] addList = { "b", "you are my wife", "c" };
        String[] fileList = { "1", "2", "3" };

        for (int i = 0; i < nameList.length; i++){
            Document doc = new Document();
            doc.add(new Field("name", nameList[i], Field.Store.YES, Field.Index.ANALYZED));
            doc.add(new Field("file", fileList[i], Field.Store.YES, Field.Index.ANALYZED));
            doc.add(new Field("address", addList[i], Field.Store.YES, Field.Index.ANALYZED));
            if (i == 2) {
                doc.setBoost(2.0f); 
            }
//            这里设置了第三个文档优先级最高,所以在搜索出来的结果中,该文档排在最前
            iw.addDocument(doc);
        }
        iw.close();

        IndexSearcher _searcher = new IndexSearcher(ramDir);
        String[] fields =new String[]{"name","address"};
        Query query=IKQueryParser.parseMultiField(fields, "you");

        TopDocs topDocs = _searcher.search(query,_searcher.maxDoc());
        ScoreDoc[] hits = topDocs.scoreDocs;
        for (int i = 0; i < hits.length; i++) {
            Document doc = _searcher.doc(hits[i].doc);
            System.out.println("name:"+doc.get("name"));
            System.out.println("file:"+doc.get("file"));
        }
        _searcher.close();
        
    }

if(i==2){doc.setBoost(2.0f);}这样Iloveyou将先输出,

2.通过设置query影响排序

/**
     * 设置query boost值影响排序结果,如果有排序sort,则完全按照sort结果进行
     * @throws Exception
     */
    public void testBoost2() throws Exception{
        System.out.println("设置query boost值影响排序结果");
        RAMDirectory ramDir = new RAMDirectory();
        Analyzer analyzer = new IKAnalyzer();
        IndexWriter iw = new IndexWriter(ramDir, analyzer, true ,IndexWriter.MaxFieldLength.LIMITED);
        
        String[] nameList = { "you are my friend", "a are my wife", "I love you" };
        String[] addList = { "b", "you are my wife", "c" };
        String[] fileList = { "1", "2", "3" };

        for (int i = 0; i < nameList.length; i++)
        {
            Document doc = new Document();
            doc.add(new Field("name", nameList[i], Field.Store.YES, Field.Index.ANALYZED));
            doc.add(new Field("file", fileList[i], Field.Store.YES, Field.Index.ANALYZED));
            doc.add(new Field("address", addList[i], Field.Store.YES, Field.Index.ANALYZED));
            iw.addDocument(doc);
        }
        iw.close();
        IndexSearcher _searcher = new IndexSearcher(ramDir);
        
        BooleanQuery bq = new BooleanQuery();
        QueryParser _parser = new QueryParser(Version.LUCENE_30,"name",analyzer);
        Query  _query = _parser.parse("you");
        _query.setBoost(2f);
        
        QueryParser _parser1 = new QueryParser(Version.LUCENE_30,"address",analyzer);
        Query  _query1 = _parser1.parse("you");
        _query1.setBoost(1f);
       
        bq.add(_query, BooleanClause.Occur.SHOULD);
        bq.add(_query1, BooleanClause.Occur.SHOULD);
//       
        
//          for(int i=0;i<2;i++){
//              QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30,new String[] {"name", "address" }, analyzer);
//              Query q1 = parser.parse("you");
//              bq.add(q1, BooleanClause.Occur.MUST);
//          }
//         
//         SortField[] sortFields = new SortField[1];  
//         SortField sortField = new SortField("file", SortField.INT, true);//false升序,true降序  
//         sortFields[0] = sortField;  
//         Sort sort = new Sort(sortFields);  
//         TopDocs topDocs = _searcher.search(bq,null,_searcher.maxDoc(),sort);
//        
        
        TopDocs topDocs = _searcher.search(bq,_searcher.maxDoc());
        ScoreDoc[] hits = topDocs.scoreDocs;
        for (int i = 0; i < hits.length; i++) {
            Document doc = _searcher.doc(hits[i].doc);
            System.out.println("name:"+doc.get("name"));
            System.out.println("file:"+doc.get("file"));
        }
        _searcher.close();
        
    }

结果如下:(name的boost最高,所以name优先于address排序在前面)

设置queryboost值影响排序结果

name:youaremyfriend

file:1

name:Iloveyou

file:3

name:aaremywife

file:2

3.通过设置fields的boost影响排序

/**
     * 设置field boost 值影响查询排序结果,有排序则按照排序
     * @throws Exception
     */
     //没设置field boost 213 设置后是132
    public void testBoost3() throws Exception{
        System.out.println("设置fields boost 值影响查询排序结果");
        RAMDirectory ramDir = new RAMDirectory();
        Analyzer analyzer = new IKAnalyzer();
        IndexWriter iw = new IndexWriter(ramDir, analyzer, true ,IndexWriter.MaxFieldLength.LIMITED);
        String[] nameList = { "you are my friend", "a are my wife", "I love you" };
        String[] addList = { "b", "you are my wife", "c" };
        String[] fileList = { "1", "2", "3" };

        for (int i = 0; i < nameList.length; i++)
        {
            Document doc = new Document();
            Field nameField =  new Field("name", nameList[i], Field.Store.YES, Field.Index.ANALYZED);
            nameField.setBoost(20f);
            doc.add(nameField);
            doc.add(new Field("file", fileList[i], Field.Store.YES, Field.Index.ANALYZED));
            Field f = new Field("address", addList[i], Field.Store.YES, Field.Index.ANALYZED);
            f.setBoost(30f);
            doc.add(f);
            iw.addDocument(doc);
        }
        iw.close();
        
       
        IndexSearcher _searcher = new IndexSearcher(ramDir);
        String[] fields =new String[]{"name","file","address"};
        Query query=IKQueryParser.parseMultiField(fields, "you");
        
//        SortField[] sortFields = new SortField[1];  
//        SortField sortField = new SortField("file", SortField.INT, true);//false升序,true降序  
//        sortFields[0] = sortField;  
//        Sort sort = new Sort(sortFields);  
//        TopDocs topDocs = _searcher.search(query,null,_searcher.maxDoc(),sort);
        
        TopDocs topDocs = _searcher.search(query,_searcher.maxDoc());
        ScoreDoc[] hits = topDocs.scoreDocs;
        for (int i = 0; i < hits.length; i++) {
            Document doc = _searcher.doc(hits[i].doc);
            System.out.println("name:"+doc.get("name"));
            System.out.println("file:"+doc.get("file"));
        }
        _searcher.close();
        
    }

结果如下:(address的boost最高,先排在前面了)

设置fieldsboost值影响查询排序结果

name:aaremywife

file:2

name:youaremyfriend

file:1

name:Iloveyou

file:3

相关推荐