gpt4 book ai didi

java - Lucene:按数字存储字段上的一组允许值进行过滤

转载 作者:行者123 更新时间:2023-11-30 07:42:32 24 4
gpt4 key购买 nike

是否有一种简单的方法来应用以下过滤器查询:

userAccessibleDocTypesSet.contains(doc.type)

其中集合被传递给查询(带有分散的整数),并且 doc.type 是文档中存储的 int 字段。对集合中的所有值使用 should 子句的 BooleanQuery 似乎有点过分,并且可能会突破限制。

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

最佳答案

这就是我想出来的。如果您发现任何性能问题,请告诉我。

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)";
}
}

关于java - Lucene:按数字存储字段上的一组允许值进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34458362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com