使用命名字段和集合查询ravendb索引

时间:2017-05-10 08:14:26

标签: lucene ravendb

我在ravendb中有以下索引,Changes_Which是一个命名字段,它映射到子集合

    public My_Index()
    {
        Map = revisions => from revision in revisions
                           from change in revision.Changes
                           where (revision.AuditedType == "typeA")
                       select
                        new
                        {
                            revision.ChangeTimestamp,
                            Changes_Which = change.Which
                        };

    }

修订版的json如下:

{
"AuditedType" : "typeA",
"Changes": [
{
"SubWhich": null,
"Which": "Regulation",
"Original": "Unknown",
"New": "Yes"
},
{
"SubWhich": null,
"Which": "Other",
"Original": "Unknown",
"New": "Yes"
},
{
"SubWhich": null,
"Which": "Regulation",
"Original": "Unknown",
"New": "Yes"
}
]
"ChangeTimestamp": "2011-05-03"
}

我正在尝试使用Changes_Which字段过滤索引中的结果:

        var q = session.Advanced.LuceneQuery<revision>("My_Index/Index")
            .WaitForNonStaleResultsAsOfLastWrite()
            .AddOrder(paging.SortColumn, paging.SortOrder == "desc")
            .Skip(((paging.CurrentPage - 1) * paging.RecordsPerPage))
            .Take(paging.RecordsPerPage);

        if (fromDate > DateTime.MinValue && toDate > DateTime.MinValue)
        {
            q = q.WhereGreaterThanOrEqual("ChangeTimestamp", fromDate)
                .AndAlso()
                .WhereLessThanOrEqual("ChangeTimestamp", toDate.AddDays(1).AddMinutes(-1));
        }

        if (removeNoChange)
        {
            q = q.AndAlso().Not.WhereEquals("Changes_Which", "Regulation");  
        }

这一行:

q = q.AndAlso()。Not.WhereEquals(“Changes_Which”,“Regulation”);

确实有效,但只删除了“哪个”:“规则”的第一个匹配,该集合中的第三个匹配仍然存在。

任何建议表示赞赏。

1 个答案:

答案 0 :(得分:1)

原因是您要为同一文档输出不同的索引条目。 在RavenDB中搜索时,您正在搜索文档,在您的情况下,您筛选出该文档的第一个索引条目,但是还有其他索引条目匹配,因此会返回。

你可能会更好:

<!DOCTYPE html>
...
<div>
<?php if ( the_thing === true ) : ?>
    <p>The thing is true! \o/</p>
<?php else if ( the_other_thing === true ) : ?>
    <p>The other thing is true! meh</p>
<?php else : ?>
    <p>Nothing is true :-(</p>
<?php endif; ?>
</div>
...

这将只输出每个文档的一个条目(建议使用),并将导致过滤因 Map = revisions => from revision in revisions where (revision.AuditedType == "typeA") select new { revision.ChangeTimestamp, Changes_Which = revision.Changes.Select(chnage => change.Which) };

而更改的所有文档