Search in sources :

Example 31 with DocSet

use of org.apache.solr.search.DocSet in project SearchServices by Alfresco.

the class AbstractAuthoritySetQuery method getACLSet.

protected HybridBitSet getACLSet(String[] auths, String field, SolrIndexSearcher searcher) throws IOException {
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    for (String current : auths) {
        queryBuilder.add(new TermQuery(new Term(field, current)), BooleanClause.Occur.SHOULD);
    }
    // NOTE: this query will be in the filter cache. Ideally it would remain cached throughout the users session.
    DocSet docSet = searcher.getDocSet(queryBuilder.build());
    DocIterator iterator = docSet.iterator();
    if (!iterator.hasNext()) {
        return new EmptyHybridBitSet();
    }
    // TODO : makes this configurable. For some systems this is huge and for others not big enough.
    HybridBitSet hybridBitSet = new HybridBitSet(60000000);
    List<LeafReaderContext> leaves = searcher.getTopReaderContext().leaves();
    LeafReaderContext context = leaves.get(0);
    NumericDocValues aclValues = DocValuesCache.getNumericDocValues(QueryConstants.FIELD_ACLID, context.reader());
    LeafReader reader = context.reader();
    int ceil = reader.maxDoc();
    int base = 0;
    int ord = 0;
    while (iterator.hasNext()) {
        int doc = iterator.nextDoc();
        if (doc >= ceil) {
            do {
                ++ord;
                context = leaves.get(ord);
                reader = context.reader();
                base = context.docBase;
                ceil = base + reader.maxDoc();
                aclValues = DocValuesCache.getNumericDocValues(QueryConstants.FIELD_ACLID, reader);
            } while (doc >= ceil);
        }
        if (aclValues != null) {
            long aclId = aclValues.get(doc - base);
            hybridBitSet.set(aclId);
        }
    }
    return hybridBitSet;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) NumericDocValues(org.apache.lucene.index.NumericDocValues) DocIterator(org.apache.solr.search.DocIterator) LeafReader(org.apache.lucene.index.LeafReader) Term(org.apache.lucene.index.Term) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocSet(org.apache.solr.search.DocSet)

Example 32 with DocSet

use of org.apache.solr.search.DocSet in project SearchServices by Alfresco.

the class SolrAuthorityScorer method createAuthorityScorer.

public static SolrAuthorityScorer createAuthorityScorer(Weight weight, LeafReaderContext context, SolrIndexSearcher searcher, String authority) throws IOException {
    Properties p = searcher.getSchema().getResourceLoader().getCoreProperties();
    boolean doPermissionChecks = Boolean.parseBoolean(p.getProperty("alfresco.doPermissionChecks", "true"));
    Query key = new SolrAuthorityQuery(authority);
    DocSet answer = (DocSet) searcher.cacheLookup(CacheConstants.ALFRESCO_AUTHORITY_CACHE, key);
    if (answer != null) {
        // Answer was in the cache, so return it.
        return new SolrAuthorityScorer(weight, answer, context, searcher);
    }
    // Answer was not in cache, so build the results, cache and return.
    final HashSet<String> globalReaders = GlobalReaders.getReaders();
    if (globalReaders.contains(authority) || (doPermissionChecks == false)) {
        // can read all
        DocSet allDocs = searcher.getDocSet(new MatchAllDocsQuery());
        return new SolrAuthorityScorer(weight, allDocs, context, searcher);
    }
    // Docs for which the authority has explicit read access.
    DocSet readableDocSet = searcher.getDocSet(new SolrReaderQuery(authority));
    // Are all doc owners granted read permissions at a global level?
    if (globalReaders.contains(PermissionService.OWNER_AUTHORITY)) {
        // Get the set of docs owned by the authority (which they can therefore read).
        DocSet authorityOwnedDocs = searcher.getDocSet(new SolrOwnerQuery(authority));
        // Final set of docs that the authority can read.
        DocSet toCache = readableDocSet.union(authorityOwnedDocs);
        searcher.cacheInsert(CacheConstants.ALFRESCO_AUTHORITY_CACHE, key, toCache);
        return new SolrAuthorityScorer(weight, toCache, context, searcher);
    } else {
        // for that docs I own that have owner Read rights
        DocSet ownerReadableDocSet = searcher.getDocSet(new SolrReaderQuery(PermissionService.OWNER_AUTHORITY));
        DocSet authorityOwnedDocs = searcher.getDocSet(new SolrOwnerQuery(authority));
        // Docs where the authority is an owner and where owners have read rights.
        DocSet docsAuthorityOwnsAndCanRead = ownerReadableDocSet.intersection(authorityOwnedDocs);
        // Final set of docs that the authority can read.
        DocSet toCache = readableDocSet.union(docsAuthorityOwnsAndCanRead);
        searcher.cacheInsert(CacheConstants.ALFRESCO_AUTHORITY_CACHE, key, toCache);
        return new SolrAuthorityScorer(weight, toCache, context, searcher);
    }
}
Also used : Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Properties(java.util.Properties) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DocSet(org.apache.solr.search.DocSet)

