You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Gabriel Landais <la...@codelutin.com> on 2008/02/22 10:24:30 UTC
Security filtering from external DB
Hello,
we have chosen to use Lucene as the metadata database for a project.
Everything works great, from indexing to multi-threading search.
Also, we have chosen to split storage and security. It works great,
except when we come to filtering.
I have understood that Filter should set bits on documents that are ok,
but it is not the way we want to do it.
What our security service can give, is a Collection<String>
corresponding to a Document field, let's say "uuid". This collection
handles all documents user can read.
So, is there a way to set an event on "this document match the query,
does the filter keep this one in the list?". This way, we have not to
get all "uuid" of all Lucene docs in our filter, that is really costy.
We can filter Hits returned by search, but there should be a smarter
way to do it, no?
As summarizing this problem, got an idea :
How to create a Filter for a field in Collection<String>?
First, split Collection in Collection<Collection> with
BooleanQuery.maxClauseCount items maximum in each collection.
For each collection :
create a BooleanQuery with a TermQuery for each String.
perform a search with a HitCollector filling a BitSet
Now we have BitSet of each Collection, perform a BitSet.or to get a
final BitSet
Build a Filter based on that BitSet, and use that Filter as parameter
of each search
Of course, a cache system should be added, managed by security telling
if Collection as changed, to rebuild the user Filter.
Is it a good idea?
Thanks,
--
Gabriel <la...@codelutin.com>
http://www.codelutin.com
tel : 02 40 50 29 28 / fax : 09 59 92 29 28
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Re: Security filtering from external DB
Posted by Gabriel Landais <la...@codelutin.com>.
Jake Mannix a écrit :
> Gabriel,
> You can make this search much more efficient as follows: say that you have
> a method
>
> public BooleanQuery createQuery(Collection<String> allowedUUIDs);
>
> that works as you describe. Then you can easily create a useful reusable
> filter as follows:
>
> Filter filter = new CachingWrapperFilter(new
> QueryFilter(createQuery(uuidCollection)));
>
> this filter will cache BitSets keyed on IndexReader instances, so hang
> onto it and use it for
> the entire time your uuidCollection doesn't change.
>
> Hope this helps.
>
> -jake
Hi,
yes it helps!
As a result, I will not use my own BitSet cache mechanism. I have been
dumb when I believed that I couldn't create a SHOULD query on >1024
terms, as I can create inner BooleanQuery...
Just as note, QueryFilter is deprecated, QueryWrapperFilter replaces it.
The new code :
private Query createQuery(Collection<String> visibleItems) {
int maxTerms = BooleanQuery.getMaxClauseCount() - 1;
int i = 0;
boolean subQueryIsQuery = true;
BooleanQuery query = new BooleanQuery();
BooleanQuery subQuery = new BooleanQuery();
for (String string : visibleItems) {
subQuery.add(new TermQuery(new Term(KEY_UUID, string)),
BooleanClause.Occur.SHOULD);
if (i % maxTerms == 0) {
subQueryIsQuery = false;
query.add(new BooleanClause(subQuery,
BooleanClause.Occur.SHOULD));
subQuery = new BooleanQuery();
}
i++;
}
if (subQueryIsQuery) {
return subQuery;
}
return query;
}
private Filter createFilter(Collection<String> visibleItems) throws
SimExplorerTechnicalException {
Filter filter = new CachingWrapperFilter(new
QueryWrapperFilter(createQuery(visibleItems)));
return filter;
}
Not tested yet, but looks good.
Thanks a lot!
Gabriel
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Re: Security filtering from external DB
Posted by Jake Mannix <ja...@gmail.com>.
Gabriel,
You can make this search much more efficient as follows: say that you have
a method
public BooleanQuery createQuery(Collection<String> allowedUUIDs);
that works as you describe. Then you can easily create a useful reusable
filter as follows:
Filter filter = new CachingWrapperFilter(new
QueryFilter(createQuery(uuidCollection)));
this filter will cache BitSets keyed on IndexReader instances, so hang
onto it and use it for
the entire time your uuidCollection doesn't change.
Hope this helps.
-jake
On Wed, Feb 27, 2008 at 2:48 AM, Gabriel Landais <la...@codelutin.com>
wrote:
> h t a écrit :
> > I guess you can implement createBitSet() more effciently by using
> > Filer,but not BooleanQuery
> Hi,
> thanks for advice, but did you mean Filter or Filer? And even if I
> should use a Filter, I don't really understand how to replace the
> Boolean query :(
> The boolean query is already very efficient, so if it can be better,
> it's would be great!
> Regards,
> Gabriel
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>
Re: Security filtering from external DB
Posted by Gabriel Landais <la...@codelutin.com>.
h t a écrit :
> I guess you can implement createBitSet() more effciently by using
> Filer,but not BooleanQuery
Hi,
thanks for advice, but did you mean Filter or Filer? And even if I
should use a Filter, I don't really understand how to replace the
Boolean query :(
The boolean query is already very efficient, so if it can be better,
it's would be great!
Regards,
Gabriel
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Re: Security filtering from external DB
Posted by h t <bl...@gmail.com>.
I guess you can implement createBitSet() more effciently by using
Filer,but not BooleanQuery
2008/2/25, Gabriel Landais <la...@codelutin.com>:
>
> Gabriel Landais a écrit :
>
> > How to create a Filter for a field in Collection<String>?
> > First, split Collection in Collection<Collection> with
> > BooleanQuery.maxClauseCount items maximum in each collection.
> > For each collection :
> > create a BooleanQuery with a TermQuery for each String.
> > perform a search with a HitCollector filling a BitSet
> > Now we have BitSet of each Collection, perform a BitSet.or to get a
> > final BitSet
> > Build a Filter based on that BitSet, and use that Filter as parameter
> > of each search
> >
>
> It works perfectly. It is implemented in a GPL project, aka
> "simexplorer-is", in class LuceneDatabase (createFilter) :
> <
> https://labs.libre-entreprise.org/plugins/scmsvn/viewcvs.php/trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/?root=simexplorer-si
> >
> I think that this class is a good sample of what is possible to do with
> Lucene as a metadata database.
> Hope it will help.
> Regards,
>
>
> --
> Gabriel <la...@codelutin.com>
> http://www.codelutin.com
> tel : 02 40 50 29 28 / fax : 09 59 92 29 28
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>
Re: Security filtering from external DB
Posted by Gabriel Landais <la...@codelutin.com>.
Gabriel Landais a écrit :
> How to create a Filter for a field in Collection<String>?
> First, split Collection in Collection<Collection> with
> BooleanQuery.maxClauseCount items maximum in each collection.
> For each collection :
> create a BooleanQuery with a TermQuery for each String.
> perform a search with a HitCollector filling a BitSet
> Now we have BitSet of each Collection, perform a BitSet.or to get a
> final BitSet
> Build a Filter based on that BitSet, and use that Filter as parameter
> of each search
>
It works perfectly. It is implemented in a GPL project, aka
"simexplorer-is", in class LuceneDatabase (createFilter) :
<https://labs.libre-entreprise.org/plugins/scmsvn/viewcvs.php/trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/?root=simexplorer-si>
I think that this class is a good sample of what is possible to do with
Lucene as a metadata database.
Hope it will help.
Regards,
--
Gabriel <la...@codelutin.com>
http://www.codelutin.com
tel : 02 40 50 29 28 / fax : 09 59 92 29 28
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org