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); }}
solr 自定义函数 评分函数
最新推荐文章于 2024-10-17 10:22:29 发布