Example 33 with DocSet

use of org.apache.solr.search.DocSet in project SearchServices by Alfresco.

the class SolrAuthoritySetScorer method createAuthoritySetScorer.

public static SolrAuthoritySetScorer createAuthoritySetScorer(Weight weight, LeafReaderContext context, SolrIndexSearcher searcher, String authorities) throws IOException {
    Properties p = searcher.getSchema().getResourceLoader().getCoreProperties();
    boolean doPermissionChecks = Boolean.parseBoolean(p.getProperty("alfresco.doPermissionChecks", "true"));
    Query key = new SolrAuthoritySetQuery(authorities);
    DocSet answer = (DocSet) searcher.cacheLookup(CacheConstants.ALFRESCO_AUTHORITY_CACHE, key);
    if (answer != null) {
        // Answer was in the cache, so return it.
        return new SolrAuthoritySetScorer(weight, answer, context, searcher);
    }
    // Answer was not in cache, so build the results, cache and return.
    String[] auths = authorities.substring(1).split(authorities.substring(0, 1));
    boolean hasGlobalRead = false;
    final HashSet<String> globalReaders = GlobalReaders.getReaders();
    for (String auth : auths) {
        if (globalReaders.contains(auth)) {
            hasGlobalRead = true;
            break;
        }
    }
    if (hasGlobalRead || (doPermissionChecks == false)) {
        // can read all
        WrappedQuery wrapped = new WrappedQuery(new MatchAllDocsQuery());
        wrapped.setCache(false);
        DocSet allDocs = searcher.getDocSet(wrapped);
        return new SolrAuthoritySetScorer(weight, allDocs, context, searcher);
    }
    // Docs for which the authorities have explicit read access.
    WrappedQuery wrapped;
    wrapped = new WrappedQuery(new SolrReaderSetQuery(authorities));
    wrapped.setCache(false);
    DocSet readableDocSet = searcher.getDocSet(wrapped);
    // Are all doc owners granted read permissions at a global level?
    if (globalReaders.contains(PermissionService.OWNER_AUTHORITY)) {
        // Get the set of docs owned by the authorities (which they can therefore read).
        wrapped = new WrappedQuery(new SolrOwnerSetQuery(authorities));
        wrapped.setCache(false);
        DocSet authorityOwnedDocs = searcher.getDocSet(wrapped);
        // Final set of docs that the authorities can read.
        DocSet toCache = readableDocSet.union(authorityOwnedDocs);
        searcher.cacheInsert(CacheConstants.ALFRESCO_AUTHORITY_CACHE, key, toCache);
        return new SolrAuthoritySetScorer(weight, toCache, context, searcher);
    } else {
        // for that docs I own that have owner Read rights
        wrapped = new WrappedQuery(new SolrReaderSetQuery("|" + PermissionService.OWNER_AUTHORITY));
        wrapped.setCache(false);
        DocSet ownerReadableDocSet = searcher.getDocSet(wrapped);
        wrapped = new WrappedQuery(new SolrOwnerSetQuery(authorities));
        wrapped.setCache(false);
        DocSet authorityOwnedDocs = searcher.getDocSet(wrapped);
        // Docs where the authority is an owner and where owners have read rights.
        DocSet docsAuthorityOwnsAndCanRead = ownerReadableDocSet.intersection(authorityOwnedDocs);
        // Final set of docs that the authorities can read.
        DocSet toCache = readableDocSet.union(docsAuthorityOwnsAndCanRead);
        searcher.cacheInsert(CacheConstants.ALFRESCO_AUTHORITY_CACHE, key, toCache);
        return new SolrAuthoritySetScorer(weight, toCache, context, searcher);
    }
}
Also used : Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WrappedQuery(org.apache.solr.search.WrappedQuery) WrappedQuery(org.apache.solr.search.WrappedQuery) Properties(java.util.Properties) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DocSet(org.apache.solr.search.DocSet)

