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);
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。