【一】自定义评分

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();

转载于:https://www.cnblogs.com/Sheeeeep/p/4648792.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值