You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: How to use MoreLikeThis?

MoreLikeThis is available through extension method in Advanced session operations. This method returns articles similar to the provided input.

Syntax

TResult[] MoreLikeThis<TResult, TIndexCreator>(
	this ISyncAdvancedSessionOperation advancedSession,
	MoreLikeThisQuery parameters) 
	where TIndexCreator : AbstractIndexCreationTask, new() { ... }

TResult[] MoreLikeThis<TResult>(
	this ISyncAdvancedSessionOperation advancedSession,
	string index,
	string documentId) { ... }

TResult[] MoreLikeThis<TTransformer, TResult, TIndexCreator>(
	this ISyncAdvancedSessionOperation advancedSession,
	string documentId)
	where TIndexCreator : AbstractIndexCreationTask, new()
	where TTransformer : AbstractTransformerCreationTask, new() { ... }

Parameters
index string Name of an index on which query will be executed.
documentId string Id of a document for which similarities will be searched.
parameters MoreLikeThisQuery A more like this query definition that will be executed.
Return Value
TResult[] Array of similar documents returned as entities.

Example I

// Search for similar articles to 'articles/1'
// using 'Articles/MoreLikeThis' index and search only field 'Body'
Article[] articles = session
	.Advanced
	.MoreLikeThis<Article>(
		"Articles/MoreLikeThis",
		null,
		new MoreLikeThisQuery
		{
			IndexName = "Articles/MoreLikeThis", 
			DocumentId = "articles/1", 
			Fields = new[] { "Body" }
		});

Example II

// Search for similar articles to 'articles/1'
// using 'Articles/MoreLikeThis' index and search only field 'Body'
// where article category is 'IT'
Article[] articles = session
	.Advanced
	.MoreLikeThis<Article>(
		"Articles/MoreLikeThis",
		null,
		new MoreLikeThisQuery
		{
			IndexName = "Articles/MoreLikeThis",
			DocumentId = "articles/1",
			Fields = new[] { "Body" },
			AdditionalQuery = "Category:IT"
		});

Remarks

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

using Raven.Abstractions.Data;
using Raven.Client.Bundles.MoreLikeThis;
using Raven.Client.Document;