RavenDB InvalidOperationException "The query results type is '<Document>' but you expected to get results of type '<IndexResult>' "
RavenDB InvalidOperationException "The query results type is '<Document>' but you expected to get results of type '<IndexResult>' "
我正在尝试按照索引教程 "Example II" 从
https://ravendb.net/docs/article-page/3.5/csharp/indexes/map-indexes
并得到以下 InvalidOperationException:
"The query results type is 'Resource' but you expected to get results of type 'Result'. If you want to return a projection, you should use .ProjectFromIndexFieldsInto() (for Query) or .SelectFields() (for DocumentQuery) before calling to .ToList()."
我失败了,我的设置与文档有何不同。
我有这样的索引设置:
public class ResourceIndex : AbstractIndexCreationTask<Raven.Resource>
{
public class Result
{
public string[] Query { get; set; }
}
public ResourceIndex()
{
Map = resources => from resource in resources
select new
{
Query = new[]
{
resource.ID.ToString(),
}
};
Index("Query", FieldIndexing.Analyzed);
}
}
...
然后这样查询:
public IEnumerable<Raven.Resource> QueryAssets(string searchTerm)
{
using (IDocumentSession session = dataStore.OpenSession())
{
var resources = session
.Advanced
.DocumentQuery<ResourceIndex.Result, ResourceIndex>()
.Search(x => x.Query, searchTerm).OfType<Raven.Resource>().ToList();
return resources;
}
}
我的设置与有关如何将索引与 DocumentQuery 和 .Search 结合使用的文档中缺少什么?
您只需在 Search
.SelectField<Raven.Resource>()
之后添加,它就会像建议的异常一样工作。您的查询应如下所示
var resources = session
.Advanced
.DocumentQuery<ResourceIndex.Result, ResourceIndex>()
.Search(x => x.Query, searchTerm).SelectFields<Raven.Resource>().ToList();
您目前没有遗漏文档中的任何内容,只需删除 OfType()
并仅使用 SelectFields
对于查询 OfType
与 SelectFields
的工作方式相同,因此如果您使用它,您的查询应该如下所示:
session.Query<ResourceIndex.Result, ResourceIndex>()
.Search(x => x.Query, "2").OfType<Resource>().ToList();
这可能有点晚了,但我认为错误的原因是:
您已声明:
public class ResourceIndex : AbstractIndexCreationTask<Raven.Resource>
这表示:根据文档 Raven.Resource
创建一个索引,它最终会返回 Raven.Resource
. 类型的结果
然后在你的代码中,为了使用这个索引,你说:
.DocumentQuery<ResourceIndex.Result, ResourceIndex>()
这表示:查询索引 ResourceIndex
,我希望返回 ResourceIndex.Result
个文档。
但是……这是错误的。然后索引包含 Raven.Resource
个文档,而不是 ResourceIndex.Result
个文档。
我正在尝试按照索引教程 "Example II" 从 https://ravendb.net/docs/article-page/3.5/csharp/indexes/map-indexes
并得到以下 InvalidOperationException:
"The query results type is 'Resource' but you expected to get results of type 'Result'. If you want to return a projection, you should use .ProjectFromIndexFieldsInto() (for Query) or .SelectFields() (for DocumentQuery) before calling to .ToList()."
我失败了,我的设置与文档有何不同。
我有这样的索引设置:
public class ResourceIndex : AbstractIndexCreationTask<Raven.Resource>
{
public class Result
{
public string[] Query { get; set; }
}
public ResourceIndex()
{
Map = resources => from resource in resources
select new
{
Query = new[]
{
resource.ID.ToString(),
}
};
Index("Query", FieldIndexing.Analyzed);
}
}
...
然后这样查询:
public IEnumerable<Raven.Resource> QueryAssets(string searchTerm)
{
using (IDocumentSession session = dataStore.OpenSession())
{
var resources = session
.Advanced
.DocumentQuery<ResourceIndex.Result, ResourceIndex>()
.Search(x => x.Query, searchTerm).OfType<Raven.Resource>().ToList();
return resources;
}
}
我的设置与有关如何将索引与 DocumentQuery 和 .Search 结合使用的文档中缺少什么?
您只需在 Search
.SelectField<Raven.Resource>()
之后添加,它就会像建议的异常一样工作。您的查询应如下所示
var resources = session
.Advanced
.DocumentQuery<ResourceIndex.Result, ResourceIndex>()
.Search(x => x.Query, searchTerm).SelectFields<Raven.Resource>().ToList();
您目前没有遗漏文档中的任何内容,只需删除 OfType()
并仅使用 SelectFields
对于查询 OfType
与 SelectFields
的工作方式相同,因此如果您使用它,您的查询应该如下所示:
session.Query<ResourceIndex.Result, ResourceIndex>()
.Search(x => x.Query, "2").OfType<Resource>().ToList();
这可能有点晚了,但我认为错误的原因是:
您已声明:
public class ResourceIndex : AbstractIndexCreationTask<Raven.Resource>
这表示:根据文档 Raven.Resource
创建一个索引,它最终会返回 Raven.Resource
. 类型的结果
然后在你的代码中,为了使用这个索引,你说:
.DocumentQuery<ResourceIndex.Result, ResourceIndex>()
这表示:查询索引 ResourceIndex
,我希望返回 ResourceIndex.Result
个文档。
但是……这是错误的。然后索引包含 Raven.Resource
个文档,而不是 ResourceIndex.Result
个文档。