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
return super.tf(freq);
}
}
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
To:
[email protected]Subject: Using different field when overriding computeNorm
Hi,
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:
@Override
public float computeNorm(String field, FieldInvertState state) {
final int numTerms;
if (discountOverlaps)
numTerms = state.getLength() - state.getNumOverlap();
else
numTerms = state.getLength();
return state.getBoost() * ((float) (1.0 / Math.sqrt(numTerms)));
}
any ideas how to do that?
Thanks,
Tsvika
---------------------------------------------------------------------
To unsubscribe, e-mail:
[email protected]For additional commands, e-mail:
[email protected]---------------------------------------------------------------------
To unsubscribe, e-mail:
[email protected]For additional commands, e-mail:
[email protected]