如何在Ravendb中创建复合索引

发布于 2024-12-04 05:34:51 字数 791 浏览 1 评论 0原文

我的基地(RavenDB)中有文档集合“消息” 文档定义如下:

class Message
{
    string Content;
    Tag[] Tags;
    Location[] Locations;
    string[] Actions;
    bool IsActive;
}

标签类定义:

class Tag
{
    string Value;
    Translation[] Translations;
}

位置类:

class Location
{
    string Code;
    Translation[] Translations;
}

翻译类:

class Translation
{
    string LanguageCode;
    string Value;
}

因此,我想创建一个索引,允许我通过多个字段进行查询:

  1. 按 Message.Content 进行全文搜索
  2. 仅包含 IsActive==true 的
  3. 消息包含我在 Message.Actions 中的操作
  4. 消息包含带有 myValue 和 myLanguageCode 的标记
  5. 位置包含带有某些 myCode 和 myLanguageCode 的位置

我想同时查询所有条件

那么,我应该如何为 RavenDB 定义索引?

I have documents collection "Messages" in my base (RavenDB)
Document definition like:

class Message
{
    string Content;
    Tag[] Tags;
    Location[] Locations;
    string[] Actions;
    bool IsActive;
}

Tag class definition:

class Tag
{
    string Value;
    Translation[] Translations;
}

Location class:

class Location
{
    string Code;
    Translation[] Translations;
}

Translation class:

class Translation
{
    string LanguageCode;
    string Value;
}

So, I want to create a index that will allow me to make queries by several fields:

  1. Full-text search by Message.Content
  2. Only messages with IsActive==true
  3. Messages that contains my action in Message.Actions
  4. Messages that contains tag with myValue and myLanguageCode
  5. Locations that contains location with some myCode and myLanguageCode

I would like to query to be on all the conditions simultaneously

So, how should i define index for RavenDB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

输什么也不输骨气 2024-12-11 05:34:51

好吧,在对 RavenDB 自动动态索引进行简短研究后,我创建了类似的东西

new IndexDefinition
{
    Map = @"
     from doc in docs.Messages 
      where IsActive==true
     from docActionsItem in (IEnumerable<dynamic>)doc.Actions
     from docTagsItem in (IEnumerable<dynamic>)doc.Tags 
     from docTagsItemTranslationsItem in (IEnumerable<dynamic>)docTagsItem.Translations
     from docLocationsItem in (IEnumerable<dynamic>)doc.Locations
     from docLocationsItemTranslationsItem in (IEnumerable<dynamic>)docLocationsItem.Translations
     select new { 
      TagsValue = docTagsItem.Value, 
      Content = doc.Content, 
      Actions=docActionsItem, 
      TagsTranslationsLanguageCode = docTagsItemTranslationsItem.LanguageCode,
      TagsTranslationsValue = docTagsItemTranslationsItem.Value, 
      LocationsCode = docLocationsItem.Code, 
      LocationsTranslationsLanguageCode=docLocationsItemTranslationsItem.LanguageCode,
      LocationsTranslationsValue=docLocationsItemTranslationsItem.Value
     }",
    Analyzers =
        {
            {"Content", typeof(StandardAnalyzer).FullName},                                    
        }
}

Well, after a short studies of RavenDB auto dynamic indexes i created something like

new IndexDefinition
{
    Map = @"
     from doc in docs.Messages 
      where IsActive==true
     from docActionsItem in (IEnumerable<dynamic>)doc.Actions
     from docTagsItem in (IEnumerable<dynamic>)doc.Tags 
     from docTagsItemTranslationsItem in (IEnumerable<dynamic>)docTagsItem.Translations
     from docLocationsItem in (IEnumerable<dynamic>)doc.Locations
     from docLocationsItemTranslationsItem in (IEnumerable<dynamic>)docLocationsItem.Translations
     select new { 
      TagsValue = docTagsItem.Value, 
      Content = doc.Content, 
      Actions=docActionsItem, 
      TagsTranslationsLanguageCode = docTagsItemTranslationsItem.LanguageCode,
      TagsTranslationsValue = docTagsItemTranslationsItem.Value, 
      LocationsCode = docLocationsItem.Code, 
      LocationsTranslationsLanguageCode=docLocationsItemTranslationsItem.LanguageCode,
      LocationsTranslationsValue=docLocationsItemTranslationsItem.Value
     }",
    Analyzers =
        {
            {"Content", typeof(StandardAnalyzer).FullName},                                    
        }
}
骑趴 2024-12-11 05:34:51

在 RavenDB 中进行查询时无需预先定义索引。只需创建 linq-query - RavenDB 就会为您动态创建索引,无需任何额外成本。

您可能想要自己创建索引的唯一用例是,如果想要指定不同的 Lucene.NET 分析器来对 Message.Content 字段执行所谓的“全文搜索”。但即便如此,也应该像为索引创建 linq 查询并在启动时将其传递到 RavenDB 一样简单。如果您想知道如何做到这一点,我建议您查看 Ayende RaccoonBlog 示例或官方文档,它们将很快更新(仅供参考 -> ravendb/docs 有新文档)。

There's no need to define an index upfront to do queries in RavenDB. Just create the linq-query - RavenDB will then dynamically create an index for you without any additional cost.

The only use-case where you would possible want to create an index by your own, would be if want do specify a different Lucene.NET analyzer to do what you call "full-text search" on the field Message.Content. But even that should be as simple as just creating the linq-query for the index and pass it into RavenDB on startup. If you want to know how to do that, I recommend to have a look into Ayende RaccoonBlog sample or the officials docs, which will be updated soon (FYI -> ravendb/docs has the new docs).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文