按 Id 查询多图索引 - 字段“__document_id"未编入索引,无法查询未编入索引的字段 [英] Query multimap index by Id - The field '__document_id' is not indexed, cannot query on fields that are not indexed

查看:67
本文介绍了按 Id 查询多图索引 - 字段“__document_id"未编入索引,无法查询未编入索引的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 RavenDb 中,我有一个简单的多图索引,如下所示:

In RavenDb I have a simple multimap index which looks like below:

public class MessageOutboxIndex : AbstractMultiMapIndexCreationTask<MessageOutboxIndex.ReduceResult>
    {
        public class ReduceResult
        {
            public string Id { get; set; }
            public string FromAccountId { get; set; }
            public Core.Enums.Message.MessageStatus OutboxStatus { get; set; }
            public string ToAccountId { get; set; }
            public string ToArtistName { get; set; }
            public DateTimeOffset DateSent { get; set; }
            public string Subject { get; set; }
        }

        public MessageOutboxIndex()
        {
            AddMap<Message>(messages => from msg in messages
                                        select new
                                                   {
                                                       Id = msg.Id,
                                                       FromAccountId = msg.FromAccountId,
                                                       OutboxStatus = msg.OutboxStatus,
                                                       ToAccountId = (string)null,
                                                       ToArtistName = (string)null,
                                                       DateSent = msg.DateSent,
                                                       Subject = msg.Subject
                                                   });

            AddMap<MessageRecipient>(recipients => from recipient in recipients
                                                       select new
                                                                  {
                                                                      Id = recipient.MessageId,
                                                                      FromAccountId = (string)null,
                                                                      OutboxStatus = (object)null,
                                                                      ToAccountId = recipient.ToAccountId,
                                                                      ToArtistName = recipient.ToArtistName,
                                                                      DateSent = DateTimeOffset.MinValue,
                                                                      Subject = (string)null
                                                                  });

            Reduce = results => from result in results
                                group result by result.Id
                                    into g
                                    select new
                                    {
                                        Id = g.Key,
                                        FromAccountId = g.Select(x => x.FromAccountId).Where(x => x != null).FirstOrDefault(),
                                        OutboxStatus = g.Select(x => x.OutboxStatus).Where(x => x != null).FirstOrDefault(),
                                        ToAccountId = g.Select(x => x.ToAccountId).Where(x => x != null).FirstOrDefault(),
                                        ToArtistName = g.Select(x => x.ToArtistName).Where(x => x != null).FirstOrDefault(),
                                        DateSent = g.Max(x => (DateTimeOffset)x.DateSent),
                                        Subject = g.Select(x => x.Subject).Where(x => x != null).FirstOrDefault()
                                    };

        }
    } 

但是,我现在只想使用下面的示例查询通过 Id 返回此索引中的特定消息:

However, I now want to only return specific messages from this index by Id using this example query below:

var messages = _documentSession.Query<MessageOutboxIndex.ReduceResult, MessageOutboxIndex>()
                .Where(x => x.Id.In(new string[2] {"messages/1", "messages/2"}))
                .ToList();

失败并出现以下错误:

System.ArgumentException: The field '__document_id' is not indexed, cannot query on fields that are not indexed

按 Id 查询适用于普通索引,但不适用于多映射索引.这是一个错误吗?

Querying by Id works for normal indexes, but not multimap indexes. Is this a bug?

保罗

推荐答案

Paul,我们自动将 Id 字段转换为 __document_id,因为我们不知道我们正在查询没有 __document_id 的 m/r 索引.是的,您的解决方法是可行的.

Paul, We auto translate the Id field to the __document_id, because we don't know that we are querying a m/r index that doesn't have a __document_id. Your workaround is the way to go, yes.

这篇关于按 Id 查询多图索引 - 字段“__document_id"未编入索引,无法查询未编入索引的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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