solr 自定义函数 评分函数

solrpackage com.yujz;import java.io.IOException;import java.util.Map;import org.apache.lucene.index.AtomicReaderContext;import org.apache.lucene.queries.function.FunctionValues;import org.apache.lucene.queries.function.ValueSource;import org.apache.lucene.queries.function.docvalues.IntDocValues;import org.apache.lucene.queries.function.valuesource.OrdFieldSource;public class B258DynamicSource extends ValueSource {public ValueSource[] valueSource;public ValueSource fieldSource;public B258DynamicSource(ValueSource[] valueSource, ValueSource fieldSource) {this.valueSource = valueSource;this.fieldSource = fieldSource;}@SuppressWarnings("rawtypes")@Overridepublic FunctionValues getValues(Map context,AtomicReaderContext readerContext) throws IOException {final FunctionValues fieldVals = fieldSource.getValues(context,readerContext);final FunctionValues[] valsArr = new FunctionValues[valueSource.length];System.err.println("valueSource.length-------------" + valueSource.length);for (int i = 0; i < valueSource.length; i++) {valsArr[i] = valueSource[i].getValues(context, readerContext);System.err.println("valsArr[i]");}return new IntDocValues(this) {@Overridepublic int intVal(int doc) {String source = fieldVals.strVal(doc);System.err.println("source------------------" + source);System.err.println("valsArr.length-----" + valsArr.length);System.err.println("doc----" + doc);for (FunctionValues fv : valsArr) {int ss = fv.intVal(doc);System.err.println("args-----" + ss);if (doc > 7) {return 2;}}return 1;}@Overridepublic String toString(int doc) {return name() + '(' + fieldVals.strVal(doc) + ')';}};}@Overridepublic boolean equals(Object o) {return o != null && o.getClass() == OrdFieldSource.class && this.fieldSource.equals(((B258DynamicSource)o).fieldSource);}@Overridepublic int hashCode() {return B258DynamicSource.class.hashCode() + fieldSource.hashCode();}@Overridepublic String description() {return name();}public String name() {return "b258dynamic";}}package com.yujz;import java.util.List;import org.apache.lucene.queries.function.ValueSource;import org.apache.solr.common.SolrException;import org.apache.solr.schema.IntField;import org.apache.solr.schema.SchemaField;import org.apache.solr.search.FunctionQParser;import org.apache.solr.search.SyntaxError;import org.apache.solr.search.ValueSourceParser;public class B258DynamicSourceParser extends ValueSourceParser{public ValueSource parse(FunctionQParser fp) throws SyntaxError {String field = fp.parseArg();ValueSource v1 = getValueSource(fp, field); List sources = fp.parseValueSourceList();return new B258DynamicSource(sources.toArray(new ValueSource[sources.size()]),v1);}public ValueSource getValueSource(FunctionQParser fp, String field) { if (field == null) return null; SchemaField f = fp.getReq().getSchema().getField(field); if (f.getType().getClass() == IntField.class) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't use ms() function on non-numeric legacy date field " + field); } return f.getType().getValueSource(f, fp); }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值