Last active
April 8, 2025 12:56
-
-
Save sitefinitySDK/575b7a7e9d2564ec604468e62b08721d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Lucene.Net.Documents; | |
using Lucene.Net.Index; | |
using Lucene.Net.Search; | |
using Lucene.Net.Search.Function; | |
using System; | |
namespace SitefinityWebApp.Documentation.Samples.Search | |
{ | |
public class SitefinityCustomScoreQuery : CustomScoreQuery | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SitefinityCustomScoreQuery"/> class. | |
/// </summary> | |
/// <param name="subQuery">The Lucene query</param> | |
public SitefinityCustomScoreQuery(Query subQuery) : base(subQuery) | |
{ | |
} | |
/// <inheritdoc /> | |
protected override CustomScoreProvider GetCustomScoreProvider(AtomicReaderContext reader) | |
{ | |
return new CustomizedScoreProvider(reader); | |
} | |
/// <summary> | |
/// The custom score provider for <see cref="SitefinityCustomScoreQuery"/> | |
/// </summary> | |
protected class CustomizedScoreProvider : CustomScoreProvider | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="CustomizedScoreProvider"/> class. | |
/// </summary> | |
/// <param name="reader">The index reader</param> | |
public CustomizedScoreProvider(AtomicReaderContext reader, bool excludeContentAge) : base(reader) | |
{ | |
} | |
/// <inheritdoc /> | |
public override float CustomScore(int doc, float subQueryScore, float[] valSrcScores) | |
{ | |
var baseScore = base.CustomScore(doc, subQueryScore, valSrcScores); | |
double boost = 1; | |
var currentDocument = this.m_context.Reader.Document(doc); | |
var dateFieldValue = currentDocument.Get("LastModified"); | |
if (dateFieldValue != null) | |
{ | |
var now = DateTime.UtcNow; | |
var date = DateTools.StringToDate(dateFieldValue); | |
var age = (int)(now - date).TotalDays; | |
boost = CalculateBoost(age); | |
} | |
var adjustedScore = baseScore * boost; | |
return (float)adjustedScore; | |
} | |
/// <summary> | |
/// Calculates the boost, based on the content's age | |
/// </summary> | |
/// <param name="days">Content's age in days</param> | |
/// <returns>The boost</returns> | |
protected virtual double CalculateBoost(int days) | |
{ | |
double boostFactor = 100; | |
double maxRampFactor = 5; | |
double curveAdjustmentFactor = 5; | |
return Math.Pow(boostFactor / (maxRampFactor + days), 1 / curveAdjustmentFactor); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment