生活随笔
收集整理的這篇文章主要介紹了
lucene 自定义评分
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘自:http://blog.csdn.net/seven_zhao/article/details/42708953
1.基于FunctionQuery,(1)創建類并繼承ValueSource;(2)重寫getValues方法。
1 private class MyScore2
extends ValueSource{
2
3 @Override
4 public FunctionValues getValues(Map context,
5 final AtomicReaderContext readerContext)
throws IOException {
6 return new FloatDocValues(
this); }
7
8 @Override
9 public float floatVal(
int doc) {
10 float s = 1
;
11 //從域緩存中讀取數據
12 // 從域緩存中加載索引字段信息
13 try {
14 Longs longs= FieldCache.DEFAULT.getLongs(readerContext.reader(), "size",
false);
15 long size =
longs.get(doc);
16 if(size>1000
){
17 s = 0.5f
;
18 }
19 }
catch (IOException e) {
20 // TODO Auto-generated catch block
21 e.printStackTrace();
22 }
23 return s;
24 }
25 }
26 }
2.(1)創建類并繼承CustomScoreQuery
? ? (2)覆蓋重寫類中的getCusomScoreProvider方法
? ?(3)創建類并繼承CustomScoreProvider
? ?(4)覆蓋重寫類中的customScore確定新的評分規則
public class MySelfScore {public void searchBySelfScore(){try{IndexSearcher search =
new IndexSearcher(DirectoryReader.open(FileIndexUtils.getDirectory()));Query q =
new TermQuery(
new Term("content","java"
));MyCustomScoreQuery myQuery =
new MyCustomScoreQuery(q);TopDocs tds = search.search(myQuery, 200
);SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"
);for(ScoreDoc sd:tds.scoreDocs){Document d =
search.doc(sd.doc);System.out.println(sd.doc+":("+sd.score+")" +"["+d.get("filename")+"【"+d.get("path")+"】--->"+
d.get("size")+"-----"+sdf.format(
new Date(Long.valueOf(d.get("date"))))+"]"
);}System.out.println("-----------Total result:"+
tds.scoreDocs.length);}catch(Exception e){e.printStackTrace();}}/***重寫評分的實現方式* **/private class MyScoreProvider
extends CustomScoreProvider{private AtomicReaderContext context;public MyScoreProvider(AtomicReaderContext context) {super(context);this.context =
context;}/**重寫評分方法,假定需求為文檔size大于1000的評分/1000**/@Overridepublic float customScore(
int doc,
float subQueryScore,
float valSrcScore)throws IOException {// 從域緩存中加載索引字段信息Longs longs= FieldCache.DEFAULT.getLongs(context.reader(), "size",
false);/**注冊使用自定義的評分實現方式**/// 1. 域值要單一,對于string類型不能分詞(NOT_ANALYZED) // 2. 該域需要建入索引(INDEXED) // 3. 支持的數據類型,byte/short/int/long/float/double// weights.get(doc).utf8ToString();獲取string值BinaryDocValues weights = FieldCache.DEFAULT.getTerms(context.reader(), "title",
true);if(weights.get(doc).utf8ToString().equals("1"
)){System.out.println(doc+" : "+
weights.get(doc).utf8ToString());System.out.println(context.reader().document(doc).get("author"
));return subQueryScore * valSrcScore*15
;}/* * 通過得分相乘放大分數 * 此處可以控制與原有得分結合的方式,加減乘除都可以 * **/return subQueryScore*valSrcScore
;}}/*** 重寫CustomScoreQuery 的getCustomScoreProvider方法 * 引用自定義的Provider */private class MyCustomScoreQuery
extends CustomScoreQuery{public MyCustomScoreQuery(Query subQuery) {super(subQuery);}@Overrideprotected CustomScoreProvider getCustomScoreProvider(AtomicReaderContext context) throws IOException {return new MyScoreProvider(context);}}
} ?
轉載于:https://www.cnblogs.com/qingyibusi/p/6170397.html
總結
以上是生活随笔為你收集整理的lucene 自定义评分的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。