Last active
December 14, 2015 04:59
-
-
Save robertdean/5031782 to your computer and use it in GitHub Desktop.
Test For MoreLikeThis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment