RavenDB:如何使用Multi Maps / Reduce索引

时间:2011-09-21 08:00:06

标签: .net nosql mapreduce ravendb

我的模型非常简单:

public class PhraseMeta:
{
 public int Id { get; set; }
 public string ModuleName { get; set; }
 public string Description { get; set; }
 public DateTime ModifiedDate { get; set; }
}

public class Phrase
{
  public int Id { get; set; }
  public int PhraseMetaId { get; set; } //reference to PhraseMeta
  public string Language { get; set; }
  public string Text { get; set; }
}

短语包含一些翻译,PhraseMeta包含多个短语的元信息。 我试图找到具有ModuleName和Language的Phrase的文本。 据我所知,RavenDB的Multi Maps / Reduce索引功能可以帮助它,而不是使用WhereEntityIs。 我的索引是:

public class PhraseEntry
{
  public string MetaId { get; set; }
  public string ModuleName { get; set; }
  public string Language { get; set; }
  public string Text { get; set; } 
}

public class PhraseTranslationIndex : AbstractMultiMapIndexCreationTask<PhraseEntry>
{
  public PhraseTranslationIndex()
  {
    this.AddMap<PhraseMeta>(phraseMetas => from pm in phraseMetas
                 select new
                 {
                   MetaId = pm.Id,
                   ModuleName = pm.ModuleName,
                   Language = (string)null,
                   Text = (string)null
                 });

    this.AddMap<Phrase>(phrases => from phrase in phrases
               select new
               {
                 MetaId = phrase.PhraseMetaId,
                 ModuleName = (string)null,
                 Language = phrase.Language,
                 Text = phrase.Text
               });

    this.Reduce = results => from entry in results
                 group entry by entry.MetaId
                   into g
                   select new
                   {
                     MetaId = g.Key,
                     ModuleName = g.Select(x => x.ModuleName).Where(x => x != null).First(),
                     Language = g.Select(x => x.Language).Where(x => x != null).First(),
                     Text = g.Select(x => x.Text).Where(x => x != null).First()
                   };

    this.Index(x => x.ModuleName, FieldIndexing.Analyzed);
    this.Index(x => x.Language, FieldIndexing.Analyzed);
    this.Index(x => x.Text, FieldIndexing.Analyzed);
  }
}

这就是我尝试使用它的方式:

var entry = documentSession.Query<PhraseEntry, PhraseTranslationIndex>
                           .Where(p => p.ModuleName == "MyModule")
                           .Where(p => p.Language == "en")
                           .FirstOrDefault();

这个索引没有结果。我正在使用build 472.

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

问题可能是你正在使用First(),尝试使用FirstOrDefault()