Using Lucene boosting in custom smart search index

Nat Tam asked on May 12, 2015 17:08

Watching Miro's video on custom search indexes, around 40 minutes in he uses SearchHelper.CreateDocument and it returns a Lucene Document object.

I’m trying to re-create this. The key thing for me is gaining access to the boost properties of Lucene documents and fields. For example, as demonstrated here. This is important because I want the presence of a search term in certain fields to be deemed more relevant than in other fields. For example, if the search term is present in the page name this is clearly a more important result than if the term is elsewhere on the page.

However, in my own tests, when I use SearchHelper.CreateDocument I get a CMS.DataEngine.ISearchDocument object and not a Lucene Document. I assume this is a change in Kentico 7 or 8. From what I can tell this is a simplified abstraction of the Lucene Document and does not offer any means of boosting particular fields.

Is boosting simply no longer possible with later versions of Kentico? This concerns me because it leaves me with very little control. I've seen suggestions that I would have to use Lucene's query parser syntax (e.g. this post) but I would much prefer if this was configured as part of the index as is traditional with Lucene.

Advice much appreciated.

Correct Answer

Rui Wang answered on May 13, 2015 20:08

You can cast the ISearchDocument to LuceneSearchDocument which comes from CMS.Search.Lucene or from CMS.Search.Lucene3, depending on the version you're using. in lucene 3, the LuceneSearchDocument has a property Boost, which can be set.

using CMS.Search.Lucene3;

    ISearchDocument doc = SearchHelper.CreateDocument(documentParameters);
    var ldoc = doc as LuceneSearchDocument;
    ldoc.Document.Boost = 2.0f;
1 votesVote for this answer Unmark Correct answer

Recent Answers

Rui Wang answered on May 12, 2015 21:58

It maybe this API update

Class method

Lucene.Net.Documents.Document CMS.SiteProvider.SearchHelper.CreateDocument(System.String, System.String, System.String, System.DateTime, System.String)

was removed. You can use

method CMS.Search.SearchHelper.CreateDocument(CMS.Search.SearchDocumentParameters)


0 votesVote for this answer Mark as a Correct answer

Nat Tam answered on May 13, 2015 16:13

Thanks Rui. But as I mentioned CMS.Search.SearchHelper.CreateDocument(CMS.Search.SearchDocumentParameters) returns an CMS.DataEngine.ISearchDocument object which is not a Lucene type and doesn't, as far as I can tell, expose any Boost properties.

0 votesVote for this answer Mark as a Correct answer

Nat Tam answered on May 14, 2015 10:21

Rui, thanks again. This is really helpful. However, the assembly CMS.Search.Lucene3 in my Kentico 8.2 Lib directory does not contain a LuceneSearchDocument class. What am I missing here? I only see the 4 classes in the screenshot below...


0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on May 14, 2015 13:58

Are you using v8.2 with out any hotfixes? It was missing in 8.2 but was fixed in 8.2.1.

Search - LuceneSearchDocument class made public
The 'LuceneSearchDocument' class was made public to support advanced search customization scenarios.

You can apply the latest hotfix

1 votesVote for this answer Mark as a Correct answer

Nat Tam answered on May 14, 2015 15:02

You're absolutely right. Thank you so much Rui, the hotfix has given me access to that class.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.