lucenenet-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Diehl <mdi...@lexprompt.com.INVALID>
Subject Re: Weighting results by 'freshness'
Date Wed, 05 Apr 2017 19:51:51 GMT
Hi Michael,

I probably didn't do it the best way, but we created a custom scorer for
this (this was on the older v3 Lucene).
This was based on:

    // https://blog.imaginea.com/custom-scoring-custom-score-query-and-custom-score-provider/



Query customScorerQuery = new MyCustomScoreQuery( q );


public class MyCustomScoreQuery : Lucene.Net.Search.Function.CustomScoreQuery
{
    public MyCustomScoreQuery( Query q )
        : base( q )
    {
    }

    protected override CustomScoreProvider GetCustomScoreProvider(
IndexReader reader )
    {
        return new MyCustomScoreProvider( reader );
    }
}

public class MyCustomScoreProvider : CustomScoreProvider
{
    // https://blog.imaginea.com/custom-scoring-custom-score-query-and-custom-score-provider/
    private const float BoostLastMonth = 2.0f;
    private const float BoostLastSixMonths = 1.5f;

    // Date format: 20160828 == August 28, 2016

     private static readonly double DateLastMonth =

        double.Parse( DateTools.DateToString( DateTime.Now.AddMonths( -1 ),
DateTools.Resolution.DAY ) );     private static readonly double
DateLast6Month =
        double.Parse( DateTools.DateToString( DateTime.Now.AddMonths( -6 ),
DateTools.Resolution.DAY ) );

    public MyCustomScoreProvider( IndexReader reader )

        : base( reader )
    {
        ;
    }

    public override float CustomScore( int docId, float subQueryScore,
float[] valSrcScores )
    {
        Document doc = reader.Document( docId );
        string[] docModifiedDate = doc.GetValues( LuceneField.ModifiedDate );
        if ( docModifiedDate.Length < 1 )
            return subQueryScore;

        var docDate = double.Parse( docModifiedDate[0] );
        // Boost based on date
        /*             * Within past month: 2.0             * Within
past 6 months: 1.5             */
        float dateBoost =
                      docDate > DateLastMonth
                        ? BoostLastMonth
                        : docDate > DateLast6Month
                            ? BoostLastSixMonths
                            : 1.0f;

        return subQueryScore * dateBoost;
    }
}


On Wed, Apr 5, 2017 at 11:39 AM, Michael Newton <michael@mavnn.co.uk> wrote:

> Hi,
> I've managed to build a nice little Lucene.net back end with 4.0.8 and the
> free text side of things is working really well. However, I'd like to add a
> weighting to the queries supplied that means that newer documents are
> considered more highly (but not to outright sort by date).
>
> I have a "created" field on all of my documents, which has been populated
> using "DateTools.DateToString" with a resolution of day, but I'm uncertain
> how to add a query which add weight to results based on "the higher the
> value of field 'x', the better".
>
> What would be the best way to go about this?
>
> My current query code (in F#) looks like this:
>
>     let parser =
>         QueryParsers.Classic.MultiFieldQueryParser
>             (Util.LuceneVersion.LUCENE_48, [| "title"; "content" |],
>              context.Analyzer)
>     let query = parser.Parse phrase
>
> Many thanks,
>
> Michael
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message