RavenDb学习(三)静态索引

在静态索引这块,RavenDb其实的是lucene,所以里面有很多概念,其实都是lucene本身的。

1.定义静态Indexes
documentStore.DatabaseCommands.PutIndex(
    "BlogPosts/PostsCountByTag",
    new IndexDefinitionBuilder<BlogPost, BlogTagPostsCount>
    {
        // The Map function: for each tag of each post, create a new BlogTagPostsCount
        // object with the name of a tag and a count of one.
        Map = posts => from post in posts
                       from tag in post.Tags
                       select new
                       {
                           Tag = tag,
                           Count = 1
                       },
 
        // The Reduce function: group all the BlogTagPostsCount objects we got back
        // from the Map function, use the Tag name as the key, and sum up all the
        // counts. Since the Map function gives each tag a Count of 1, when the Reduce
        // function returns we are going to have the correct Count of posts filed under
        // each tag.
        Reduce = results => from result in results
                            group result by result.Tag
                                into g
                                select new
                                {
                                    Tag = g.Key,
                                    Count = g.Sum(x => x.Count)
                                }
    });
public class BlogTagPostsCount
{
    public string Tag { get; set; }
    public int Count { get; set; }
}

2.索引层次化的数据
如下图中的数据,如果我们要索引Comments的话,应该如何索引
{  //posts/123
  'Name': 'Hello Raven',
  'Comments': [
    {
      'Author': 'Ayende',
      'Text': '...',
      'Comments': [
        {
          'Author': 'Rahien',
          'Text': '...',
          "Comments": []
        }
      ]
    }
  ]
}

store.DatabaseCommands.PutIndex("SampleRecurseIndex", new IndexDefinition
{
    Map = @"from post in docs.Posts
            from comment in Recurse(post, (Func<dynamic, dynamic>)(x => x.Comments))
            select new
            {
                Author = comment.Author,
                Text = comment.Text
            }"
});

当然我们也可以定义一个类
public class SampleRecurseIndex : AbstractIndexCreationTask<Post>
{
    public SampleRecurseIndex()
    {
        Map = posts => from post in posts
                       from comment in Recurse(post, x => x.Comments)
                       select new
                       {
                           Author = comment.Author,
                           Text = comment.Text
                       };
    }
}

然后创建new SampleRecurseIndex().Execute(store);

3.索引相关文档

1)第一个例子

这个例子:Invoice和Customer,Invoice当中包含了Customer的Id ,现在我们要通过Customer的姓名来查询invoices
public class Invoice
{
    public string Id { get; set; }
 
    public string CustomerId { get; set; }
}
 
public class Customer
{
    public string Id { get; set; }
 
    public string Name { get; set; }
}

public class SampleIndex : AbstractIndexCreationTask<Invoice>
{
    public SampleIndex()
    {
        Map = invoices => from invoice in invoices
                          select new
                          {
                              CustomerId = invoice.CustomerId,
                              CustomerName = LoadDocument<Customer>(invoice.CustomerId).Name
                          };
    }
}

建立完索引之后,我们就可以客户的名称来查询invoices了 

2)第二个例子
public class Book
{
    public string Id { get; set; }
     
    public string Name { get; set; }
}
 
public class Author
{
    public string Id { get; set; }
 
    public string Name { get; set; }
 
    public IList<string> BookIds { get; set; }
}

public class AnotherIndex : AbstractIndexCreationTask<Author>
{
    public AnotherIndex()
    {
        Map = authors => from author in authors
                         select new
                             {
                                 Name = author.Name,
                                 Books = author.BookIds.Select(x => LoadDocument<Book>(x).Name)
                             };
    }
}

Author当中保存了所有的书的id,通过作者可以查询他出了多少书,通过书名页可以查到作者

这里面需要注意的是:
1)当相关文档变化的时候,索引也会变化
2)使用LoadDocument 去跟踪一个文档,当多个文档跟踪同一个文档的时候,这会变成一个很耗费资源的开销

