问题描述
如果我有一个字段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
Examplex 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
执行必需的查询将返回以下列表:test1,test2,test4
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,所以我跳过了上面的第一项.我已经相应地更新了代码,以使其正常工作.
I was skipping the first term above, due to some absent-mindedness. I've updated the code accordingly to work properly.
这篇关于在lucene .net中查找字段的所有可用值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!