如果lucene中为null,如何按具有替代值的字段排序? [英] How to sort by a field that has an alternative value if null in lucene?

查看:86
本文介绍了如果lucene中为null,如何按具有替代值的字段排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按日期字段(date1)对lucene(.net)搜索结果进行排序,但是如果未设置date1,我想使用date2.

I want to sort my lucene(.net) search results by a date field (date1), but if date1 is not set, I'd like to use date2.

传统的排序方法是按日期1排序,然后按日期2排序相同的值.这意味着每当我退回到日期2时,这些值将位于日期的顶部(或底部).结果集.我想将date2值与date1值交错.

The traditional sort method is to sort by date1, and then sort the values that are the same by date 2. This would mean that whenever I did fall back to date2, these values would be at the top (or bottom) of the result set. I'd like to interleave the date2 values with the date1 values.

换句话说,我想排序(date1!= null?date1:​​date2).

In other words, I want to sort on (date1 != null ? date1 : date2).

在lucene中有可能吗?

Is this possible in lucene?

我认为我可以在索引创建阶段做到这一点(只需将相关的日期值放在一个新字段中),但是我没有足够的控制索引过程的能力来做到这一点,所以我想进行排序解决方案.

I reckon I could do this in the index creation phase (just put the relevant date value in a new field) but I don't have enough control of the indexing process to be able to do this, so would like a sorting solution.

有什么想法吗?

谢谢 马特

推荐答案

结果非常简单.正如Yuval建议的那样,您必须实现ScoreDocComparator.但是,您只需要实现一次(我有一个包含两个日期的文档,我不想按date1然后按date2排序,而是按date1(如果指定了)或date2(如果未指定)排序.请考虑实际日期和临时日期.我想使用实际日期(如果可用),但如果没有,那么临时日期就足够了.

Turns out it's very easy. You do have to implement ScoreDocComparator, as Yuval suggested. However, you only need to implement it once (I have a document with two dates, I don't want to sort by date1 then date2, but rather by date1 if it's specified, or date2 if not. Think of an actual date and a provisional date. I want to use the actual date if it's available, but if not, then the provisional date is good enough).

这是我的代码:

public class ActualOrProvisionalDateSortComparator : ScoreDocComparator
{
    private readonly StringIndex actualDates;
    private readonly StringIndex provisionalDates;

    public TxOrCreatedDateSortComparator(IndexReader reader, FieldCache fieldCache)
    {
        actualDates = fieldCache.GetStringIndex(reader, "actualDate");
        provisionalDates = fieldCache.GetStringIndex(reader, "provisionalDate");
    }

    public int Compare(ScoreDoc i, ScoreDoc j)
    {
        var date1 = GetValue(i.doc);
        var date2 = GetValue(j.doc);

        return date1.CompareTo(date2);
    }

    public IComparable SortValue(ScoreDoc i)
    {
        return GetValue(i.doc);
    }

    public int SortType()
    {
        return SortField.CUSTOM;
    }

    private string GetValue(int doc)
    {
        return actualDates.Lookup[actualDates.Order[doc]] ?? provisionalDates.Lookup[provisionalDates.Order[doc]];
    }
}

我的ActualOrProvisionalDateSortComparatorSource传入FieldCache_Fields.DEFAULT,我们走了!

My ActualOrProvisionalDateSortComparatorSource passes in FieldCache_Fields.DEFAULT and we're away!

这篇关于如果lucene中为null,如何按具有替代值的字段排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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