开发者

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

My indexer indexes the title and the body of a post, but I'd like the words contained in the title of the post to carry more weight, and thus floa开发者_Python百科t to the top of the results.

How can I add extra weight to the title words?


You can set a field-boost during indexing. This assumes that you have your data in two different fields. You need to write a custom scorer if you want to store all data in one big merged field.

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

To search, use a BooleanQuery that searches both title and body.

var queryText = "where's 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