我正在从Beginning Raven 2.x学习RavenDB(内部版本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,然后在Raven Studio上运行
Population [long]
字段的过滤器,过滤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应用程序存储任何对象,只能读取。
这可能意味着架构不同步,或者因为元数据为
{}
,DB尚不确定数据类型。这是从文档中得到的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"
但这也没有帮助串行器。
最佳答案
您必须告诉Raven,Population是一个数字,因为所有值都存储为文本。
因此,在您的索引构造函数中编写类似
Sort(x => x.Population , SortOptions.Long);
关于c# - Raven-Studio索引向C#LINQ查询返回不同的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22766400/