SOLR自定义查询函数抛出错误

时间:2018-06-11 11:45:46

标签: solr lucene solrj

我试图按升序排序几个文件。在我的doc中有一个名为“price”的字段,我希望根据该字段进行排序。 我正在使用自定义solr函数策略。

SOLR排序字段命令: mySuggestRankingFunc(price)desc

mySuggestRankingFunc (价格):这是solr自定义函数,它将占用一个字段(这里我用价格调用)并返回该字段的值(暂时保持简单的实施)

price:我想要函数返回的值的字段。(FLOAT)

//我的ValueSourceParser实现

public class MyValueSourceParser extends ValueSourceParser {
    public void init(NamedList namedList) {
    }
    public ValueSource parse(FunctionQParser fqp) throws SyntaxError {
        // Trying to get the field 
         String field = fqp.parseArg(); 
         // Passing the field to my custom ValueSource class
         return new PriceFieldSource(field);
    }
}

// PriceFieldSource Implementation

import org.apache.log4j.Logger;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.search.IndexSearcher;

import java.io.IOException;
import java.util.Map;

public class PriceFieldSource extends ValueSource {
    Logger logger = Logger.getLogger(PriceFieldSource.class);

    final ValueSource source;
    final String field;
    public PriceFieldSource(ValueSource source, String field) {
        this.source = source;
        this.field = field;
    }
    public FunctionValues getValues(Map context, LeafReaderContext reader) throws IOException {

        return new FunctionValues() {
            public float floatVal(int doc) {
                return 1.0f; 
            // Want to return the value of price field from here.
            }
            public int intVal(int doc) {
                return (int)floatVal(doc);
            }
            public long longVal(int doc) {
                return (long)floatVal(doc);
            }
            public double doubleVal(int doc) {
                return (double)floatVal(doc);
            }
            public String strVal(int doc) {
                return Float.toString(floatVal(doc));
            }
            public String toString(int doc) {
                return description();
            }
        };
    }

    @Override
    public boolean equals(Object o) {
        return false;
    }

    @Override
    public int hashCode() {
        return 0;
    }

    @Override
    public String description() {
        return "price";
    }

    @Override
    public void createWeight(Map context, IndexSearcher searcher) throws IOException {
       super.createWeight(context, searcher);
    }
}

solr的错误:

{   "responseHeader":{
    "status":400,
    "QTime":1,
    "params":{
      "q":"*:*",
      "sort":"mySuggestRankingFunc(price) desc",
      "_":"1528698673642"}},   "error":{
    "metadata":[
      "error-class","org.apache.solr.common.SolrException",
      "root-error-class","org.apache.solr.search.SyntaxError"],
    "msg":"sort param could not be parsed as a query, and is not a field that exists in the index: mySuggestRankingFunc(price)",
    "code":400}}

请帮助!!

0 个答案:

没有答案