我正在从开始 Raven 2.x 开始学习 RavenDB(Build 2851,版本 2.5.0 / 6dce79a),并且发现 Raven-Studio 没有正确过滤。
我的数据库中有一个城市表,存储了他们的人口、位置等。我在代码中添加了一个索引,使用这个:
public class Cities_ByPopulation : AbstractIndexCreationTask<City>
{
public Cities_ByPopulation()
{
this.Map = cities => from city in cities
select new { Population = city.Population };
// Generates as this in the RDBMS
// docs.Cities.Select(city => new {
// Population = city.Population
// })
}
}
IndexCreation.CreateIndex(typeof(Cities_ByPopulation).Assembly, documentStore)
并用代码注册它。
问题 1 - Raven Studio 未按预期过滤
现在索引已添加到 RavenDB,我Population [long]
在 Raven Studio 上运行过滤器,过滤 200'000 到 500'000。
正如你所看到的,它的回调值完全超出了范围。我也尝试过,Population: [Lx200000 TO Lx500000]
但没有出现任何结果。
为了验证这一点,我创建了一个动态索引,但有同样的问题:
问题 2 - LINQ 根本没有按预期过滤
除此之外,我发现即使使用原始 LINQ 查询,也不会返回任何数据!
// RavenStore stores a singleton,
// so I can share across console apps in this solution
using (var store = RavenStore.GetDocumentStore())
{
IndexCreation.CreateIndexes(typeof(Cities_ByPopulation).Assembly, store);
const long MinRange = 200000;
const long MaxRange = 300000;
Debug.Assert(MinRange < MaxRange, "Ranges need swapping round!");
// Get cities using the index
using (var session = store.OpenSession())
{
var cities =
session.Query<City>("Cities/ByPopulation")
.Customize(x => x.WaitForNonStaleResults())
.Where(x => x.Population > MinRange && x.Population < MaxRange);
Console.WriteLine("Number of normal cities within population range: {0}", cities.Count());
}
// Get cities from raw query
using (var session = store.OpenSession())
{
var cities = session.Query<City>().Where(x => x.Population > MinRange && x.Population < MaxRange);
Console.WriteLine("Number of normal cities within population range: {0}", cities.Count());
}
// Output :
// Number of normal cities within population range: 0
// Number of normal cities within population range: 0
}
此查询的日志记录如下
Request # 275: GET - 1 ms - <system> - 200 - /docs/Raven/Databases/World
Request # 276: HEAD - 0 ms - World - 200 - /indexes/Cities/ByPopulation
Request # 277: PUT - 2 ms - World - 201 - /indexes/Cities/ByPopulation
Request # 278: GET - 0 ms - World - 404 - /docs/Raven/Replication/Destinations
Request # 279: GET - 6 ms - World - 200 - /indexes/Cities/ByPopulation?&query=Population_Range%3A%7BLx200000%20TO%20Lx300000%7D&pageSize=0&operationHeadersHash=1690003523
Query: Population_Range:{Lx200000 TO Lx300000}
Time: 6 ms
Index: Cities/ByPopulation
Results: 0 returned out of 0 total.
Request # 280: GET - 7 ms - World - 200 - /indexes/dynamic/Cities?&query=Population_Range%3A%7BLx200000%20TO%20Lx300000%7D&pageSize=0&operationHeadersHash=1690003523
Query: Population_Range:{Lx200000 TO Lx300000}
Time: 6 ms
Index: Cities/ByPopulation
Results: 0 returned out of 0 total.
一些可能有助于故障排除的附加信息
- 数据是通过 CSV 导入器导入的。
- 没有对象从 .NET 应用程序中存储,仅读取。
这可能意味着模式不同步,或者数据库还不确定数据类型,因为元数据是{}
这是来自文档的结果 JSON:
[city/1989]
{
"Name": "Aachen",
"CountryCode": "D",
"Province": "Nordrhein Westfalen",
"Population": 247113,
"CountryId": "country/1009"
}
和 C# 类:
public class City
{
public string Id { get; set; }
public string Name { get; set; }
public string CountryCode { get; set; }
public long Population { get; set; }
public string Province { get; set; }
public string CountryId { get; set; }
}
}
另一个更新
我已经手动修补了集合
this['@metadata']['Raven-Clr-Type'] = "Domain.City, Domain"
但这也没有帮助序列化程序。