4.TransformResults 
有时候索引非常复杂,但是我们需要的数据比较简单,这个时候我们需要怎么做呢?
public class PurchaseHistoryIndex : AbstractIndexCreationTask<Order, Order>
{
    public PurchaseHistoryIndex()
    {
        Map = orders => from order in orders
                        from item in order.Items
                        select new
                        {
                            UserId = order.UserId,
                            ProductId = item.Id
                        };
 
        TransformResults = (database, orders) =>
                           from order in orders
                           from item in order.Items
                           let product = database.Load<Product>(item.Id)
                           where product != null
                           select new
                           {
                               ProductId = item.Id,
                               ProductName = product.Name
                           };
    }
}

我们在查询的时候只需要PurchaseHistoryViewItem,这样子我们就用OfType来进行类型转换。
documentSession.Query<Shipment, PurchaseHistoryIndex>()
    .Where(x => x.UserId == userId)
    .OfType<PurchaseHistoryViewItem>()
    .ToArray();

5.错误处理
当索引出现错误的时候,因为它是由一个后台线程执行的,索引我们很难发现的,通过查看'/stats'表或者 '/raven/studio.html#/statistics'或者'/raven/statistics.html'。
当错误超过15%的时候,索引就会被禁用掉,15%的数量是在前10个文档之后统计的,为了防止一开始的文旦就不好使,就别禁用了。
下面是错误的一些信息,查看'/stats'得到的
{
    "LastDocEtag": "00000000-0000-0b00-0000-000000000001",
    "LastAttachmentEtag": "00000000-0000-0000-0000-000000000000",
    "CountOfIndexes": 1,
    "ApproximateTaskCount": 0,
    "CountOfDocuments": 1,
    "StaleIndexes": [],
    "CurrentNumberOfItemsToIndexInSingleBatch": 512,
    "CurrentNumberOfItemsToReduceInSingleBatch": 256,
    "Indexes":[
        {
            "Name": "PostsByTitle",
            "IndexingAttempts": 1,
            "IndexingSuccesses": 0,
            "IndexingErrors": 1
        }
    ],
    "Errors":[
        {
            "Index": "PostsByTitle",
            "Error": "Cannot   perform   runtime   binding   on   a   null   reference",
            "Timestamp": "\/Date(1271778107096+0300)\/",
            "Document": "bob"
        }
    ]
}

6.查询

在查询当中用 string.Contains()方式是会报错的,因为RavenDb不支持类似通配符*term*这样的方式,这样会引起性能问题,它会抛出NotSupportedException异常。

1)多字段索引
documentStore.DatabaseCommands.PutIndex("UsersByNameAndHobbies", new IndexDefinition
{
    Map = "from user in docs.Users select new { user.Name, user.Hobbies }",
    Indexes = { { "Name", FieldIndexing.Analyzed }, { "Hobbies", FieldIndexing.Analyzed } }
});

2)多字段查询
users = session.Query<User>("UsersByNameAndHobbies")
               .Search(x => x.Name, "Adam")
               .Search(x => x.Hobbies, "sport").ToList();

3)相关性加速
通过设置相关性字段,可以减少一些不相关的内容搜索
users = session.Query<User>("UsersByHobbies")
               .Search(x => x.Hobbies, "I love sport", boost:10)
               .Search(x => x.Hobbies, "but also like reading books", boost:5).ToList();

也可以在索引定义时候设定
public class Users_ByName : AbstractIndexCreationTask<User>
{
    public Users_ByName()
    {
        this.Map = users => from user in users
                            select new
                                {
                                    FirstName = user.FirstName.Boost(10),
                                    LastName = user.LastName
                                };
    }
}

4)操作符
AND操作符
users = session.Query<User>("UsersByNameAndHobbiesAndAge")
               .Search(x => x.Hobbies, "computers")
               .Search(x => x.Name, "James")
               .Where(x => x.Age == 20).ToList();

上面的这一句也可以这么写
users = session.Query<User>("UsersByNameAndHobbies")
               .Search(x => x.Name, "Adam")
               .Search(x => x.Hobbies, "sport", options: SearchOptions.And).ToList();

NOT操作符
users = session.Query<User>("UsersByName")
        .Search(x => x.Name, "James", options: SearchOptions.Not).ToList();

多操作符合作
并且不等于
users = session.Query<User>("UsersByNameAndHobbies")
        .Search(x => x.Name, "Adam")
        .Search(x => x.Hobbies, "sport", options: SearchOptions.Not | SearchOptions.And)
        .ToList();

5)通配符,模糊查询
EscapeAll (default),
AllowPostfixWildcard,
AllowAllWildcards,
RawQuery.
users = session.Query<User>("UsersByName")
    .Search(x => x.Name, "Jo* Ad*",
            escapeQueryOptions:EscapeQueryOptions.AllowPostfixWildcard).ToList();

users = session.Query<User>("UsersByName")
    .Search(x => x.Name, "*oh* *da*",
            escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards).ToList();

users = session.Query<User>("UsersByName")
    .Search(x => x.Name, "*J?n*",
            escapeQueryOptions: EscapeQueryOptions.RawQuery).ToList();

6)高亮显示

public class SearchItem
{
    public string Id { get; set; }
 
    public string Text { get; set; }
}
 
public class ContentSearchIndex : AbstractIndexCreationTask<SearchItem>
{
    public ContentSearchIndex()
    {
        Map = (docs => from doc in docs
                       select new { doc.Text });
 
        Index(x => x.Text, FieldIndexing.Analyzed);
        Store(x => x.Text, FieldStorage.Yes);
        TermVector(x => x.Text, FieldTermVector.WithPositionsAndOffsets);
    }
}
//查询完毕之后进行处理
FieldHighlightings highlightings;
var results = session.Advanced.LuceneQuery<SearchItem>("ContentSearchIndex")
                 .Highlight("Text", 128, 1, out highlightings)
                 .Search("Text", "raven")
                 .ToArray();
 
var builder = new StringBuilder()
    .AppendLine("<ul>");
 
foreach (var result in results)
{
    var fragments = highlightings.GetFragments(result.Id);
    builder.AppendLine(string.Format("<li>{0}</li>", fragments.First()));
}
 
var ul = builder
    .AppendLine("</ul>")
    .ToString();

//查询时候设置前后符号
FieldHighlightings highlightings;
var results = session.Advanced.LuceneQuery<SearchItem>("ContentSearchIndex")
                 .Highlight("Text", 128, 1, out highlightings)
                 .SetHighlighterTags("**", "**")
                 .Search("Text", "raven")
                 .ToArray();

7)推荐

下面是用户和基于用户名的索引
public class User
{
    public string Id { get; set; }
    public string FullName { get; set; }
}

public class Users_ByFullName : AbstractIndexCreationTask<User>
{
    public Users_ByFullName()
    {
        Map = users => from user in users
                       select new { user.FullName };
 
        Indexes.Add(x => x.FullName, FieldIndexing.Analyzed);
    }
}

假设数据库里面存着以下数据:
// users/1
{
    "Name": "John Smith"
}
// users/2
{
    "Name": "Jack Johnson"
}
// users/3
{
    "Name": "Robery Jones"
}
// users/4
{
    "Name": "David Jones"
}

你使用了以下的查询语句
var query = session.Query<User, Users_ByFullName>().Where(x => x.FullName == "johne");
var user = query.FirstOrDefault();

如果查询不到,可以使用推荐功能
if (user == null)
{
    SuggestionQueryResult suggestionResult = query.Suggest();
 
    Console.WriteLine("Did you mean?");
 
    foreach (var suggestion in suggestionResult.Suggestions)
    {
        Console.WriteLine("\t{0}", suggestion);
    }
}

它会给你推荐
 john
 jones
 johnson
