Sitecore 9
have OOTB extention GetMoreLikeThis for IProviderSearchContext
. To use it you need to install SolrNet
nuget package. You can use it like following:
var contextIndex = _contextRepository.GetSearchIndexContext("search_index");
using (var context = contextIndex.CreateSearchContext())
{
var parameters = new MoreLikeThisHandlerParameters(new[] {"field1", "field2"})
{
MinDocFreq = 1, // minimum document frequency
MinTermFreq = 1, // minimum term frequency
};
var query = SolrMLTQuery.FromQuery(new SolrQuery("search_term"));
var queryOption = new MoreLikeThisHandlerQueryOptions(parameters);
var results = context.GetMoreLikeThis<SearchResultItem>(query, queryOption);
var interestingTerms = results.InterestingTerms;
}
Another way, you can use native SorlNet
query:
var contextIndex = _contextRepository.GetSearchIndexContext("search_index");
using (var context = contextIndex.CreateSearchContext())
{
var results = context.Query<SearchResultItem>(new SolrQuery("search_term"), new QueryOptions
{
MoreLikeThis = new MoreLikeThisParameters(new[] { "field1", "field2" })
{
MinDocFreq = 1, // minimum document frequency
MinTermFreq = 1, // minimum term frequency
},
});
var similarResults = results.SimilarResults;
}