Session: How to Use MoreLikeThis
MoreLikeThis
is available through query extension methods and will return similar documents according to the provided criteria and options.
Syntax
IRavenQueryable<T> MoreLikeThis<T>(MoreLikeThisBase moreLikeThis);
IRavenQueryable<T> MoreLikeThis<T>(Action<IMoreLikeThisBuilder<T>> builder);
Parameters
moreLikeThis
MoreLikeThisBase
Defines the type of MoreLikeThis that should be executed
builder
Action<IMoreLikeThisFactory<T>>
Builder with fluent API that constructs the MoreLikeThisBase
instance
Builder
IMoreLikeThisOperations<T> UsingAnyDocument();
IMoreLikeThisOperations<T> UsingDocument(string documentJson);
IMoreLikeThisOperations<T> UsingDocument(Expression<Func<T, bool>> predicate);
IMoreLikeThisOperations<T> WithOptions(MoreLikeThisOptions options);
Parameters
documentJson
string
Inline JSON document that will be used as a base for operation
predicate
Expression<Func<T, bool>>
Filtering expression utilized to find a document that will be used as a base for operation
options
MoreLikeThisOptions
Non-default options that should be used for operation
Options
public int? MinimumTermFrequency { get; set; } = 2;
public int? MaximumQueryTerms { get; set; } = 25;
public int? MaximumNumberOfTokensParsed { get; set; } = 5000;
public int? MinimumWordLength { get; set; } = 0;
public int? MaximumWordLength { get; set; } = 0;
public int? MinimumDocumentFrequency { get; set; } = 5;
public int? MaximumDocumentFrequency { get; set; } = int.MaxValue;
public int? MaximumDocumentFrequencyPercentage { get; set; }
public bool? Boost { get; set; } = false;
public float? BoostFactor { get; set; } = 1;
public string StopWordsDocumentId { get; set; }
public string[] Fields { get; set; }
Options
MinimumTermFrequency
int?
Ignores terms with less than this frequency in the source doc
MaximumQueryTerms
int?
Returns a query with no more than this many terms
MaximumNumberOfTokensParsed
int?
The maximum number of tokens to parse in each example doc field that is not stored with TermVector support
MinimumWordLength
int?
Ignores words less than this length or, if 0, then this has no effect
MaximumWordLength
int?
Ignores words greater than this length or if 0 then this has no effect
MinimumDocumentFrequency
int?
Ignores words which do not occur in at least this many documents
MaximumDocumentFrequency
int?
Ignores words which occur in more than this many documents
MaximumDocumentFrequencyPercentage
int?
Ignores words which occur in more than this percentage of documents
Boost
bool?
Boost terms in query based on score
BoostFactor
float?
Boost factor when boosting based on score
StopWordsDocumentId
string
Document ID containing custom stop words
Fields
string[]
Fields to compare
Example I
// Search for similar articles to 'articles/1'
// using 'Articles/MoreLikeThis' index and search only field 'Body'
List<Article> articles = session
.Query<Article>("Articles/MoreLikeThis")
.MoreLikeThis(builder => builder
.UsingDocument(x => x.Id == "articles/1")
.WithOptions(new MoreLikeThisOptions
{
Fields = new[] { "Body" }
}))
.ToList();
// Search for similar articles to 'articles/1'
// using 'Articles/MoreLikeThis' index and search only field 'Body'
List<Article> articles = await asyncSession
.Query<Article>("Articles/MoreLikeThis")
.MoreLikeThis(builder => builder
.UsingDocument(x => x.Id == "articles/1")
.WithOptions(new MoreLikeThisOptions
{
Fields = new[] { "Body" }
}))
.ToListAsync();
from index 'Articles/MoreLikeThis'
where morelikethis(id() = 'articles/1', '{ "Fields" : [ "Body" ] }')
Example II
// Search for similar articles to 'articles/1'
// using 'Articles/MoreLikeThis' index and search only field 'Body'
// where article category is 'IT'
List<Article> articles = session
.Query<Article>("Articles/MoreLikeThis")
.MoreLikeThis(builder => builder
.UsingDocument(x => x.Id == "articles/1")
.WithOptions(new MoreLikeThisOptions
{
Fields = new[] { "Body" }
}))
.Where(x => x.Category == "IT")
.ToList();
// Search for similar articles to 'articles/1'
// using 'Articles/MoreLikeThis' index and search only field 'Body'
// where article category is 'IT'
List<Article> articles = await asyncSession
.Query<Article>("Articles/MoreLikeThis")
.MoreLikeThis(builder => builder
.UsingDocument(x => x.Id == "articles/1")
.WithOptions(new MoreLikeThisOptions
{
Fields = new[] { "Body" }
}))
.Where(x => x.Category == "IT")
.ToListAsync();
from index 'Articles/MoreLikeThis'
where morelikethis(id() = 'articles/1', '{ "Fields" : [ "Body" ] }') and Category == 'IT'
Do not forget to add the following using statement which contains necessary extensions:
using Raven.Client.Documents;
Please enable JavaScript to view the comments powered by Disqus.