org.apache.lucene.document.FieldSelector Java Examples

The following examples show how to use org.apache.lucene.document.FieldSelector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocumentUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static FieldSelector declaredTypesFieldSelector (
        final boolean includeSource,
        final boolean includeSimpleName) {
    return includeSource ?
        includeSimpleName ?
            Queries.createFieldSelector(FIELD_PACKAGE_NAME, FIELD_BINARY_NAME, FIELD_SIMPLE_NAME, FIELD_SOURCE) :
            Queries.createFieldSelector(FIELD_PACKAGE_NAME, FIELD_BINARY_NAME, FIELD_SOURCE) :
        includeSimpleName ?
            Queries.createFieldSelector(FIELD_PACKAGE_NAME, FIELD_BINARY_NAME, FIELD_SIMPLE_NAME) :
            Queries.createFieldSelector(FIELD_PACKAGE_NAME, FIELD_BINARY_NAME);
}
 
Example #2
Source File: PersistentClassIndex.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public <T> void getDeclaredElements (
        @NonNull final String simpleName,
        @NonNull final ClassIndex.NameKind kind,
        @NonNull final Set<? extends ClassIndex.SearchScopeType> scope,
        @NonNull final FieldSelector selector,
        @NonNull final Convertor<? super Document, T> convertor,
        @NonNull final Collection<? super T> result) throws InterruptedException, IOException {
    final Pair<Convertor<? super Document, T>,Index> ctu = indexPath.getPatch(convertor);
    try {
        IndexManager.priorityAccess(() -> {
            final Query query =  QueryUtil.scopeFilter(
                    Queries.createQuery(
                            DocumentUtil.FIELD_SIMPLE_NAME,
                            DocumentUtil.FIELD_CASE_INSENSITIVE_NAME,
                            simpleName,
                            DocumentUtil.translateQueryKind(kind)),
                    scope);
            if (query != null) {
                index.query(result, ctu.first(), selector, cancel.get(), query);
                if (ctu.second() != null) {
                    ctu.second().query(result, convertor, selector, cancel.get(), query);
                }
            }
            return null;
        });
    } catch (IOException ioe) {
        this.<Void,IOException>handleException(null, ioe, root);
    }
}
 
Example #3
Source File: ClassIndexImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public abstract <T> void getDeclaredElements (
@NonNull String name,
@NonNull ClassIndex.NameKind kind,
@NonNull Set<? extends ClassIndex.SearchScopeType> scope,
@NonNull FieldSelector selector,
@NonNull Convertor<? super Document, T> convertor,
@NonNull Collection<? super T> result) throws IOException, InterruptedException;
 
Example #4
Source File: JavaSourceTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public <T> void query(
        Collection<? super T> result,
        Convertor<? super org.apache.lucene.document.Document, T> convertor,
        FieldSelector selector,
        AtomicBoolean cancel,
        Query... queries) throws IOException, InterruptedException {
    await(cancel);
}
 
Example #5
Source File: JavaSourceTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public <S, T> void queryDocTerms(
        Map<? super T, Set<S>> result,
        Convertor<? super org.apache.lucene.document.Document, T> convertor,
        Convertor<? super Term, S> termConvertor,
        FieldSelector selector,
        AtomicBoolean cancel,
        Query... queries) throws IOException, InterruptedException {
    await(cancel);
}
 
Example #6
Source File: SearchResultsImpl.java    From olat with Apache License 2.0 5 votes vote down vote up
private List<ResultDocument> initResultList(final Identity identity, final Roles roles, final Query query, final Analyzer analyzer, final Searcher searcher,
        final TopDocs docs, final int firstResult, final int maxReturns, final boolean doHighlight) throws IOException {
    final FieldSelector selector = new FieldSelector() {
        @Override
        public FieldSelectorResult accept(final String fieldName) {
            return (doHighlight || !AbstractOlatDocument.CONTENT_FIELD_NAME.equals(fieldName)) ? FieldSelectorResult.LOAD : FieldSelectorResult.NO_LOAD;
        }
    };

    maxHits = SearchServiceFactory.getService().getSearchModuleConfig().getMaxHits();
    totalHits = docs.totalHits;
    totalDocs = (docs.scoreDocs == null ? 0 : docs.scoreDocs.length);
    final int numOfDocs = Math.min(maxHits, docs.totalHits);
    final List<ResultDocument> res = new ArrayList<ResultDocument>(maxReturns + 1);
    for (int i = firstResult; i < numOfDocs && res.size() < maxReturns; i++) {
        final Document doc = searcher.doc(docs.scoreDocs[i].doc, selector);
        final String reservedTo = doc.get(AbstractOlatDocument.RESERVED_TO);
        if (StringHelper.containsNonWhitespace(reservedTo) && !"public".equals(reservedTo) && !reservedTo.contains(identity.getKey().toString())) {
            continue;// admin cannot see private documents
        }

        final ResultDocument rDoc = createResultDocument(doc, i, query, analyzer, doHighlight, identity, roles);
        if (rDoc != null) {
            res.add(rDoc);
        }

        if (!roles.isOLATAdmin() && i % 10 == 0) {
            // Do commit after certain number of documents because the transaction should not be too big
            DBFactory.getInstance().intermediateCommit();
        }
    }
    return res;
}
 
Example #7
Source File: SearchResultsImpl.java    From olat with Apache License 2.0 5 votes vote down vote up
private List<ResultDocument> initResultList(final Identity identity, final Roles roles, final Query query, final Analyzer analyzer, final Searcher searcher,
        final TopDocs docs, final int firstResult, final int maxReturns, final boolean doHighlight) throws IOException {
    final FieldSelector selector = new FieldSelector() {
        @Override
        public FieldSelectorResult accept(final String fieldName) {
            return (doHighlight || !AbstractOlatDocument.CONTENT_FIELD_NAME.equals(fieldName)) ? FieldSelectorResult.LOAD : FieldSelectorResult.NO_LOAD;
        }
    };

    maxHits = SearchServiceFactory.getService().getSearchModuleConfig().getMaxHits();
    totalHits = docs.totalHits;
    totalDocs = (docs.scoreDocs == null ? 0 : docs.scoreDocs.length);
    final int numOfDocs = Math.min(maxHits, docs.totalHits);
    final List<ResultDocument> res = new ArrayList<ResultDocument>(maxReturns + 1);
    for (int i = firstResult; i < numOfDocs && res.size() < maxReturns; i++) {
        final Document doc = searcher.doc(docs.scoreDocs[i].doc, selector);
        final String reservedTo = doc.get(AbstractOlatDocument.RESERVED_TO);
        if (StringHelper.containsNonWhitespace(reservedTo) && !"public".equals(reservedTo) && !reservedTo.contains(identity.getKey().toString())) {
            continue;// admin cannot see private documents
        }

        final ResultDocument rDoc = createResultDocument(doc, i, query, analyzer, doHighlight, identity, roles);
        if (rDoc != null) {
            res.add(rDoc);
        }

        if (!roles.isOLATAdmin() && i % 10 == 0) {
            // Do commit after certain number of documents because the transaction should not be too big
            DBFactory.getInstance().intermediateCommit();
        }
    }
    return res;
}
 
Example #8
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public List<Field> get(int n, FieldSelector fieldSelector) throws IOException
{
    Document document = ReferenceCountingReadOnlyIndexReader.super.document(n, fieldSelector);
    Field[] fields = document.getFields(fieldName);
    ArrayList<Field> cacheable = new ArrayList<Field>(fields.length);
    for (Field field : fields)
    {
        cacheable.add(field);
    }
    return cacheable;
}
 
Example #9
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public List<Field> get(int n, FieldSelector fieldSelector) throws IOException
{
    Document document = ReferenceCountingReadOnlyIndexReader.super.document(n, fieldSelector);
    List<Field> fields = (List<Field>) document.getFields();
    ArrayList<Field> cacheable = new ArrayList<Field>(fields.size());
    cacheable.addAll(fields);
    return cacheable;
}
 
Example #10
Source File: DocumentUtil.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static FieldSelector sourceNameFieldSelector () {
    return Queries.createFieldSelector(FIELD_SOURCE);
}
 
Example #11
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private <T> T manageCache(ConcurrentHashMap<Integer, WithUseCount<T>> cache, Accessor<T> accessor, int n, FieldSelector fieldSelector, int limit) throws IOException
{
    Integer key = Integer.valueOf(n);
    WithUseCount<T> value = cache.get(key);
    if (value == null)
    {
        T made = accessor.get(n, fieldSelector);
        value = new WithUseCount<T>(made, n);
        cache.put(key, value);

        // resize

        if (limit >= 0)
        {
            if (cache.size() >= limit)
            {
                HashMap<Integer, WithUseCount<T>> keep = new HashMap<Integer, WithUseCount<T>>();
                WithUseCount<T>[] existing = new WithUseCount[0];
                synchronized (cache)
                {
                    existing = cache.values().toArray(existing);
                    cache.clear();
                }
                Arrays.sort(existing);

                for (WithUseCount<T> current : existing)
                {
                    keep.put(Integer.valueOf(current.doc), current);
                    if ((current.count.get() == 0) || (keep.size() > (limit / 4)))
                    {
                        break;
                    }
                }
                keep.put(key, value);
                cache.putAll(keep);
            }
        }
    }
    else
    {
        value.count.getAndIncrement();
    }
    return value.object;
}
 
Example #12
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Field get(int n, FieldSelector fieldSelector) throws IOException
{
    return new Field(fieldName, getStringValue(n, fieldName), Store.NO, Index.UN_TOKENIZED);
}
 
Example #13
Source File: LuceneIndex.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public <S, T> void queryDocTerms(
        final @NonNull Map<? super T, Set<S>> result,
        final @NonNull Convertor<? super Document, T> convertor,
        final @NonNull Convertor<? super Term, S> termConvertor,
        @NullAllowed FieldSelector selector,
        final @NullAllowed AtomicBoolean cancel,
        final @NonNull Query... queries) throws IOException, InterruptedException {
    Parameters.notNull("queries", queries);             //NOI18N
    Parameters.notNull("slector", selector);            //NOI18N
    Parameters.notNull("convertor", convertor);         //NOI18N
    Parameters.notNull("termConvertor", termConvertor); //NOI18N
    Parameters.notNull("result", result);               //NOI18N
    if (selector == null) {
        selector = AllFieldsSelector.INSTANCE;
    }
    IndexReader in = null;
    try {
        in = dirCache.acquireReader();
        if (in == null) {
            LOGGER.log(Level.FINE, "{0} is invalid!", this);
            return;
        }
        final BitSet bs = new BitSet(in.maxDoc());
        final Collector c = new BitSetCollector(bs);
        final IndexSearcher searcher = new IndexSearcher(in);
        final TermCollector termCollector = new TermCollector(c);
        try {
            for (Query q : queries) {
                if (cancel != null && cancel.get()) {
                    throw new InterruptedException ();
                }
                if (q instanceof TermCollector.TermCollecting) {
                    ((TermCollector.TermCollecting)q).attach(termCollector);
                } else {
                    throw new IllegalArgumentException (
                            String.format("Query: %s does not implement TermCollecting",    //NOI18N
                            q.getClass().getName()));
                }
                searcher.search(q, termCollector);
            }
        } finally {
            searcher.close();
        }
    
        boolean logged = false;
        if (convertor instanceof IndexReaderInjection) {
            ((IndexReaderInjection)convertor).setIndexReader(in);
        }
        try {
            if (termConvertor instanceof IndexReaderInjection) {
                ((IndexReaderInjection)termConvertor).setIndexReader(in);
            }
            try {
                for (int docNum = bs.nextSetBit(0); docNum >= 0; docNum = bs.nextSetBit(docNum+1)) {
                    if (cancel != null && cancel.get()) {
                        throw new InterruptedException ();
                    }
                    final Document doc = in.document(docNum, selector);
                    final T value = convertor.convert(doc);
                    if (value != null) {
                        final Set<Term> terms = termCollector.get(docNum);
                        if (terms != null) {
                            result.put (value, convertTerms(termConvertor, terms));
                        } else {
                            if (!logged) {
                                LOGGER.log(Level.WARNING, "Index info [maxDoc: {0} numDoc: {1} docs: {2}]",
                                        new Object[] {
                                            in.maxDoc(),
                                            in.numDocs(),
                                            termCollector.docs()
                                        });
                                logged = true;
                            }
                            LOGGER.log(Level.WARNING, "No terms found for doc: {0}", docNum);
                        }
                    }
                }
            } finally {
                if (termConvertor instanceof IndexReaderInjection) {
                    ((IndexReaderInjection)termConvertor).setIndexReader(null);
                }
            }
        } finally {
            if (convertor instanceof IndexReaderInjection) {
                ((IndexReaderInjection)convertor).setIndexReader(null);
            }
        }
    } finally {
        dirCache.releaseReader(in);
    }
}
 
Example #14
Source File: LuceneIndex.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void query (
        final @NonNull Collection<? super T> result,
        final @NonNull Convertor<? super Document, T> convertor,
        @NullAllowed FieldSelector selector,
        final @NullAllowed AtomicBoolean cancel,
        final @NonNull Query... queries
        ) throws IOException, InterruptedException {
    Parameters.notNull("queries", queries);   //NOI18N
    Parameters.notNull("convertor", convertor); //NOI18N
    Parameters.notNull("result", result);       //NOI18N   
    
    if (selector == null) {
        selector = AllFieldsSelector.INSTANCE;
    }
    IndexReader in = null;
    try {
        in = dirCache.acquireReader();
        if (in == null) {
            LOGGER.log(Level.FINE, "{0} is invalid!", this);
            return;
        }
        final BitSet bs = new BitSet(in.maxDoc());
        final Collector c = new BitSetCollector(bs);
        final IndexSearcher searcher = new IndexSearcher(in);
        try {
            for (Query q : queries) {
                if (cancel != null && cancel.get()) {
                    throw new InterruptedException ();
                }
                searcher.search(q, c);
            }
        } finally {
            searcher.close();
        }
        if (convertor instanceof IndexReaderInjection) {
            ((IndexReaderInjection)convertor).setIndexReader(in);
        }
        try {
            for (int docNum = bs.nextSetBit(0); docNum >= 0; docNum = bs.nextSetBit(docNum+1)) {
                if (cancel != null && cancel.get()) {
                    throw new InterruptedException ();
                }
                final Document doc = in.document(docNum, selector);
                final T value = convertor.convert(doc);
                if (value != null) {
                    result.add (value);
                }
            }
        } finally {
            if (convertor instanceof IndexReaderInjection) {
                ((IndexReaderInjection)convertor).setIndexReader(null);
            }
        }
    } finally {
        dirCache.releaseReader(in);
    }
}
 
Example #15
Source File: MemoryIndex.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public <S, T> void queryDocTerms(
        @NonNull Map<? super T, Set<S>> result,
        @NonNull Convertor<? super Document, T> convertor,
        @NonNull Convertor<? super Term, S> termConvertor,
        @NullAllowed FieldSelector selector,
        @NullAllowed AtomicBoolean cancel,
        @NonNull Query... queries) throws IOException, InterruptedException {
    Parameters.notNull("result", result);   //NOI18N
    Parameters.notNull("convertor", convertor);   //NOI18N
    Parameters.notNull("termConvertor", termConvertor); //NOI18N
    Parameters.notNull("queries", queries);   //NOI18N
    
    
    if (selector == null) {
        selector = AllFieldsSelector.INSTANCE;
    }

    lock.readLock().lock();
    try {
        final IndexReader in = getReader();
        if (in == null) {
            return;
        }
        final BitSet bs = new BitSet(in.maxDoc());
        final Collector c = new BitSetCollector(bs);
        final Searcher searcher = new IndexSearcher(in);
        final TermCollector termCollector = new TermCollector(c);
        try {
            for (Query q : queries) {
                if (cancel != null && cancel.get()) {
                    throw new InterruptedException ();
                }
                if (q instanceof TermCollector.TermCollecting) {
                    ((TermCollector.TermCollecting)q).attach(termCollector);
                } else {
                    throw new IllegalArgumentException (
                            String.format("Query: %s does not implement TermCollecting",    //NOI18N
                            q.getClass().getName()));
                }
                searcher.search(q, termCollector);
            }
        } finally {
            searcher.close();
        }

        for (int docNum = bs.nextSetBit(0); docNum >= 0; docNum = bs.nextSetBit(docNum+1)) {
            if (cancel != null && cancel.get()) {
                throw new InterruptedException ();
            }
            final Document doc = in.document(docNum, selector);
            final T value = convertor.convert(doc);
            if (value != null) {
                final Set<Term> terms = termCollector.get(docNum);
                if (terms != null) {
                    result.put (value, convertTerms(termConvertor, terms));
                }
            }
        }
    } finally {
        lock.readLock().unlock();
    }
}
 
Example #16
Source File: MemoryIndex.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void query(
        @NonNull Collection<? super T> result,
        @NonNull Convertor<? super Document, T> convertor,
        @NullAllowed FieldSelector selector,
        @NullAllowed AtomicBoolean cancel,
        @NonNull Query... queries) throws IOException, InterruptedException {
    Parameters.notNull("queries", queries);   //NOI18N
    Parameters.notNull("convertor", convertor); //NOI18N
    Parameters.notNull("result", result);       //NOI18N   
    
    if (selector == null) {
        selector = AllFieldsSelector.INSTANCE;
    }
    
    lock.readLock().lock();
    try {
        final IndexReader in = getReader();
        if (in == null) {
            return;
        }
        final BitSet bs = new BitSet(in.maxDoc());
        final Collector c = new BitSetCollector(bs);
        final Searcher searcher = new IndexSearcher(in);
        try {
            for (Query q : queries) {
                if (cancel != null && cancel.get()) {
                    throw new InterruptedException ();
                }
                searcher.search(q, c);
            }
        } finally {
            searcher.close();
        }        
        for (int docNum = bs.nextSetBit(0); docNum >= 0; docNum = bs.nextSetBit(docNum+1)) {
            if (cancel != null && cancel.get()) {
                throw new InterruptedException ();
            }
            final Document doc = in.document(docNum, selector);
            final T value = convertor.convert(doc);
            if (value != null) {
                result.add (value);
            }
        }
    } finally {
        lock.readLock().unlock();
    }
}
 
Example #17
Source File: Queries.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a FieldSelector loading the given fields.
 * @param fieldsToLoad the fields to be loaded into the document.
 * @return the created FieldSelector
 */
public static FieldSelector createFieldSelector(final @NonNull String... fieldsToLoad) {
    return new FieldSelectorImpl(fieldsToLoad);
}
 
Example #18
Source File: Index.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Queries the {@link Index} by given queries. In addition to documents it also collects the terms which matched the queries.
 * @param result the {@link Collection} to store query results into
 * @param convertor the {@link Convertor} used to convert lucene documents into the user objects added into the result
 * @param termConvertor the {@link Convertor} used to convert lucene terms into the user objects added into the result
 * @param selector the selector used to select document's fields which should be loaded, if null all fields are loaded
 * @param cancel the {@link AtomicBoolean} used to cancel the index iteration by the caller. When set to true the iteration
 * is stopped.
 * @param queries the queries to be performed on the {@link Index}
 * @throws IOException in case of IO problem
 * @throws InterruptedException when query was canceled
 */
<S, T> void queryDocTerms(Map<? super T, Set<S>> result, @NonNull Convertor<? super Document, T> convertor, @NonNull Convertor<? super Term, S> termConvertor,@NullAllowed FieldSelector selector, @NullAllowed AtomicBoolean cancel, @NonNull Query... queries) throws IOException, InterruptedException;
 
Example #19
Source File: Index.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Queries the {@link Index} by given queries.
 * @param result the {@link Collection} to store query results into
 * @param convertor the {@link Convertor} used to convert lucene documents into the user objects added into the result
 * @param selector the selector used to select document's fields which should be loaded, if null all fields are loaded
 * @param cancel the {@link AtomicBoolean} used to cancel the index iteration by the caller. When set to true the iteration
 * is stopped.
 * @param queries the queries to be performed on the {@link Index}
 * @throws IOException in case of IO problem
 * @throws InterruptedException when query was canceled
 */
<T> void query (Collection<? super T> result, @NonNull Convertor<? super Document, T> convertor, @NullAllowed FieldSelector selector, @NullAllowed AtomicBoolean cancel, @NonNull Query... queries) throws IOException, InterruptedException;
 
Example #20
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 votes vote down vote up
T get(int n, FieldSelector fieldSelector) throws IOException;