在RavenDB中创建更多这样的内容 [英] Creating more like this in RavenDB

查看:159
本文介绍了在RavenDB中创建更多这样的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的域中有以下文件:

I have these documents in my domain:

public class Article {
    public string Id { get; set; }
    // some other properties
    public IList<string> KeywordIds { get; set; }
}

public class Keyword {
    public string Id { get; set; }
    public string UrlName { get; set; }
    public string Title { get; set; }
    public string Tooltip { get; set; }
    public string Description { get; set; }
}

我有这种情况:

  • 商品A1具有关键字K1
  • 商品A2具有关键字K1
  • 一个用户阅读文章A1
  • 我想建议用户阅读文章A2
  • Article A1 has keyword K1
  • Article A2 has keyword K1
  • One user reads article A1
  • I want to suggest user to read article A2

我知道我可以使用More Like This捆绑软件,并且阅读了文档,但是我不知道该怎么做?你能帮我吗?

I know I can use More Like This bundle and I read the documentation, but I don't know how to do this? Can you help me please?

推荐答案

看看此示例,您可以将流派"替换为关键字":

Take a look at this example, you can substitute "Genres" for your "Keywords":

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using Lucene.Net.Analysis;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Bundles.MoreLikeThis;
using Raven.Client.Indexes;
using Raven.Tests.Helpers;
using Xunit;

namespace RavenDBEval
{
    public class MoreLikeThisEvaluation : RavenTestBase
    {
        private readonly IDocumentStore _store;

        public MoreLikeThisEvaluation()
        {
            _store = (IDocumentStore)NewDocumentStore();
            _store.Initialize();
        }

    [Fact]
    public void ShouldMatchTwoMoviesWithSameCast()
    {
        string id;
        using (var session = _store.OpenSession())
        {
            new MoviesByCastIndex().Execute(_store);
            new MoviesByGenreIndex().Execute(_store);
            GetGenreList().ForEach(session.Store);
            var list = GetMovieList();
            list.ForEach(session.Store);
            session.SaveChanges();
            id = session.Advanced.GetDocumentId(list.First());
            WaitForIndexing(_store);
        }

        using (var session = _store.OpenSession())
        {
            var moreLikeThisByCast = session
                .Advanced
                .MoreLikeThis<Movie, MoviesByCastIndex>(new MoreLikeThisQuery
                                                     {
                                                         DocumentId = id, 
                                                         Fields = new[] { "Cast" },
                                                         MinimumTermFrequency = 1,
                                                         MinimumDocumentFrequency = 2
                                                     });
            var moreLikeThisByGenre = session
                .Advanced
                .MoreLikeThis<Movie, MoviesByGenreIndex>(new MoreLikeThisQuery
                {
                    DocumentId = id,
                    Fields = new[] { "Genres" },
                    MinimumTermFrequency = 1,
                    MinimumDocumentFrequency = 2
                });

            foreach (var movie in moreLikeThisByCast)
            {
                Debug.Print("{0}, Cast={1}", movie.Title, string.Join(",", movie.Cast));
            }

            Assert.NotEmpty(moreLikeThisByCast);

            foreach (var movie in moreLikeThisByGenre)
            {
                Debug.Print("{0}", movie.Title);
                foreach (var genreId in movie.Genres)
                {
                    var genre = session.Load<Genre>(genreId);
                    Debug.Print("\t\t{0}", genre.Name);
                }
            }
            Assert.NotEmpty(moreLikeThisByGenre);

        }
    }

    private static List<Genre> GetGenreList()
    {
        return new List<Genre>
                   {
                       new Genre {Id = "genres/1", Name = "Comedy"},
                       new Genre {Id = "genres/2", Name = "Drama"},
                       new Genre {Id = "genres/3", Name = "Action"},
                       new Genre {Id = "genres/4", Name = "Sci Fi"},
                   };
    } 

