solr4.0扩展payload搜索

1.重写自己的Similarity 或者修改org.apache.lucene.search.similarities.DefaultSimilarity,重写 scorePayload方法,这样生成索引时payload分数才会生效了,否则永远是1(默认值),如果重写自己的similarity,需要在 org.apache.solr.schema.IndexSchema中当node==null时修改设置自己的similarity

Node node = (Node) xpath.evaluate("/schema/similarity", document, XPathConstants.NODE);
    if (node==null) {
      similarityFactory = new SimilarityFactory() {
        @Override
        public Similarity getSimilarity() {
          return Similarity.getDefault();
        }
      };
      log.debug("using default similarity");
    } else {
      final Object obj = loader.newInstance(((Element) node).getAttribute("class"));
      if (obj instanceof SimilarityFactory) {
        // configure a factory, get a similarity back
        SolrParams params = SolrParams.toSolrParams(DOMUtil.childNodesToNamedList(node));
        similarityFactory = (SimilarityFactory)obj;
        similarityFactory.init(params);
      } else {
        // just like always, assume it's a Similarlity and get a ClassCastException - reasonable error handling
        similarityFactory = new SimilarityFactory() {
          @Override
          public Similarity getSimilarity() {
            return (Similarity) obj;
          }
        };
      }
      if (similarityFactory instanceof SchemaAware){
        schemaAware.add((SchemaAware) similarityFactory);
      }
      log.debug("using similarity factory" + similarityFactory.getClass().getName());
    }

 

或者在schema中设置自己的similarity

<similarity class="com.example.solr.CustomSimilarityFactory"> </similarity>

 @Override
  public float scorePayload(int doc, int start, int end, BytesRef payload) {
    return PayloadHelper.decodeFloat(payload.bytes,payload.offset);
  }

2.扩展payload的搜索插件。

 

package org.suishi.solr;


import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.payloads.AveragePayloadFunction;
import org.apache.lucene.search.payloads.MaxPayloadFunction;
import org.apache.lucene.search.payloads.MinPayloadFunction;
import org.apache.lucene.search.payloads.PayloadFunction;
import org.apache.lucene.search.payloads.PayloadTermQuery;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QParserPlugin;
import org.apache.solr.search.QueryParsing;


public class PayloadTermQueryPlugin extends QParserPlugin {
@Override
public void init(NamedList args) {
}
@Override
public QParser createParser(String qstr, SolrParams localParams,
SolrParams params, SolrQueryRequest req) {
return new QParser(qstr, localParams, params, req) {
public Query parse() throws ParseException {
return new PayloadTermQuery(new Term(
localParams.get(QueryParsing.F),
localParams.get(QueryParsing.V)),
createPayloadFunction(localParams.get("func")), false);
}
};
}
private PayloadFunction createPayloadFunction(String func) {
// TODO: refactor so that payload functions are registered as plugins
// and loaded
// through SolrResourceLoader.


PayloadFunction payloadFunction = null;
if ("min".equals(func)) {
payloadFunction = new MinPayloadFunction();
} else if ("avg".equals(func)) {
payloadFunction = new AveragePayloadFunction();
} else if ("max".equals(func)) {
payloadFunction = new MaxPayloadFunction();
}
if (payloadFunction == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"unknown PayloadFunction: " + func);
}
return payloadFunction;
}
}

3. 在solrconfig配置文件中添加2中的插件。

 

<queryParsername="payload"class="org.suishi.solr.PayloadTermQueryPlugin"/>

4.payload特用的查询语句。

 

http://localhost:8983/solr/select?q={!payload%20f=payloadfield%20func=avg}searchvalue&debugQuery=true

http://blog.csdn.net/july_2/article/details/7951188

你可能感兴趣的:(Solr)