lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ryan Aylward <>
Subject RE: Using different field when overriding computeNorm
Date Tue, 01 Feb 2011 18:51:27 GMT
I have had to do similar things to other methods of Similarity. In my example, I wanted to
have different behavior for the tf() method for each field. The tf method does not include
a field parameter as an input to it. The only solution I could come up with was to add a thread
local to set the field and then check the thread local within the tf function. Here's the
tf function...

	public float tf(float freq) {

		// Get the value of the thread local...
		String field = FieldThreadLocal.getField();

		if ("fieldA".equals(field)) {
			// always return 1 for field A
			return 1;
		} else {
			// otherwise, use the normal tf function

tf() is used during scoring so I had to override the TermQuery (and TermWeight and TermScorer)
to be able to set and clear the thread local at the appropriate times. This is a pretty ugly
hack, but I couldn't find another way to make this work.

computeNorm() is calculated at index creation time but you try to do something similar.

Would be curious if other people had a better suggestion as to how to do this.

-----Original Message-----
From: Tsvika Rabkin [] 
Sent: Tuesday, February 01, 2011 5:27 AM
Subject: Using different field when overriding computeNorm


I would like to override default similarity's computeNorm to work with
a different field, other than the query field.

Here is the DefaultSimilarity implementation:

  public float computeNorm(String field, FieldInvertState state) {
    final int numTerms;
    if (discountOverlaps)
      numTerms = state.getLength() - state.getNumOverlap();
      numTerms = state.getLength();
    return state.getBoost() * ((float) (1.0 / Math.sqrt(numTerms)));

any ideas how to do that?



To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message