如何从我的自定义相似性实现中访问schema.xml中的值

时间:2013-07-03 08:58:41

标签: java solr similarity solr4

我为solr制作了一个非常简单的自定义相似度类。它主要用于测试。

public class StaticNormSimilarity extends DefaultSimilarity {

  private static final Logger LOG = LoggerFactory.getLogger(StaticNormSimilarity.class);
  private float norm = 0.1f;

  public void setNorm(float norm) {
    this.norm = norm;

    //TODO: not working how can I access parameters
    LOG.info("##gyk: StaticNormSimilarity norm = {}", norm);
    System.out.println(String.format("##gyk: StaticNormSimilarity: %f",norm));
  }
  public float getNorm() {
    return this.norm;
  }

  @Override
  public float queryNorm(float sumOfSquaredWeights) {
    return this.norm;
  }

}

在schema.xml中,我执行以下操作:

<fieldType name="text_en" class="solr.TextField" positionIncrementGap="100">
  <similarity class="gyk.solr.similarity.StaticNormSimilarity">
    **HOW CAN I SEE THIS VALUE FROM MY CODE**
    <float name="norm">0.1</float>
  </similarity>
  <analyzer type="index">
  ...
  </analyzer>
  <analyzer type="query">
  ...
  </analyzer>
</fieldType>

  <similarity class="solr.SchemaSimilarityFactory" />

我的问题是如何从代码中看到schema.xml文件中指定的norm值?提前谢谢。

1 个答案:

答案 0 :(得分:3)

检查DFRSimilarityFactory源代码...

schema条目

  <fieldType name="text_dfr" class="solr.TextField">
    <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>
    <similarity class="solr.DFRSimilarityFactory">
      <str name="basicModel">I(F)</str>
      <str name="afterEffect">B</str>
      <str name="normalization">H2</str>
    </similarity>
  </fieldType>

Init方法允许从schema.xml

中检索params
  @Override
  public void init(SolrParams params) {
    super.init(params);
    discountOverlaps = params.getBool("discountOverlaps", true);
    basicModel = parseBasicModel(params.get("basicModel"));
    afterEffect = parseAfterEffect(params.get("afterEffect"));
    normalization = parseNormalization(
        params.get("normalization"), params.get("c"), params.get("mu"), params.get("z"));
  }