How can you boost documents by recency in RavenDB?

落爺英雄遲暮 提交于 2020-01-14 09:29:07

问题


Is it possible to boost recent documents in a RavenDB query?

This question is exactly what I want to do but refers to native Lucene, not RavenDB.

For example, if I have a Document like this

public class Document
{
    public string Title { get; set; }
    public DateTime DateCreated  { get; set; }
}

How can I boost documents who's date are closer to a given date, e.g. DateTime.UtcNow?

I do not want to OrderByDecending(x => x.DateCreated) as there are other search parameters that need to affect the results.


回答1:


You can boost during indexing, it's been in RavenDB for quite some time, but it's not in the documentation at all. However, there are some unit tests that illustrate here.

Those tests show a single boost value, but it can easily be calculated from other document values instead. You have the full document available to you since this is done when the index entries are written. You should be able to combine this with the technique described in the post you referenced.

Map = docs => from doc in docs
              select new
              {
                  Title = doc.Title.Boost(doc.DateCreated.Ticks / 1000000f)
              };

You could also boost the entire document instead of just the Title field, which might be useful if you have other fields in your search algorithm:

Map = docs => from doc in docs
              select new
              {
                  doc.Title
              }.Boost(doc.DateCreated.Ticks / 1000000f);

You may need to experiment with the right value to use for the boost amount. There are 10,000 ticks in a millisecond, so that's why i divide by such a large number.

Also, be careful that the DateTime you're working with is in UTC, or if you don't have control over where it comes from, then use a DateTimeOffset instead. Why? Because you're using a calculated duration from some reference point and you don't want the result to be ambiguous for different time zones or around daylight savings time changes.



来源:https://stackoverflow.com/questions/13863739/how-can-you-boost-documents-by-recency-in-ravendb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!