在 lucene .net 中查找字段的所有可用值 [英] Find all available values for a field in lucene .net

查看:14
本文介绍了在 lucene .net 中查找字段的所有可用值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个字段 x,它可以包含一个值 y 或 z 等,有没有一种方法可以查询,以便我可以只返回已被索引的值?

If I have a field x, that can contain a value of y, or z etc, is there a way I can query so that I can return only the values that have been indexed?

示例x 可用的可设置值 = test1, test2, test3, test4

Example x available settable values = test1, test2, test3, test4

第 1 项:字段 x = test1

Item 1 : Field x = test1

第 2 项:字段 x = test2

Item 2 : Field x = test2

第 3 项:字段 x = test4

Item 3 : Field x = test4

第 4 项:字段 x = test1

Item 4 : Field x = test1

执行所需的查询将返回以下列表:测试1,测试2,测试4

Performing required query would return a list of: test1, test2, test4

推荐答案

我之前已经实现了这个作为扩展方法:

I've implemented this before as an extension method:

public static class ReaderExtentions
{
    public static IEnumerable<string> UniqueTermsFromField(
                                          this IndexReader reader, string field)
    {
        var termEnum = reader.Terms(new Term(field));

        do
        {
            var currentTerm = termEnum.Term();

            if (currentTerm.Field() != field)
                yield break;

            yield return currentTerm.Text();
        } while (termEnum.Next());
    }
}

您可以像这样非常轻松地使用它:

You can use it very easily like this:

var allPossibleTermsForField = reader.UniqueTermsFromField("FieldName");

这会让你得到你想要的.

That will return you what you want.

由于心不在焉,我跳过了上面的第一个学期.我已经相应地更新了代码以使其正常工作.

I was skipping the first term above, due to some absent-mindedness. I've updated the code accordingly to work properly.

这篇关于在 lucene .net 中查找字段的所有可用值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