c# - Lucene boosting / scoring with multiple columns -
how take following data , able results below?
ive included code sample cant seem figure out how search through multiple columns , apply boost need.
am going right way?
boost / weight each column
first name = 100 last name = 75 bio = 50
data
first name, last name, biobenny, benson, test - "ben" appears in first name , last name - score = 175 jim, smith, test word ben - "ben" appears in bio - score = 50 john, benson, , test here - "ben" appears in last name - score = 75
results
1. benny 2. john 3. jim protected override void _addtoluceneindex(dynamic item, indexwriter writer) { var user = item usertestitem; if (user == null) return; // remove older index entry var searchquery = new termquery(new term(user_id, user.userid.tostring(cultureinfo.invariantculture))); writer.deletedocuments(searchquery); // add new index entry var doc = new document(); // fields var userid = new field(user_id, user.userid.tostring(cultureinfo.invariantculture), field.store.yes, field.index.not_analyzed); var firstname = new field(first_name, user.firstname ?? string.empty, field.store.yes, field.index.analyzed, field.termvector.yes); var lastname = new field(last_name, user.lastname ?? string.empty, field.store.yes, field.index.analyzed, field.termvector.yes); var bio = new field(bio, user.bio ?? string.empty, field.store.yes, field.index.analyzed, field.termvector.yes); // add boosts firstname.boost = 100f; lastname.boost = 75f; bio.boost = 50f; // add lucene fields mapped db fields doc.add(userid); doc.add(firstname); doc.add(lastname); doc.add(bio); // add entry index writer.adddocument(doc); } public string[] fieldstosearch { get; set; } // i.e. "firstname", "lastname", "bio" public usersearchresults searchusers(string searchquery, bool exact = false) { var results = new usersearchresults(); if (!string.isnullorempty(searchquery)) { //searchquery = prepareinput(searchquery, exact); try { using (var searcher = new indexsearcher(indexdirectory, false)) { var analyzer = new standardanalyzer(lucene_version); // search multiple fields (ordered relevance) var parser = new multifieldqueryparser(lucene_version, fieldstosearch, analyzer); parser.allowleadingwildcard = true; parser.defaultoperator = exact ? queryparser.and_operator : queryparser.or_operator; var multifieldquery = parsequery(searchquery, parser); var hits = searcher.search(multifieldquery, null, searchresultlimit, sort.relevance); var docs = hits.scoredocs; results.items = _maplucenetodatalist(docs, searcher).cast<usertestitem>().tolist(); results.total = results.items.count; results.rawquery = lastusedquery.tostring(); analyzer.close(); searcher.dispose(); } } catch (exception ex) { debug.writeline(ex.tostring()); } } return results; }
you can pass boosts directly multifieldqueryparser, example:
var boosts = new dictionary<string, float> { {"first name", 100}, {"last name", 75}, {"bio", 50}, } var parser = new multifieldqueryparser(lucene_version, fieldstosearch, analyzer, boosts);
Comments
Post a Comment