Example 34 with DocSet

use of org.apache.solr.search.DocSet in project SearchServices by Alfresco.

the class SolrReaderSetScorer2 method createReaderSetScorer.

public static AbstractSolrCachingScorer createReaderSetScorer(Weight weight, LeafReaderContext context, SolrIndexSearcher searcher, String authorities, LeafReader reader) throws IOException {
    DocSet readableDocSet = (DocSet) searcher.cacheLookup(CacheConstants.ALFRESCO_READER_CACHE, authorities);
    if (readableDocSet == null) {
        String[] auths = authorities.substring(1).split(authorities.substring(0, 1));
        readableDocSet = new BitDocSet(new FixedBitSet(searcher.maxDoc()));
        BooleanQuery.Builder bQuery = new BooleanQuery.Builder();
        for (String current : auths) {
            bQuery.add(new TermQuery(new Term(QueryConstants.FIELD_READER, current)), Occur.SHOULD);
        }
        WrappedQuery wrapped = new WrappedQuery(bQuery.build());
        wrapped.setCache(false);
        DocSet aclDocs = searcher.getDocSet(wrapped);
        HashSet<Long> aclsFound = new HashSet<Long>(aclDocs.size());
        NumericDocValues aclDocValues = searcher.getSlowAtomicReader().getNumericDocValues(QueryConstants.FIELD_ACLID);
        for (DocIterator it = aclDocs.iterator(); it.hasNext(); ) /**/
        {
            int docID = it.nextDoc();
            // Obtain the ACL ID for this ACL doc.
            long aclID = aclDocValues.get(docID);
            aclsFound.add(getLong(aclID));
        }
        if (aclsFound.size() > 0) {
            for (LeafReaderContext readerContext : searcher.getSlowAtomicReader().leaves()) {
                int maxDoc = readerContext.reader().maxDoc();
                NumericDocValues fieldValues = DocValuesCache.getNumericDocValues(QueryConstants.FIELD_ACLID, readerContext.reader());
                if (fieldValues != null) {
                    for (int i = 0; i < maxDoc; i++) {
                        long aclID = fieldValues.get(i);
                        Long key = getLong(aclID);
                        if (aclsFound.contains(key)) {
                            readableDocSet.add(readerContext.docBase + i);
                        }
                    }
                }
            }
        }
        // Exclude the ACL docs from the results, we only want real docs that match.
        // Probably not very efficient, what we really want is remove(docID)
        readableDocSet = readableDocSet.andNot(aclDocs);
        searcher.cacheInsert(CacheConstants.ALFRESCO_READER_CACHE, authorities, readableDocSet);
    }
    // plus check of course, for presence in cache at start of method.
    return new SolrReaderSetScorer2(weight, readableDocSet, context, searcher);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) NumericDocValues(org.apache.lucene.index.NumericDocValues) DocIterator(org.apache.solr.search.DocIterator) WrappedQuery(org.apache.solr.search.WrappedQuery) Term(org.apache.lucene.index.Term) BitDocSet(org.apache.solr.search.BitDocSet) FixedBitSet(org.apache.lucene.util.FixedBitSet) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BitDocSet(org.apache.solr.search.BitDocSet) DocSet(org.apache.solr.search.DocSet) HashSet(java.util.HashSet)

