You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@lucenenet.apache.org by Michael Newton <mi...@mavnn.co.uk> on 2017/04/05 17:39:38 UTC

Weighting results by 'freshness'

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

Re: Weighting results by 'freshness'

Posted by Michael Newton <mi...@mavnn.co.uk>.
Thanks for the reply Itamar (and Matt and Alex!),
I did actually discover the FunctionQuery and added an "OrdFieldSource"; it
ended up looking something like this (in F#):

    let mostRecent =
        Queries.Function.ValueSources.OrdFieldSource("created")
        |> Queries.Function.FunctionQuery
    mostRecent.Boost <- 0.5f
    let parser =
        QueryParsers.Classic.MultiFieldQueryParser
            (Util.LuceneVersion.LUCENE_48, [| "title"; "content" |],
             context.Analyzer)
    let parsedQuery = parser.Parse phrase
    parsedQuery.Boost <- 10.f
    let query = BooleanQuery()
    query.Add(parsedQuery, BooleanClause.Occur.MUST)
    query.Add(mostRecent, BooleanClause.Occur.MUST)

My only issue so far is that multiterm queries sometimes mess with the
respective weightings, meaning that "freshness" can end up having a very
variable effect on the ordering depending on the query supplied. I'm
willing to live with this as that mostly seems to mean that more specific
queries are less weighed by freshness, which feels reasonable.

Now I'm just having a play with multi-word synonyms, which is proving
interesting; overall I'm hugely impressed with the library so far and I'm
really enjoying working on it, so many thanks for all your work!

Michael

On Fri, 5 May 2017 at 11:19 Itamar Syn-Hershko <it...@code972.com> wrote:

> 2 notes on this:
>
> 1. You can use the date as a tie breaker as a second or third sort order.
> See for instance
> http://stackoverflow.com/questions/34035405/lucene-sort-by-score-and-then-modified-date
>
> 2. In 4.8 there is something new called FunctionQuery - if you store dates
> as numerics you get to execute a function on the score and document values
> during search. I will try to find some time to add a working example of
> this to https://github.com/synhershko/LuceneNetDemo
>
> HTH
>
> --
>
> Itamar Syn-Hershko
> Freelance Developer & Consultant
> Elasticsearch Partner
> Microsoft MVP | Lucene.NET PMC
> http://code972.com | @synhershko <https://twitter.com/synhershko>
> http://BigDataBoutique.co.il/
>
> On Thu, Apr 6, 2017 at 1:26 AM, <al...@bluewire-technologies.com>
> wrote:
>
>> Hi Michael,
>>
>> I’d do it by storing the datestamp as a numeric field at the desired
>> resolution, and then writing a scorer which calculates ‘age’ of a document
>> (‘now’ minus the datestamp) and *reducing* the document’s score (multiply
>> by weighting less than 1) based on that. Something like (x/(x+age))^n
>> should work, with x and n adjustable for the desired falloff
>> characteristics.
>>
>> Mapping DateTimeOffset->integer in a stable fashion is usually awkward
>> because future calendar changes can break stored data, but since we’re only
>> dealing with past dates here it should be simple. You could probably get
>> away with using Ticks, which is at least evenly distributed across past
>> times.
>>
>> Alex Davidson
>> Bluewire Technologies
>>
>> From: Michael Newton
>> Sent: 05 April 2017 18:39
>> To: user@lucenenet.apache.org
>> Subject: Weighting results by 'freshness'
>>
>> 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
>>
>>
>

Re: Weighting results by 'freshness'

Posted by Itamar Syn-Hershko <it...@code972.com>.
2 notes on this:

1. You can use the date as a tie breaker as a second or third sort order.
See for instance
http://stackoverflow.com/questions/34035405/lucene-sort-by-score-and-then-modified-date

2. In 4.8 there is something new called FunctionQuery - if you store dates
as numerics you get to execute a function on the score and document values
during search. I will try to find some time to add a working example of
this to https://github.com/synhershko/LuceneNetDemo

HTH

--

Itamar Syn-Hershko
Freelance Developer & Consultant
Elasticsearch Partner
Microsoft MVP | Lucene.NET PMC
http://code972.com | @synhershko <https://twitter.com/synhershko>
http://BigDataBoutique.co.il/

On Thu, Apr 6, 2017 at 1:26 AM, <al...@bluewire-technologies.com>
wrote:

> Hi Michael,
>
> I’d do it by storing the datestamp as a numeric field at the desired
> resolution, and then writing a scorer which calculates ‘age’ of a document
> (‘now’ minus the datestamp) and *reducing* the document’s score (multiply
> by weighting less than 1) based on that. Something like (x/(x+age))^n
> should work, with x and n adjustable for the desired falloff
> characteristics.
>
> Mapping DateTimeOffset->integer in a stable fashion is usually awkward
> because future calendar changes can break stored data, but since we’re only
> dealing with past dates here it should be simple. You could probably get
> away with using Ticks, which is at least evenly distributed across past
> times.
>
> Alex Davidson
> Bluewire Technologies
>
> From: Michael Newton
> Sent: 05 April 2017 18:39
> To: user@lucenenet.apache.org
> Subject: Weighting results by 'freshness'
>
> 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
>
>

RE: Weighting results by 'freshness'

Posted by al...@bluewire-technologies.com.
Hi Michael,

I’d do it by storing the datestamp as a numeric field at the desired resolution, and then writing a scorer which calculates ‘age’ of a document (‘now’ minus the datestamp) and *reducing* the document’s score (multiply by weighting less than 1) based on that. Something like (x/(x+age))^n should work, with x and n adjustable for the desired falloff characteristics.

Mapping DateTimeOffset->integer in a stable fashion is usually awkward because future calendar changes can break stored data, but since we’re only dealing with past dates here it should be simple. You could probably get away with using Ticks, which is at least evenly distributed across past times.

Alex Davidson
Bluewire Technologies

From: Michael Newton
Sent: 05 April 2017 18:39
To: user@lucenenet.apache.org
Subject: Weighting results by 'freshness'

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


Re: Weighting results by 'freshness'

Posted by Matt Diehl <md...@lexprompt.com.INVALID>.
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 <mi...@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
>

Re: Weighting results by 'freshness'

Posted by Mirek Novák <mi...@auto.cz>.
Hi,

I've done something like this using custom collector and reciprocal function 
known from SOLR.  My solution incorporates age of the doc and documents' 
category. With reciprocal function I have achieved nonlinear aging  of a 
document and different aging schema for different categories. I did it years 
ago with Lucene.NET 2.9.* it still works great.

All my work is based on those slides

https://www.slideshare.net/lucenerevolution/potter-timothy-boosting-documents-in-solr

M.


-----Původní zpráva----- 
From: Michael Newton
Sent: Wednesday, April 05, 2017 7:39 PM
To: user@lucenenet.apache.org
Subject: Weighting results by 'freshness'

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