我只需要从Lucene搜索中返回用户有权访问的文档。此Lucene索引中有几种类型的数据。我可以从数据库中获取一个ID列表,这些ID构成了每种类型数据的“允许”子集。那么如何使用这两个属性('type'和'entity_id')来构建客户过滤器?我正在使用Java。
/**
* @param idMap key: type; value: IDs
*/
public MyCustomerFilter(Map<Integer, Set<Long>> idMap){
this.idMap = idMap;
}
@Override
public DocIdSet getDocIdSet(IndexReader reader) throws Exception {
OpenBitSet bitSet = new OpenBitSet(reader.maxDoc());
Set<Integer result = permit(reader);
for (Integer position : result) {
bitSet.set(position);
}
return bitSet;
}
private Set<Integer> permit(IndexReader reader) {
Set<Integer> result = new HashSet<>();
for (Map.Entry<Integer, Set<Long>> entry : idMap.entrySet()) {
Integer type = entry.getKey();
Set<Long> idSet = entry.getValue();
Set<Integer> typeResult = new HashSet<>();
Set<Integer> idResult = new HashSet<>();
int[] docs = new int[1];
int[] freqs= new int[1];
try {
int count = reader.termDocs(new Term("type",
String.valueOf(type))).read(docs, freqs);
if (count > 0) {
typeResult.add(docs[0]);
}
} catch(Exception e) {
// ignore
}
for(Long id : idSet) {
docs = new int[1];
freqs= new int[1];
try {
int count = reader.termDocs(new Term("entityId",
String.valueOf(id))).read(docs, freqs);
if (count > 0) {
idResult.add(docs[0]);
}
} catch(Exception e) {
// ignore
}
}
for (Integer i : idResult){
for (Integer j : typeResult) {
if (i.intValue == j.intValue) {
result.add(i);
break;
}
}
}
}
return result ;
}