【Lucene】Lucene通过CustomScoreQuery实现自定义评分
2016-06-12 09:11
615 查看
建立业务查询的query,该query嵌套在自定义评分CustomScoreQuery中,从而为query添加了自定义评分功能
从上一步可以看出, 我们需要新建一个ProductCustomScoreQuery类,该类继承CustomScoreQuery类,并重写getCustomScoreProvider()方法,该方法返回一个CustomScoreProvider对象,该对象是最终实现自定义评分功能的对象
从上一步可以看出,我们还需要新建一个ProductCustomScoreProvider类, 该类继承CustomScoreProvider类,并重写customScore()方法,该方法是实现自定义评分的核心方法。
本文出自 “DeaconLi” 博客,请务必保留此出处http://lizhuquan0769.blog.51cto.com/2591147/1788065
Query query = new TermQuery(new Term("name", "myname")); query = new ProductCustomScoreQuery(query);
从上一步可以看出, 我们需要新建一个ProductCustomScoreQuery类,该类继承CustomScoreQuery类,并重写getCustomScoreProvider()方法,该方法返回一个CustomScoreProvider对象,该对象是最终实现自定义评分功能的对象
public class ProductCustomScoreQuery extends CustomScoreQuery { public ProductCustomScoreQuery(Query subQuery) { super(subQuery); } @Override protected CustomScoreProvider getCustomScoreProvider(AtomicReaderContext context) throws IOException { return new ProductCustomScoreProvider(context); } }
从上一步可以看出,我们还需要新建一个ProductCustomScoreProvider类, 该类继承CustomScoreProvider类,并重写customScore()方法,该方法是实现自定义评分的核心方法。
public class ProductCustomScoreProvider extends CustomScoreProvider { public ProductCustomScoreProvider(AtomicReaderContext context) { super(context); } @Override public float customScore(int doc, float subQueryScore, float valSrcScore) throws IOException { //获取Document的ProductCode BytesRef br = new BytesRef(); FieldCache.DEFAULT.getTerms(this.context.reader(), "ProductCode", false).get(doc, br); String productCode = br.utf8ToString(); //文档在原始评分的基础上, 再乘以productCode的长度, 实现自定义评分 return productCode.length * super.customScore(doc, subQueryScore, valSrcScore); } }
本文出自 “DeaconLi” 博客,请务必保留此出处http://lizhuquan0769.blog.51cto.com/2591147/1788065
相关文章推荐
- Android UI Libs之ExplosionField
- leecode_300 Longest Increasing Subsequence
- LeetCode--No.347--Top K Frequent Elements
- MessageQueue&Message Pool—消息的读写、删除和回收
- Android UI(二)
- easyUI,radio控制select
- easyUI,select控制tr列的radio显示
- 获取设备类型--作UI布局
- UIImageView赋值习惯错误
- 在C#中使用装饰器模式和扩展方法实现Fluent Interface
- Fluent interface(维基百科)
- 如何在自己的项目中实现 Fluent Interface(流畅接口)
- iOS UIBezeirPath理解 欢迎指点
- UIButton多参数传值实现
- String & CharSequence
- HIGHGUI ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
- java GUI
- string &stringBuilder & stringBuffer
- LeetCode-128.Longest Consecutive Sequence
- JAVA UUID