Lucene用多列提升/得分

时间:2013-08-15 19:38:46

标签: c# asp.net lucene lucene.net

我如何获取以下数据并能够得到以下结果?

我已经包含了一个代码示例,但我似乎无法弄清楚如何正确 搜索多个列并应用我需要的提升。

我能以正确的方式解决这个问题吗?

每列的增强/重量

First Name = 100

Last Name = 75

Bio = 50

数据

名字,姓氏,生物
Benny, Benson, This is a test

- "ben" appears in the first name AND last name

- Score = 175

Jim, Smith, Another test with the word ben

- "ben" appears in the bio

- Score = 50

John, Benson, And another test here

- "ben" appears in the last name

- Score = 75

结果

1. Benny
2. John
3. Jim

protected override void _addToLuceneIndex(dynamic item, IndexWriter writer)
{
    var user = item as 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();

    // get 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 to db fields
    doc.Add(userId);
    doc.Add(firstName);
    doc.Add(lastName);
    doc.Add(bio);

    // add entry to 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 by multiple fields (ordered by 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;
}

3 个答案:

答案 0 :(得分:1)

您可以直接将提升传递给MultiFieldQueryParser,例如:

var boosts = new Dictionary<string, float>
{
    {"First Name", 100},
    {"Last Name", 75},
    {"Bio", 50},
}
var parser = new MultiFieldQueryParser(LUCENE_VERSION, FieldsToSearch, analyzer, boosts);

答案 1 :(得分:0)

我相信我现在有我想要的行为。我不得不像这样提升查询,而不是在我正在进行索引的过程中提升。

var mainQuery = new BooleanQuery();

var fnQuery = new BooleanQuery();
fnQuery.Add(new WildcardQuery(new Term(FIRST_NAME, searchQuery)), Occur.SHOULD);
fnQuery.Boost = 100f;

var lnQuery = new BooleanQuery();
lnQuery.Add(new WildcardQuery(new Term(LAST_NAME, searchQuery)), Occur.SHOULD);
lnQuery.Boost = 75f;

var bioQuery = new BooleanQuery();
bioQuery.Add(new WildcardQuery(new Term(BIO, searchQuery)), Occur.SHOULD);
bioQuery.Boost = 50f;

mainQuery.Add(fnQuery, Occur.SHOULD);
mainQuery.Add(lnQuery, Occur.SHOULD);
mainQuery.Add(bioQuery, Occur.SHOULD);

var hits = searcher.Search(mainQuery, null, SearchResultLimit, Sort.RELEVANCE);

有更合适的方法吗?

答案 2 :(得分:0)

Boost迷失在Lucene的算法中,所以它是一个加速和搜索术语的混合体。你必须返回一组比赛的另一种选择(即我在凯文史密斯上搜索),根据相似度/卢森的得分有50场比赛。然后在数据库中计算一个额外的字段并按字段排序,使用LINQ对Lucene收集器的结果进行排序。这与增强功能略有不同,但您可以更好地控制确切的分数和排序。