Lucene.Net: How can I add extra weight to a term?

My index indexes the title and body of the message, but I would like the words contained in the message header to carry more weight and thus float at the top of the results.

How to add excess weight to the title?

+6
source share
1 answer

You can set the field gain during indexing. This assumes that you have data in two different fields. You need to write your own counter if you want to save all the data in one large combined field.

var field = new Field("title", "My title of awesomeness", Field.Store.NO, Field.Index.Analyzed); field.SetBoost(2.0); document.Add(field); 

To perform a search, use BooleanQuery, which searches for both the header and body.

 var queryText = "where my awesomeness"; var titleParser = new QueryParser(Version.LUCENE_29, "title", null); var titleQuery = titleParse.Parse(queryText); var bodyParser = new QueryParser(Version.LUCENE_29, "body", null); var bodyQuery = bodyParser.Parse(queryText); var mergedQuery = new BooleanQuery(); mergedQuery.Add(titleQuery, BooleanClause.Occur.Should); mergedQuery.Add(bodyQuery, BooleanClause.Occur.Should); // TODO: Do search with mergedQuery. 
+7
source share

All Articles