2

我为 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" />

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

4

1 回答 1

3

检查DFRSimilarityFactory源代码...

对于模式条目

  <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 中检索参数

  @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"));
  }
于 2013-07-03T09:23:43.517 回答