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

时间:2022-04-29
本文章向大家介绍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
      }
   ]
}