Created
October 28, 2013 13:52
-
-
Save rajanim/7197154 to your computer and use it in GitHub Desktop.
ACLPostFilter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cn.com.os.solr; | |
| import java.io.IOException; | |
| import java.util.BitSet; | |
| import org.apache.lucene.index.AtomicReaderContext; | |
| import org.apache.lucene.search.FieldCache; | |
| import org.apache.lucene.search.FieldCache.Ints; | |
| import org.apache.lucene.search.IndexSearcher; | |
| import org.apache.solr.search.DelegatingCollector; | |
| import org.apache.solr.search.ExtendedQueryBase; | |
| import org.apache.solr.search.PostFilter; | |
| public class ACLPostFilter extends ExtendedQueryBase implements PostFilter { | |
| BitSet documentsAclBits; | |
| public ACLPostFilter(String userId, BitSet documentsAclBits) { | |
| /*This is the bit map array created for the user logged in. | |
| * Bit is of size - MaxDocs in solr server. | |
| * If bit is set at any position it means user has access to the document at that position. | |
| * Ex: If the total number of documents in the solr server is 5, the bitset is of actual size 5 [trailing 3 positions will be zero] | |
| * And if the current user logged in has access to only 1,2 and 3rd document, bitarray will be - 11100[000] | |
| * It is set at position 1,2 and 3 because user has access to document 1,2 and 3 only. | |
| */ | |
| this.documentsAclBits = documentsAclBits; | |
| } | |
| @Override | |
| public DelegatingCollector getFilterCollector(IndexSearcher searcher) { | |
| return new AclDocCollector(this.documentsAclBits); | |
| } | |
| class AclDocCollector extends DelegatingCollector { | |
| //gets the uniqueKeyDocId Value | |
| //This is the documentId's value - A Primary Key - assigned to each document. | |
| //It is auto increment integer. | |
| Ints documentIdValue; | |
| BitSet bits; | |
| public AclDocCollector(BitSet bits){ | |
| this.bits = bits; | |
| } | |
| @Override | |
| public void collect(int doc) throws IOException { | |
| if (isSet(doc, this.bits)) { | |
| delegate.collect(doc); | |
| } | |
| } | |
| private boolean isSet(int doc, BitSet bits) { | |
| if (bits.get(documentIdValue.get(doc))) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public void setNextReader(AtomicReaderContext context) | |
| throws IOException { | |
| documentIdValue = FieldCache.DEFAULT.getInts(context.reader(),"documentId", true); | |
| super.setNextReader(context); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Rajani,
I am interested in your bitset filter approach. However I have a few questions with regards to how this can be put in practice. One primary question that I have is how are you mapping lucene doc ids with the docids sent as part of the bitset (in the bitset array's index). Do you update the bitset everytime the lucene index is updated?