如何在RavenDB中提高新近度的文档? [英] How can you boost documents by recency in RavenDB?

查看:69
本文介绍了如何在RavenDB中提高新近度的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在RavenDB查询中增强最近的文档?

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

这个问题正是我想要做的,但指的是本机Lucene,而不是RavenDB.

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; }
}

如何增强日期接近给定日期的文档,例如DateTime.UtcNow?

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

我不想OrderByDecending(x => x.DateCreated),因为还有其他搜索参数需要影响结果.

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

推荐答案

您可以在索引编制过程中进行增强,它已经在RavenDB中使用了相当长的时间,但根本没有在文档中找到.但是,有一些单元测试说明了此处.

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);

您可能需要尝试使用正确的值以用于增加量.一毫秒有10,000个滴答声,所以这就是为什么我要除以这么大的数字.

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.

此外,请注意要使用的DateTime是UTC,或者如果无法控制它的来源,请改用DateTimeOffset.为什么?因为您使用的是从某个参考点算起的持续时间,并且您不希望结果对于不同的时区或夏时制时间变化不明确.

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.

这篇关于如何在RavenDB中提高新近度的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