我有很简单的模型:
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; }
}
Phrase 包含一些翻译,PhraseMeta 有几个 Phrase 的元信息。
我试图找到具有 ModuleName 和 Language 的 Phrase 文本。
据我了解,RavenDB 的多映射/减少索引功能可以帮助它而不是使用 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();
而这个索引没有结果。我正在使用内部版本 472。
有任何想法吗?
最佳答案
问题可能是您使用的是 First(),请尝试使用 FirstOrDefault()
关于.net - RavenDB:如何使用多映射/减少索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7496413/