Skip to main content

Using 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
moreLikeThisMoreLikeThisBaseDefines the type of MoreLikeThis that should be executed
builderAction<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
documentJsonstringInline JSON document that will be used as a base for operation
predicateExpression<Func<T, bool>>Filtering expression utilized to find a document that will be used as a base for operation
optionsMoreLikeThisOptionsNon-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
MinimumTermFrequencyint?Ignores terms with less than this frequency in the source doc
MaximumQueryTermsint?Returns a query with no more than this many terms
MaximumNumberOfTokensParsedint?The maximum number of tokens to parse in each example doc field that is not stored with TermVector support
MinimumWordLengthint?Ignores words less than this length or, if 0, then this has no effect
MaximumWordLengthint?Ignores words greater than this length or if 0 then this has no effect
MinimumDocumentFrequencyint?Ignores words which do not occur in at least this many documents
MaximumDocumentFrequencyint?Ignores words which occur in more than this many documents
MaximumDocumentFrequencyPercentageint?Ignores words which occur in more than this percentage of documents
Boostbool?Boost terms in query based on score
BoostFactorfloat?Boost factor when boosting based on score
StopWordsDocumentIdstringDocument ID containing custom stop words
Fieldsstring[]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();

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

Remarks

Do not forget to add the following using statement which contains necessary extensions:

using Raven.Client.Documents;