Indexing RavenDB索引创建错误:索引已存在

Indexing RavenDB索引创建错误:索引已存在,indexing,ravendb,Indexing,Ravendb,我现在正在尝试使用RavenDb创建索引,以供以后使用(例如,使用索引进行大规模删除) 据我所知,最好的做法是在应用程序启动时创建索引,而无需检查索引是否已经存在,因为在这种情况下,它只需更新(以防发生更改) 考虑到我试图在global.asax应用程序启动事件中构建自己的索引(因为我正在MVC应用程序中玩)。以下是我创建索引的代码: store.DatabaseCommands.PutIndex("Xlns/ProductItems/ByCatalogueId", new Inde

我现在正在尝试使用RavenDb创建索引,以供以后使用(例如,使用索引进行大规模删除)

据我所知,最好的做法是在应用程序启动时创建索引,而无需检查索引是否已经存在,因为在这种情况下,它只需更新(以防发生更改)

考虑到我试图在global.asax应用程序启动事件中构建自己的索引(因为我正在MVC应用程序中玩)。以下是我创建索引的代码:

store.DatabaseCommands.PutIndex("Xlns/ProductItems/ByCatalogueId",
      new IndexDefinitionBuilder<ProductItem>
          {
              Map = items => from product in items
                             select new
                                    {
                                       CatalogueId = product.CatalogueId
                                    }
           }
);
store.DatabaseCommands.PutIndex(“Xlns/ProductItems/ByCatalogEID”,
新的IndexDefinitionBuilder
{
Map=items=>来自items中的产品
选择新的
{
CatalogEID=product.catalogEID
}
}
);
很简单,不是吗? 太糟糕了,当我第一次在一个空的RavenDB上启动应用程序时,它没有引发任何错误(即使看起来我不能使用索引),从第二次开始,它给了我这个错误:
无法放置索引:Xlns/ProductItems/ByCatalogEID,索引已经存在


(并非如此)有趣的是,使用RavendbStudio,我在任何地方都看不到索引,也无法使用它来查询。因此,索引似乎不可用,但系统不知何故知道它?

首先,建议:将索引设置为类。这样,您可以在以后使用索引类型强式键入查询:

public class ProductItems_ByCatalogueId : AbstractIndexCreationTask<ProductItem>
{
    public ProductItems_ByCatalogueId()
    {
        Map = items => from product in items
                       select new
                       {
                           CatalogueId = product.CatalogueId
                       }
    }
}
现在,您的索引已准备就绪。要使用此索引进行查询,请执行以下操作:

var products = ravenSession.Query<ProductItem, ProductItems_ByCatalogueId>()
    .Where(...)
var products=ravenSession.Query()
.其中(…)
带有:

    documentStore.DatabaseCommands.GetIndex("BlogPosts/ByTitles")
您可以知道是否存在索引,然后知道是否需要添加索引; ravendb文档建议使用类定义静态索引:

Judah Himango的例子正是您在链接中找到的(并且是正确的)

    documentStore.DatabaseCommands.GetIndex("BlogPosts/ByTitles")