【一】自定义评分

CustomScoreQuery类
 
构造函数
 
Constructor Summary
CustomScoreQuery(Query subQuery) 
          Create a CustomScoreQuery over input subQuery.
CustomScoreQuery(Query subQuery, Query... scoringQueries) 
          Create a CustomScoreQuery over input subQuery and a FunctionQuery.
CustomScoreQuery(Query subQuery, Query scoringQuery) 
          Create a CustomScoreQuery over input subQuery and a FunctionQuery.
 
FunctionQuery,FunctionQuery(ValueSource valueSource),自定义评分query;
ValueSource,读取某个域的评分,ValueSource valueSource = new IntFieldSource("score");读取Score域的评分;
 
通过重写getCustomScoreProvider(AtomicReaderContext)修改评分计算方法
getCustomScoreProvider默认计算方法
super.getCustomScoreProvider(reader):原有评分*传入评分域评分
 
CustomScoreProvider类
 
重写 customScore(int doc, float subQueryScore, float valSrcScore)方法,
  1. doc是文件编号
  2. subQueryScore是原有评分值
  3. valSrcScore是传入域的评分值
在这个方法里,可以编写自己的算法,操作两个评分值
 
 

1、原有评分*传入的评分域打分

 
 
 1 Query subquery = new TermQuery(new Term("content", "java"));
 2  //评分域
 3 ValueSource valueSource = new IntFieldSource("score");
 4 FunctionQuery fq = new FunctionQuery(valueSource );//自定义评分query
 5 //自定义评分query
 6 MyCustomScoreQuery myCustomScoreQuery = new MyCustomScoreQuery(subquery ,fq );
 7 
 8 private class MyCustomScoreQuery extends CustomScoreQuery {
 9 
10               public MyCustomScoreQuery(Query subQuery , FunctionQuery fq) {
11                      super(subQuery ,fq );
12                      // TODO Auto-generated constructor stub
13              }
14              
15               protected CustomScoreProvider getCustomScoreProvider(AtomicReaderContext reader ) throws IOException {
16                      return super .getCustomScoreProvider(reader);
17              }
18        }

 

2、原有评分/传入评分域打分
 
 1 private class MyCustomScoreQuery extends CustomScoreQuery {
 2  
 3      public MyCustomScoreQuery(Query subQuery , FunctionQuery fq) {
 4                      super(subQuery ,fq );
 5                      // TODO Auto-generated constructor stub
 6              }
 7              
 8               protected CustomScoreProvider getCustomScoreProvider(AtomicReaderContext reader ) throws IOException {
 9                      //return super.getCustomScoreProvider(reader);
10                      return new MyCustomScoreProvider(reader);
11              }
12        }
13        
14   private class MyCustomScoreProvider extends CustomScoreProvider {
15        public MyCustomScoreProvider(AtomicReaderContext reader){
16                      super(reader );
17              }
18              
19               public float customScore(int doc, float subQueryScore, float valSrcScore) throws IOException{
20                      //return super.customScore(doc, subQueryScore, valSrcScore);
21                      return subQueryScore /valSrcScore;
22              }
23        }

 

 
3、根据文件名评分,指定的特殊文件名获得更高的评分比重
 
[1]从reader的缓存中获得文件名
Lucene4.0前,Sring[] filenames = FieldCache. DEFAULT.getString(  "filename" );
Lucene4.0后,没有了getString,getByte也变成了 Deprecate。
 
所以采用了下面的方法获取StringField
Binaryvalues filenames = FieldCache.DEFAULT.getTerms( atomicReader, "filename" , false);
 
[2]针对每个文件名进行操作
String filename = filenames.get(doc ).utf8ToString();
posted @ 2015-07-15 16:39  FatSheeeeep  阅读(121)  评论(0编辑  收藏  举报