Example 35 with DocSet

use of org.apache.solr.search.DocSet in project SearchServices by Alfresco.

the class SolrReaderScorer method createReaderScorer.

public static SolrReaderScorer createReaderScorer(Weight weight, LeafReaderContext context, SolrIndexSearcher searcher, String authority) throws IOException {
    DocSet readableDocs = (DocSet) searcher.cacheLookup(CacheConstants.ALFRESCO_READER_CACHE, authority);
    if (readableDocs == null) {
        // Cache miss: query the index for ACL docs where the reader matches the authority.
        DocSet aclDocs = searcher.getDocSet(new TermQuery(new Term(QueryConstants.FIELD_READER, authority)));
        // Allocate a bitset to store the results.
        readableDocs = new BitDocSet(new FixedBitSet(searcher.maxDoc()));
        // Translate from ACL docs to real docs
        for (DocIterator it = aclDocs.iterator(); it.hasNext(); ) /**/
        {
            int docID = it.nextDoc();
            // Obtain the ACL ID for this ACL doc.
            long aclID = searcher.getSlowAtomicReader().getNumericDocValues(QueryConstants.FIELD_ACLID).get(docID);
            SchemaField schemaField = searcher.getSchema().getField(QueryConstants.FIELD_ACLID);
            Query query = schemaField.getType().getFieldQuery(null, schemaField, Long.toString(aclID));
            DocSet docsForAclId = searcher.getDocSet(query);
            readableDocs = readableDocs.union(docsForAclId);
            // Exclude the ACL docs from the results, we only want real docs that match.
            // Probably not very efficient, what we really want is remove(docID)
            readableDocs = readableDocs.andNot(aclDocs);
        }
        searcher.cacheInsert(CacheConstants.ALFRESCO_READER_CACHE, authority, readableDocs);
    }
    return new SolrReaderScorer(weight, readableDocs, context, searcher);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) TermQuery(org.apache.lucene.search.TermQuery) BitDocSet(org.apache.solr.search.BitDocSet) DocIterator(org.apache.solr.search.DocIterator) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) FixedBitSet(org.apache.lucene.util.FixedBitSet) Term(org.apache.lucene.index.Term) BitDocSet(org.apache.solr.search.BitDocSet) DocSet(org.apache.solr.search.DocSet)

Aggregations

DocSet (org.apache.solr.search.DocSet)37 BitDocSet (org.apache.solr.search.BitDocSet)19 Query (org.apache.lucene.search.Query)15 Term (org.apache.lucene.index.Term)12 TermQuery (org.apache.lucene.search.TermQuery)11 FixedBitSet (org.apache.lucene.util.FixedBitSet)9 DocIterator (org.apache.solr.search.DocIterator)9 BooleanQuery (org.apache.lucene.search.BooleanQuery)8 SchemaField (org.apache.solr.schema.SchemaField)8 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)7 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)6 HashDocSet (org.apache.solr.search.HashDocSet)6 SortedIntDocSet (org.apache.solr.search.SortedIntDocSet)6 WrappedQuery (org.apache.solr.search.WrappedQuery)6 FieldType (org.apache.solr.schema.FieldType)5 ArrayList (java.util.ArrayList)4 BytesRef (org.apache.lucene.util.BytesRef)4 NamedList (org.apache.solr.common.util.NamedList)4 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)4 IdentityHashMap (java.util.IdentityHashMap)3