如何更改索引上的集合名称

时间:2015-05-14 23:24:43

标签: ravendb

当我保存具有通用类型DataView<Customer>的文档时,我手动将集合名称设置为“customers”。但是,我在使用带有非默认集合名称的AbstractIndexCreationTask制作索引时遇到了一些麻烦。这是我的索引:

public class customers_Search
    : AbstractIndexCreationTask<DataView<Customer>, customers_Search.Result>
{
    public class Result
    {
        public string Query { get; set; }
    }

    public customers_Search()
    {
        Map = customers =>
            from customer in customers
            where customer.Data != null
            select new
            {
                Query = AsDocument(customer.Data).Select(x => x.Value)
            };
        Index(x => x.Query, FieldIndexing.Analyzed);
    }
}

部署后,它看起来像这样:

from customer in docs.DataViewOfCustomer
where customer.Data != null
select new {
    Query = customer.Data.Select(x => x.Value)
}

这显然不起作用,如果我将DataViewOfCustomer更改为“customers”,它就可以正常工作。

我宁愿不必使用非类型检查(字符串)索引进行部署。 有没有办法设置AbstractIndexCreationTask班级的馆藏名称?

更新

由于我的数据类是通用的,我创建了一个修复名称的通用索引。

public class DataViewQuery<TEntity>
    : AbstractIndexCreationTask<DataView<TEntity>, DataViewQueryResult>
{
    private readonly string _entityName;
    private readonly string _indexName;

    // this is to fix the collection name for the index name
    public override string IndexName { get { return _indexName; } }
    // this is to fix the collection name for the index query
    public override void Execute(IDatabaseCommands databaseCommands, DocumentConvention documentConvention)
    {
        var conventions = documentConvention.Clone();
        conventions.FindTypeTagName =
            type =>
                typeof(DataView<TEntity>) == type
                    ? _entityName
                    : documentConvention.FindTypeTagName(type);
        base.Execute(databaseCommands, conventions);
    }

    public DataViewQuery(string entityName)
    {
        _entityName = entityName;
        _indexName = String.Format("{0}/{1}", entityName, "Query");

        Map = items =>
            from item in items
            where item.Data != null
            select new
            {
                Query = AsDocument(item.Data).Select(x => x.Value)
            };
        Index(x => x.Query, FieldIndexing.Analyzed);
    }
}

public class DataViewQueryResult
{
    public string Query { get; set; }
}

然后我可以创建一个具有所有配置的特定索引。

// sets the collection type (DataView<Customer>) for the index
public class CustomerQuery : DataViewQuery<Customer>
{
    // sets the collection name for the index
    public CustomerQuery() : base(EntityName.Customers) {  }
}

1 个答案:

答案 0 :(得分:1)

您需要在约定中配置它。 要配置的属性是FindTypeTagName