RavenDb学习(七) 异步工作以及维度查询

1、异步执行

var entity = new Company {Name = "Async Company #2", Id = "companies/2"};
using (var session = documentStore.OpenAsyncSession())
{
    var company = await session.LoadAsync<Company>(1); // loading an entity asynchronously
 
    await session.StoreAsync(entity); // in-memory operations are committed asynchronously when calling SaveChangesAsync
    await session.SaveChangesAsync(); // returns a task that completes asynchronously
 
    var query = session.Query<Company>()
                       .Where(x => x.Name == "Async Company #1")
                       .ToListAsync(); // returns a task that will execute the query
}

2、维度查询

假设我们有一个这样的文档:
 {
    DateOfListing: "2000-09-01T00:00:00.0000000+01:00"
    Manufacturer: "Jessops"
    Model: "blah"
    Cost: 717.502206059872
    Zoom: 9
    Megapixels: 10.4508949012733
    ImageStabiliser: false
}

1)建立基于制造厂商,成本和像素的维度

var _facets = new List<Facet>
              {
                  new Facet
                      {
                          Name = "Manufacturer"
                      },
                  new Facet
                      {
                          Name = "Cost_Range",
                          Mode = FacetMode.Ranges,
                          Ranges =
                              {
                                  "[NULL TO Dx200.0]",
                                  "[Dx200.0 TO Dx400.0]",
                                  "[Dx400.0 TO Dx600.0]",
                                  "[Dx600.0 TO Dx800.0]",
                                  "[Dx800.0 TO NULL]",
                              }
                      },
                  new Facet
                      {
                          Name = "Megapixels_Range",
                          Mode = FacetMode.Ranges,
                          Ranges =
                              {
                                  "[NULL TO Dx3.0]",
                                  "[Dx3.0 TO Dx7.0]",
                                  "[Dx7.0 TO Dx10.0]",
                                  "[Dx10.0 TO NULL]",
                              }
                      }
              };


成本字段范围
Cost <= 200.0
200.0 <= Cost <= 400.0
400.0 <= Cost <= 600.0
600.0 <= Cost <= 800.0
Cost >= 800.0
像素字段范围
Megapixels <= 3.0
3.0 <= Megapixels <= 7.0
7.0 <= Megapixels <= 10.0
Megapixels >= 10.0

//保存
session.Store(new FacetSetup { Id = "facets/CameraFacets", Facets = _facets });

2、创建索引

store.DatabaseCommands.PutIndex("CameraCost",
            new IndexDefinition
            {
                Map = @"from camera in docs
                                select new
                                {
                                    camera.Manufacturer,
                                    camera.Model,
                                    camera.Cost,
                                    camera.DateOfListing,
                                    camera.Megapixels
                                }"
            });

3、查询
var facetResults = session.Query<Camera>("CameraCost")
    .Where(x => x.Cost >= 100 && x.Cost <= 300)
    .ToFacets("facets/CameraFacets");

通过通过这个网址查询:
http://localhost:8080/facets/CameraCost?facetDoc=facets/CameraFacets&query=Cost_Range:[Dx100 TO Dx300.0]


结果如下:
 {
   Manufacturer: [
      {
         Range: 'canon',
         Count: 42
      },
      {
         Range: 'jessops',
         Count: 50
      },
      {
         Range: 'nikon',
         Count: 46
      },
      {
         Range: 'phillips',
         Count: 44
      },
      {
         Range: 'sony',
         Count: 35
      }
   ],
   Cost_Range: [
      {
         Range: '[NULL TO Dx200.0]',
         Count: 115
      },
      {
         Range: '[Dx200.0 TO Dx400.0]',
         Count: 102
      }
   ],
   Megapixels_Range: [
      {
         Range: '[NULL TO Dx3.0]',
         Count: 42
      },
      {
         Range: '[Dx3.0 TO Dx7.0]',
         Count: 79
      },
      {
         Range: '[Dx7.0 TO Dx10.0]',
         Count: 82
      },
      {
         Range: '[Dx10.0 TO NULL]',
         Count: 14
      }
   ]
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏技术之路

WPF MVVM实现TreeView

今天有点时间,做个小例子WPF MVVM 实现TreeView 只是一个思路大家可以自由扩展 文章最后给出了源码下载地址 图1 ?    图2     ? 模...

2639
来自专栏上善若水

CG008收费软件库Asprise ocr库识别图片验证码

1313
来自专栏马洪彪

IniHelper——INI操作辅助类

使用INI配置文件,简单便捷。 该辅助工具类为C#操作INI文件的辅助类,源码在某位师傅的基础上完善的来,因为忘记最初的来源了,因此不能提及引用,在此深感遗憾,...

4107
来自专栏racaljk

2018第九届蓝桥杯决赛(C++ B组)

x星球的钞票的面额只有:100元,5元,2元,1元,共4种。 小明去x星旅游,他手里只有2张100元的x星币,太不方便,恰好路过x星银行就去换零钱。 小明有...

7873
来自专栏jeremy的技术点滴

python开发小技巧

3094
来自专栏HansBug's Lab

3891: [Usaco2014 Dec]Piggy Back

3891: [Usaco2014 Dec]Piggy Back Time Limit: 10 Sec  Memory Limit: 128 MB Submit:...

3719
来自专栏xingoo, 一个梦想做发明家的程序员

Spark MLlib 之 aggregate和treeAggregate从原理到应用

由于treeAggregate是在aggregate基础上的优化版本,因此先来看看aggregate是什么.

1300
来自专栏木宛城主

Thinking In Design Pattern——Query Object模式

什么是Query Object模式 Query Object的架构设计 Query Object在服务层的应用 测试 Query Obj...

2186
来自专栏马洪彪

使用NOPI读取Word、Excel文档内容

使用NOPI读取Excel的例子很多,读取Word的例子不多。 Excel的解析方式有多中,可以使用ODBC查询,把Excel作为一个数据集对待。也可以使用文档...

4636
来自专栏数据结构与算法

洛谷P2925 [USACO08DEC]干草出售Hay For Sale

题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate ...

2974

扫码关注云+社区

领取腾讯云代金券