通过重写QueryParser实现
1,继承CustomScoreProvider类,重写customScore方法,从DocValues中动态读取评分有关因子,计算后,影响总评分
2,继承CustomScoreQuery类,重写getCustomScoreProvider方法,需要用到1,返回自定义的CustomScoreProvider类
到此,在lucene中就完事了,但是在solr中我们还需要继续
3,继承QParser类,重写parse方法,需要用到2,并在构造方法中,完成一些必须的初始化操作
4,继承QParserPlugin类,重写createParser方法,需要用到3,至此,代码完成
5,在solrconfig.xml中,注册我们写的插件:
<queryParser name=
"myqp"
class
=
"com.easy.custom.queryparser.MyQueryParserPlugin"/
>
6, 重启solr,或者reload指定的core
插件调用入口
public class
TestParserPlugin
extends
QParserPlugin {
public
QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
return new
TestParser(qstr, localParams, params, req);
}
}
参数解析,结果过滤
public class
TestParser
extends
ExtendedDismaxQParser {
public
Query parse()
throws
SyntaxError {
Query query =
super
.parse();
return new
TestScoreQuery(query);
}
public
TestParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
super
(qstr, localParams, params, req);
}
}
query解析
public class
TestScoreQuery
extends
CustomScoreQuery {
public
TestScoreQuery(Query subQuery) {
super
(subQuery);
}
@Override
public
CustomScoreProvider getCustomScoreProvider(LeafReaderContext context){
return new
TestScoreProvider(context);
}
}
评分修改
public class
TestScoreProvider
extends
CustomScoreProvider {
public
TestScoreProvider(LeafReaderContext context){
super
(context);
}
}