Examine icon indicating copy to clipboard operation
Examine copied to clipboard

How to boost a document during indexing

Open RosenPetrov opened this issue 2 years ago • 2 comments
trafficstars

Hello,

In Umbraco 8 I was able to hook into the Examine's DocumentWriting event and boost document by setting its Boost property. (sorry but didn't check the exact Examine version which is used there)

However this has changed in latest Umbraco 10/11 and the new Examine version used there (I think v3+). There is no Boost property on the Document anymore.

Could you give me some hints about how to set some boost on the Document now?

Kind regards!

RosenPetrov avatar Mar 20 '23 21:03 RosenPetrov

I actually saw this comment in the IIndexableField interface

/// <summary>
/// Returns the field's index-time boost.
/// <para/>
/// Only fields can have an index-time boost, if you want to simulate
/// a "document boost", then you must pre-multiply it across all the
/// relevant fields yourself.
/// <para/>
/// The boost is used to compute the norm factor for the field.  By
/// default, in the <see cref="Search.Similarities.Similarity.ComputeNorm(FieldInvertState)"/> method,
/// the boost value is multiplied by the length normalization factor and then
/// rounded by <see cref="Search.Similarities.DefaultSimilarity.EncodeNormValue(float)"/> before it is stored in the
/// index.  One should attempt to ensure that this product does not overflow
/// the range of that encoding.
/// <para/>
/// It is illegal to return a boost other than 1.0f for a field that is not
/// indexed (<see cref="IIndexableFieldType.IsIndexed"/> is false) or omits normalization values
/// (<see cref="IIndexableFieldType.OmitNorms"/> returns true).
/// </summary>
/// <seealso cref="Search.Similarities.Similarity.ComputeNorm(FieldInvertState)"/>
/// <seealso cref="Search.Similarities.DefaultSimilarity.EncodeNormValue(float)"/>
float Boost { get; }

So what I did is something like this:

  var titleField = e.Document.GetField("title") as TextField;
  if (titleField != null)
           titleField.Boost = boostValue;

Am I in the right direction?

RosenPetrov avatar Mar 22 '23 07:03 RosenPetrov

@RosenPetrov Is it possible to use something like this in your case? https://shazwazza.github.io/Examine/articles/searching.html#boosting

nikcio avatar Jun 22 '23 05:06 nikcio

You cannot do document level boosting with the newer lucene version, in java this has been deprecated but in c# it has been removed

image

This describes all of the different deprecation techniques

https://stackoverflow.com/questions/50952727/ho-to-use-functionscorequery-with-text-fields

In our version of lucene though, there isn't FunctionScoreQuery only CustomScoreQuery. There is a Draft PR to help enable this but is in draft and I haven't reviewed https://github.com/Shazwazza/Examine/pull/338/files

And also this one https://github.com/nzdev/Examine/pull/3

For now, the advise is to boost in your query, or manually add a boost field and specify values specific to the documents you want boosted and then boost based on that custom field.

Shazwazza avatar Jun 14 '24 19:06 Shazwazza