首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lucene:在数字存储字段上通过一组允许的值进行过滤

Lucene:在数字存储字段上通过一组允许的值进行过滤
EN

Stack Overflow用户
提问于 2015-12-24 22:21:16
回答 1查看 440关注 0票数 1

是否有一种简单的方法应用以下筛选器查询:

代码语言:javascript
复制
userAccessibleDocTypesSet.contains(doc.type)

其中,集合被传递给查询(带有散乱的整数),而doc.type是文档上存储的int字段。对集合中的所有值都使用with子句的BooleanQuery似乎过分了,可能会推高限制。

什么是正确的方法?如果由于用户的完全访问权限而没有过滤任何内容,如何最后应用此筛选器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-25 18:54:58

这就是我想出来的。如果你看到任何表现上的问题,请告诉我。

代码语言:javascript
复制
public class FilterByIntegerSetQuery extends Query
{
    protected String numericDocValueFieldName;
    protected Set<Integer> allowedValues;


    public FilterByIntegerSetQuery(String numericDocValueFieldName, Set<Integer> allowedValues)
    {
        this.numericDocValueFieldName = numericDocValueFieldName;
        this.allowedValues = allowedValues;
    }

    @Override
    public Weight createWeight(IndexSearcher searcher, boolean needsScores)
    {
        return new RandomAccessWeight(this)
        {
            @Override
            protected Bits getMatchingDocs(LeafReaderContext context) throws IOException
            {
                final int len = context.reader().maxDoc();
                final NumericDocValues values = context.reader().getNumericDocValues(numericDocValueFieldName);
                return new Bits()
                {
                    @Override
                    public boolean get(int index)
                    {
                        return allowedValues.contains((int) values.get(index));
                    }

                    @Override
                    public int length()
                    {
                        return len;
                    }
                };
            }
        };
    }


    @Override
    public String toString(String field)
    {
        return "(filter "+numericDocValueFieldName+" by set)";
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34458362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档