下面是包括全部参数的查询:
session.Query<User, Users_ByFullName>()
       .Suggest(new SuggestionQuery()
                    {
                        Field = "FullName",
                        Term = "johne",
                        Accuracy = 0.4f,
                        MaxSuggestions = 5,
                        Distance = StringDistanceTypes.JaroWinkler,
                        Popularity = true,
                    });
另外一种查询方式:
store.DatabaseCommands.Suggest("Users/ByFullName", new SuggestionQuery()
                                                   {
                                                       Field = "FullName",
                                                       Term = "johne"
                                                   });

多个关键词的推荐:
同时输入johne davi
SuggestionQueryResult resultsByMultipleWords = session.Query<User, Users_ByFullName>()
       .Suggest(new SuggestionQuery()
       {
           Field = "FullName",
           Term = "<<johne davi>>",
           Accuracy = 0.4f,
           MaxSuggestions = 5,
           Distance = StringDistanceTypes.JaroWinkler,
           Popularity = true,
       });
 
Console.WriteLine("Did you mean?");
 
foreach (var suggestion in resultsByMultipleWords.Suggestions)
{
    Console.WriteLine("\t{0}", suggestion);
}

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏IT开发技术与工作效率

Hibernate一对多保存思考题 Java框架

1003
来自专栏智能大石头

老瓶装新酒 - C#调用WM手机发送短信(源码)

一些系统,需要能够发送短信,量很小,平均每日10条。 运营商平台太贵,白名单很严格,小额只能发省内; 各短信平台有各种限制,大事件前后会关闭; 飞信以前可以用W...

2455
来自专栏大内老A

开发自己的Data Access Application Block[上篇]

经常在网上看到对ORM的讨论沸沸扬扬,我也来凑个热闹,谈谈我写的一个ORM。最近在做一项工作,把我们经常用到的一些业务逻辑抽象出来,写成一个个的Applicat...

1928
来自专栏程序员的SOD蜜

PDF.NET数据开发框架实体类操作实例

本篇是 PDF.NET数据开发框架实体类操作实例(MySQL) 的姊妹篇,两者使用了同一个测试程序,不同的只是使用的类库和数据库不同,下面说说具体的使用过程。 ...

2266
来自专栏菩提树下的杨过

silverlight + wcf(json格式) + sqlserver存储过程分页

silverlight并没有提供现成的分页控件,百度了一圈,也没有发现aspx中好用的类似AspNetPager成熟控件,网上现有的一些分页代码,很多也是基于1...

2497
来自专栏更流畅、简洁的软件开发方式

我的数据访问类(第二版)—— for .net2.0 (二)

下面写一下相对来说不变的地方 SQL语句部分,改成了静态函数的形式。 using System; using System.Collections.Generi...

1548
来自专栏木宛城主

工欲善其事,必先利其器:分享一套Code Smith 搭建N层架构模板

 开篇 平常开发时,由于冗余代码过多,程序员做重复的工作过多势必会影响开发效率。倘若对重复性代码简单的复制、粘贴,虽然也能节省时间,但也需仔细一步步替换,这无...

1968
来自专栏大内老A

Enterprise Library Policy Injection Application Block 之三:PIAB的扩展—创建自定义CallHandler(提供Source Code下载)

本系列的第一部分对PIAB使用场景进行了简单的介绍,作中阐述了通过PI(Policy Injection)的方式实现了Business Logic和Non-Bu...

34610
来自专栏ASP.NET MVC5 后台权限管理系统

MVC解决Json DataGrid返回的日期格式是/Date(20130450000365)

实际上是Json格式化问题,我们应该在返回json的时候进行格式化,我们需要重写系统的JsonResult类 using System; using Syste...

26110
来自专栏JadePeng的技术博客

使用Mongodb 做对象缓存

mongodb高效的访问速度,用来快速存取数据再合适不过了,缓存神马的,可以用这个的 另外,有的时候,如果仅仅存储几条数据,单独去建立一张表代价太大,这个时候,...

4995

扫码关注云+社区

领取腾讯云代金券