Raven-Studio Index将不同的结果返回给C#LINQ Query

时间:2014-03-31 15:58:40

标签: c# ravendb ravendb-studio

我正在从Beginning 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,我在Raven Studio上运行Population [long]字段过滤,过滤200到39和500&#39。

IDE not showing results correctly

正如您所看到的,它将值拉回到范围之外。我也试过了Population: [Lx200000 TO Lx500000]但是没有结果出现。

为了验证这一点,我创建了一个动态索引,但遇到了同样的问题:

Raven Studio filtering with a dynamic index

问题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#class:

  
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"

但这对串行器也没有帮助。

enter image description here

1 个答案:

答案 0 :(得分:2)

你必须告诉Raven,Population是一个数字,因为所有值都存储为文本。 所以在你的index-constructor中编写类似

的东西
Sort(x => x.Population , SortOptions.Long);