    private static List<Movie> GetMovieList()
    {
        return new List<Movie>
                   {
                       new Movie
                           {
                               Title = "Star Wars Episode IV: A New Hope",
                               Genres = new[] {"genres/3", "genres/4"},
                               Cast = new[]
                                          {
                                              "Mark Hamill",
                                              "Harrison Ford",
                                              "Carrie Fisher"
                                          }
                           },
                       new Movie
                           {
                               Title = "Star Wars Episode V: The Empire Strikes Back",
                               Genres = new[] {"genres/3", "genres/4"},
                               Cast = new[]
                                          {
                                              "Mark Hamill",
                                              "Harrison Ford",
                                              "Carrie Fisher"
                                          }
                           },
                       new Movie
                           {
                               Title = "Some Fake Movie",
                               Genres = new[] {"genres/2"},
                               Cast = new[]
                                          {
                                              "James Franco",
                                              "Sting",
                                              "Carrie Fisher"
                                          }
                           },
                       new Movie
                           {
                               Title = "The Conversation",
                               Genres = new[] {"genres/2"},
                               Cast =
                                   new[]
                                       {
                                           "Gene Hackman",
                                           "John Cazale",
                                           "Allen Garfield",
                                           "Harrison Ford"
                                       }
                           },
                       new Movie
                           {
                               Title = "Animal House",
                               Genres = new[] {"genres/1"},
                               Cast = new[]
                                          {
                                              "John Belushi",
                                              "Karen Allen",
                                              "Tom Hulce"
                                          }
                           },
                       new Movie
                           {
                               Title="Superman",
                               Genres = new[] {"genres/3", "genres/4"},
                               Cast= new[]
                                         {
                                             "Christopher Reeve", 
                                             "Margot Kidder", 
                                             "Gene Hackman",
                                             "Glen Ford"
                                         }
                           }
                   };
    }
}

public class Movie
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string[] Cast { get; set; }
    public string[] Genres { get; set; }
}

public class Genre
{
    public string Id { get; set; }
    public string Name { get; set; }
}

public class MoviesByGenreIndex : AbstractIndexCreationTask<Movie>
{
    public MoviesByGenreIndex()
    {
        Map = docs => from doc in docs
                      select new { doc.Genres };

        Analyzers = new Dictionary<Expression<Func<Movie, object>>, string>
                        {
                            {
                                x => x.Genres,
                                typeof (KeywordAnalyzer).FullName
                                }
                        };

        Stores = new Dictionary<Expression<Func<Movie, object>>, FieldStorage>
                     {
                         {
                             x => x.Genres, FieldStorage.Yes
                         }
                     };
    }
}

public class MoviesByCastIndex : AbstractIndexCreationTask<Movie>
{
    public MoviesByCastIndex()
    {
        Map = docs => from doc in docs
                      select new { doc.Cast };

        Analyzers = new Dictionary<Expression<Func<Movie, object>>, string>
                        {
                            {
                                x => x.Cast,
                                typeof (KeywordAnalyzer).FullName
                                }
                        };

        Stores = new Dictionary<Expression<Func<Movie, object>>, FieldStorage>
                     {
                         {
                             x => x.Cast, FieldStorage.Yes
                         }
                     };
    }
}

}

输出:

-《星球大战》第5集:帝国反击,演员=马克·哈米尔,哈里森·福特,凯莉·费舍尔 -某些假电影,演员=詹姆斯·佛朗哥,斯廷,凯莉·费舍尔 -The Conversation,Cast = Gene Hackman,John Cazale,Allen Garfield,Harrison Ford 按类型: 星球大战第5集:帝国反击 行动 科幻 -超人 行动 科幻片

-Star Wars Episode V: The Empire Strikes Back, Cast=Mark Hamill,Harrison Ford,Carrie Fisher -Some Fake Movie, Cast=James Franco,Sting,Carrie Fisher -The Conversation, Cast=Gene Hackman,John Cazale,Allen Garfield,Harrison Ford By Genre: -Star Wars Episode V: The Empire Strikes Back Action Sci Fi -Superman Action Sci Fi

注意nuget包:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Lucene.Net" version="3.0.3" targetFramework="net40" />
  <package id="Lucene.Net.Contrib" version="3.0.3" targetFramework="net40" />
  <package id="RavenDB.Client" version="2.0.2261" targetFramework="net40" />
  <package id="RavenDB.Database" version="2.0.2261" targetFramework="net40" />
  <package id="RavenDB.Embedded" version="2.0.2261" targetFramework="net40" />
  <package id="RavenDB.Tests.Helpers" version="2.0.2261" targetFramework="net40" />
  <package id="SharpZipLib" version="0.86.0" targetFramework="net40" />
  <package id="xunit" version="1.9.1" targetFramework="net40" />
</packages>

这篇关于在RavenDB中创建更多这样的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