Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Take a look at this example, you can substitute "Genres" for your "Keywords":</p> <pre><code>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&lt;Movie, MoviesByCastIndex&gt;(new MoreLikeThisQuery { DocumentId = id, Fields = new[] { "Cast" }, MinimumTermFrequency = 1, MinimumDocumentFrequency = 2 }); var moreLikeThisByGenre = session .Advanced .MoreLikeThis&lt;Movie, MoviesByGenreIndex&gt;(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&lt;Genre&gt;(genreId); Debug.Print("\t\t{0}", genre.Name); } } Assert.NotEmpty(moreLikeThisByGenre); } } private static List&lt;Genre&gt; GetGenreList() { return new List&lt;Genre&gt; { 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&lt;Movie&gt; GetMovieList() { return new List&lt;Movie&gt; { 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&lt;Movie&gt; { public MoviesByGenreIndex() { Map = docs =&gt; from doc in docs select new { doc.Genres }; Analyzers = new Dictionary&lt;Expression&lt;Func&lt;Movie, object&gt;&gt;, string&gt; { { x =&gt; x.Genres, typeof (KeywordAnalyzer).FullName } }; Stores = new Dictionary&lt;Expression&lt;Func&lt;Movie, object&gt;&gt;, FieldStorage&gt; { { x =&gt; x.Genres, FieldStorage.Yes } }; } } public class MoviesByCastIndex : AbstractIndexCreationTask&lt;Movie&gt; { public MoviesByCastIndex() { Map = docs =&gt; from doc in docs select new { doc.Cast }; Analyzers = new Dictionary&lt;Expression&lt;Func&lt;Movie, object&gt;&gt;, string&gt; { { x =&gt; x.Cast, typeof (KeywordAnalyzer).FullName } }; Stores = new Dictionary&lt;Expression&lt;Func&lt;Movie, object&gt;&gt;, FieldStorage&gt; { { x =&gt; x.Cast, FieldStorage.Yes } }; } } </code></pre> <p>}</p> <p>Output:</p> <p>-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</p> <p>Take note of the nuget packages:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;packages&gt; &lt;package id="Lucene.Net" version="3.0.3" targetFramework="net40" /&gt; &lt;package id="Lucene.Net.Contrib" version="3.0.3" targetFramework="net40" /&gt; &lt;package id="RavenDB.Client" version="2.0.2261" targetFramework="net40" /&gt; &lt;package id="RavenDB.Database" version="2.0.2261" targetFramework="net40" /&gt; &lt;package id="RavenDB.Embedded" version="2.0.2261" targetFramework="net40" /&gt; &lt;package id="RavenDB.Tests.Helpers" version="2.0.2261" targetFramework="net40" /&gt; &lt;package id="SharpZipLib" version="0.86.0" targetFramework="net40" /&gt; &lt;package id="xunit" version="1.9.1" targetFramework="net40" /&gt; &lt;/packages&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload