Lucene 中搜索结果的相关性

发布于 2024-11-18 04:19:10 字数 1083 浏览 0 评论 0原文

我想要的是:

在搜索方法中,我将添加一个额外的参数,即 float 类型的相关性参数来设置截止相关性。假设如果截止值为 60%,我想要相关性高于 60% 的项目。

这是当前的搜索代码:

假设搜索文本是 在 lucene 文件系统中我有以下描述: 1)abcdef 2)ABC 3)abcd

现在它将获取上述所有三个文档,我想获取那些相关性高于 60% 的文档。

//现在我没有在方法中的任何地方使用相关性参数:

public static string[] Search(string searchText,float relevanceparam)
        {
            //List of ID
            List<string> searchResultID = new List<string>();

            IndexSearcher searcher = new IndexSearcher(reader);
            Term searchTerm = new Term("Text", searchText);
            Query query = new TermQuery(searchTerm);
            Hits hits = searcher.Search(query);
            for (int i = 0; i < hits.Length(); i++)
            {
                float r = hits.Score(i);
                Document doc = hits.Doc(i);

                searchResultID.Add(doc.Get("ID"));
            }
            return searchResultID.ToArray();
        }

编辑:

如果我为查询设置增强会怎样 说:query.SetBoost(1.6);--这相当于 60% 吗?

What I want is :

In the search method i will add an extra parameter say relevance param of type float to setup the cuttoff relevance. So lets say if the cutoff is 60% I want items that are higher than 60% relevance.

Here is current code of search :

say the search text is a
and in lucene file system i have following description:
1) abcdef
2)abc
3)abcd

for now it will fetch all the above three docuements , i want to fetch those which are that are higher than 60% relevance.

//for now i am not using the relevanceparam anywhere in the method :

public static string[] Search(string searchText,float relevanceparam)
        {
            //List of ID
            List<string> searchResultID = new List<string>();

            IndexSearcher searcher = new IndexSearcher(reader);
            Term searchTerm = new Term("Text", searchText);
            Query query = new TermQuery(searchTerm);
            Hits hits = searcher.Search(query);
            for (int i = 0; i < hits.Length(); i++)
            {
                float r = hits.Score(i);
                Document doc = hits.Doc(i);

                searchResultID.Add(doc.Get("ID"));
            }
            return searchResultID.ToArray();
        }

Edit :

what if i set boost to my query
say : query.SetBoost(1.6);-- is this is equivalent to 60 percent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煞人兵器 2024-11-25 04:19:10

您可以通过忽略那些小于 TopDocs.MaxScore * minRelativeRelevance 的点击轻松完成此操作,其中 minRelativeRelevance 应该是 0 到 1 之间的值。

我已经修改了您的代码为了匹配 Lucene.Net 3.0.3 版本,并在对 IndexSearcher.Doc 的调用中添加了 FieldSelector 以避免加载非必填字段。

调用 Query.SetBoost(1.6) 仅意味着该查询计算出的分数将提高 60%(乘以 1.6)。如果涉及其他查询(例如,在 BooleanQuery 中),它可能会更改结果的顺序,但不会更改返回的结果。

public static String[] Search(IndexReader reader, String searchText, 
                              Single minRelativeRelevance) {
    var resultIds = new List<String>();

    var searcher = new IndexSearcher(reader);
    var searchTerm = new Term("Text", searchText);
    var query = new TermQuery(searchTerm);
    var hits = searcher.Search(query, 100);

    var minScore = hits.MaxScore * minRelativeRelevance;
    var fieldSelector = new MapFieldSelector("ID");
    foreach (var hit in hits.ScoreDocs) {
        if (hit.Score >= minScore) {
            var document = searcher.Doc(hit.Doc, fieldSelector);
            var hitId = document.Get("ID");
            resultIds.Add(hitId);
        }
    }

    return resultIds.ToArray();
}

You can easily do this by ignore those hits that have less than a TopDocs.MaxScore * minRelativeRelevance where minRelativeRelevance should be a value between 0 and 1.

I've modified your code to match the 3.0.3 release of Lucene.Net, and added a FieldSelector to your call to IndexSearcher.Doc to avoid loading non-required fields.

Calling Query.SetBoost(1.6) would only mean that the score calculated by that query would be boosted by 60% (multiplied with 1.6). It may change the ordering of the result if there were other queries involved (in a BooleanQuery, for example), but it wont change which results are returned.

public static String[] Search(IndexReader reader, String searchText, 
                              Single minRelativeRelevance) {
    var resultIds = new List<String>();

    var searcher = new IndexSearcher(reader);
    var searchTerm = new Term("Text", searchText);
    var query = new TermQuery(searchTerm);
    var hits = searcher.Search(query, 100);

    var minScore = hits.MaxScore * minRelativeRelevance;
    var fieldSelector = new MapFieldSelector("ID");
    foreach (var hit in hits.ScoreDocs) {
        if (hit.Score >= minScore) {
            var document = searcher.Doc(hit.Doc, fieldSelector);
            var hitId = document.Get("ID");
            resultIds.Add(hitId);
        }
    }

    return resultIds.ToArray();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文