简体   繁体   English

在RavenDB中索引愿望和交易清单

[英]Indexing wish and trade lists in RavenDB

I've tried many different strategies for indexing my data but can't seem to figure it out by myself. 我尝试了许多不同的策略来索引我的数据,但我自己似乎无法弄清楚。

I'm building a database over users and their games. 我正在建立有关用户及其游戏的数据库。 The users can supply the database with games they own and would like to trade as well as a list of games they would like to have: 用户可以向数据库提供他们拥有并想要交易的游戏以及他们想要拥有的游戏列表:

public class Member : EntityBase
{
    public List<Game> TradeList { get; set; }
    public List<Game> WishList { get; set; }
}

I'm trying to create and index I can query in the form of "Give me a list of all games (with corresponding members) which have games in their TradeList matching my WishList as well as having games in their WishList matching my TradeList".. and of course, myself excluded. 我正在尝试以“给我所有具有与我的愿望清单匹配的交易清单中的游戏以及在与我的贸易清单匹配的愿望清单中的游戏的所有游戏(具有相应成员)的列表的形式来查询我的索引”。当然,我自己也被排除在外。

I tried creating a MultiMapIndex: 我尝试创建一个MultiMapIndex:

public class TradingIndex : AbstractMultiMapIndexCreationTask<TradingIndex.Result>
{
    public enum ListType
    {
        Wishlist,
        Tradelist
    }

    public class Result
    {
        public string Game { get; set; }
        public string Member { get; set; }
        public ListType List { get; set; }
    }

    public TradingIndex()
    {
        AddMap<Member>(members => from member in members
            from game in member.TradeList
            select new Result()
            {
                Game = game.Id,
                Member = member.Id,
                List = ListType.Tradelist
            });

        AddMap<Member>(members => from member in members
            from game in member.WishList
            select new Result()
            {
                Game = game.Id,
                Member = member.Id,
                List = ListType.Wishlist
            });
    }
}

And then querying it like this: 然后像这样查询它:

db.Query<TradingIndex.Result, TradingIndex>()
    .Where(g => 
        (g.Game.In(gamesIWant) && g.List == TradingIndex.ListType.Tradelist)
        &&
        (g.Game.In(gamesITrade) && g.List == TradingIndex.ListType.Wishlist)
        &&
        g.Member != me.Id
)

But I can't get that to work. 但是我无法解决这个问题。 I've also looked at Map/Reduce, but the problem I have seem to be getting RavenDB to give me the correct result type. 我也看过Map / Reduce,但问题似乎是让RavenDB为我提供正确的结果类型。

I hope you get what I'm trying to do and can give me some hints on what to look into. 我希望您能得到我正在尝试做的事情,并且可以给我一些有关要研究的提示。

First, you'll need to make sure that you store the fields you are indexing. 首先,您需要确保存储要建立索引的字段。 This is required so you can get the index results back, instead of the documents that matched the index. 这是必需的,因此您可以取回索引结果 ,而不是与索引匹配的文档

Add this to the bottom of your index definition: 将其添加到索引定义的底部:

StoreAllFields(FieldStorage.Yes);

Or if you want to be more verbose, (perhaps your index is doing other things also): 或者,如果您想更详细些(也许您的索引还在做其他事情):

Store(x => x.Game, FieldStorage.Yes);
Store(x => x.Member, FieldStorage.Yes);
Store(x => x.List, FieldStorage.Yes);

When you query this, you'll need to tell Raven to send you back the index entries, by using ProjectFromIndexFieldsInto as described here . 当查询此内容时,您需要通过使用ProjectFromIndexFieldsInto告知Raven将索引条目发回给您, 如此处所述

Next, you need to realize that you aren't creating any single index entry that will match your query. 接下来,您需要认识到您不会创建任何与查询匹配的索引条目。 The multimap index is creating separate entries in the index for each map. 多图索引正在为每个图的索引中创建单独的条目。 If you want to combine them in your results, you'll need to use an intersection query . 如果要将它们合并到结果中,则需要使用交集查询

Putting this together, your query should look like this: 综合起来,您的查询应如下所示:

var q = session.Query<TradingIndex.Result, TradingIndex>()
               .Where(g => g.Game.In(gamesIWant) && 
                           g.List == TradingIndex.ListType.Tradelist &&
                           g.Member != me.Id)
               .Intersect()
               .Where(g => g.Game.In(gamesITrade) &&
                           g.List == TradingIndex.ListType.Wishlist &&
                           g.Member != me.Id)
               .ProjectFromIndexFieldsInto<TradingIndex.Result>();

Full test in this GIST . 在此GIST中进行全面测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM